var r=(d,i,a)=>new Promise((c,m)=>{var l=t=>{try{n(a.next(t))}catch(u){m(u)}},p=t=>{try{n(a.throw(t))}catch(u){m(u)}},n=t=>t.done?c(t.value):Promise.resolve(t.value).then(l,p);n((a=a.apply(d,i)).next())});import{u as R}from"./useEntityForm-CMwp7TbN.js";import{u as $}from"./useRoleAccess-rNIDgdat.js";import{b6 as o}from"./bootstrap-Bqxdt4iG.js";import{u as U}from"./customers-CYPE-oGV.js";import{u as j}from"./stock-outs-YMFu1xyT.js";import{O as C,o as q}from"./tms-orders-DrMtRe-L.js";import{_ as z}from"./customer-create-drawer.vue_vue_type_script_setup_true_lang-BxaYQnGR.js";import{a1 as T,L as O,E as b,U as G,ap as H,aj as J,p as v,a9 as y}from"../jse/index-index-BUJ_4tQs.js";import{u as K}from"./use-drawer-CLDmRz16.js";const S="__add_new__",me=T({__name:"order-form",props:{order:{default:null},mode:{},defaultCustomerId:{default:""}},emits:["cancel","submit"],setup(d,{expose:i,emit:a}){const c=d,m=a,l=U(),{isWarehouseScoped:p}=$(),n=j(),[t,u]=K({connectedComponent:z}),f=O([]),g={label:`+ ${o("page.tms.orders.form.addNewCustomer")}`,value:S},_=()=>r(null,null,function*(){try{const e=yield p?l.fetchOptionsByCurrentWarehouses():l.fetchOptions();f.value=e}catch(e){console.error("Failed to load customer options:",e),f.value=[]}}),N=b(()=>[g,...f.value]);_();const h=O([]),k=O(""),I=e=>r(null,null,function*(){try{h.value=yield n.fetchOptions()}catch(s){console.error("Failed to load stock out options:",s),h.value=[]}}),E=(e,s)=>r(null,null,function*(){if(e===S){yield w.setValues({customerId:void 0}),u.open();return}k.value=e,yield I(),yield w.setValues({stockOutIds:[]})}),V=e=>r(null,null,function*(){yield _(),yield w.setValues({customerId:e.id})}),P={customerId:c.defaultCustomerId||"",date:"",status:C.NEW,remarks:"",stockOutIds:[]},W=b(()=>[{component:"Select",componentProps:{"data-testid":"order-form-customer-select",id:"order-form-customer-select",options:N.value,onChange:E,placeholder:o("page.tms.orders.form.customerPlaceholder"),showSearch:!0,filterOption:(e,s)=>s.value===S||s.label.toLowerCase().includes(e.toLowerCase())},fieldName:"customerId",label:o("page.tms.orders.form.customer"),rules:"selectRequired"},{component:"Select",componentProps:{"data-testid":"order-form-status-select",options:q,placeholder:o("page.tms.orders.form.statusPlaceholder"),defaultValue:C.NEW},fieldName:"status",label:o("page.tms.orders.form.status"),defaultValue:C.NEW},{component:"Select",componentProps:{"data-testid":"order-form-stock-outs-select",options:h.value,placeholder:o("page.tms.orders.form.stockOutsPlaceholder"),mode:"multiple",showSearch:!0,allowClear:!0,filterOption:(e,s)=>s.label.toLowerCase().includes(e.toLowerCase())},fieldName:"stockOutIds",label:o("page.tms.orders.form.stockOuts")},{component:"Textarea",componentProps:{"data-testid":"order-form-remarks-input",placeholder:o("page.tms.orders.form.remarksPlaceholder"),autoSize:{minRows:2}},fieldName:"remarks",label:o("page.tms.orders.form.remarks")}]),{Form:A,formApi:w,focus:D,isDirty:F,resetForm:L,submitForm:x,watchEntity:B}=R({emptyValues:P,firstInputId:"order-form-customer-select",schema:W,wrapperClass:"grid-cols-1"});return B(()=>c.order,()=>c.mode),G(()=>c.order,e=>r(null,null,function*(){e!=null&&e.customerId&&(k.value=e.customerId,yield I(e.customerId))}),{immediate:!0}),i({cancel:()=>{m("cancel")},focus:D,isDirty:F,resetForm:L,submit:()=>r(null,null,function*(){const e=yield x();e&&m("submit",e)})}),(e,s)=>(J(),H("div",null,[v(y(A)),v(y(t),{onSuccess:V})]))}});export{me as _};
