var p=(c,d,l)=>new Promise((o,n)=>{var f=r=>{try{i(l.next(r))}catch(u){n(u)}},w=r=>{try{i(l.throw(r))}catch(u){n(u)}},i=r=>r.done?o(r.value):Promise.resolve(r.value).then(f,w);i((l=l.apply(c,d)).next())});import{c9 as m,b6 as s,c8 as M,bi as h}from"./bootstrap-Bqxdt4iG.js";import{u as T}from"./useEntityForm-CMwp7TbN.js";import{u as W}from"./useRoleAccess-rNIDgdat.js";import{u as q}from"./warehouses-BnkLPhlE.js";import{a1 as x,L as R,E as D,U as g,ai as V,aj as k,a9 as B}from"../jse/index-index-BUJ_4tQs.js";const X=x({__name:"wms-user-form",props:{user:{default:null},mode:{},defaultRole:{default:m.STAFF}},emits:["cancel","submit"],setup(c,{expose:d,emit:l}){var b,S,P;const o=c,n=l,f=q(),{isWarehouseScoped:w,warehouseIds:i}=W(),r=R([]);p(null,null,function*(){try{r.value=yield f.fetchOptions()}catch(e){console.error("Failed to fetch warehouse options:",e),r.value=[]}});const t=R((P=(S=(b=o.user)==null?void 0:b.role)!=null?S:o.defaultRole)!=null?P:m.STAFF),I={email:"",password:void 0,role:m.STAFF,warehouseIds:w?[...i]:[],name:"",phone:"",commissionRate:null},v=D(()=>{const e=[{component:"Select",componentProps:{class:"w-48","data-testid":"wms-user-form-role-select",disabled:o.mode==="edit",options:M.map(a=>({label:a.label,value:a.value})),placeholder:s("page.wmsUsers.form.rolePlaceholder"),onChange:a=>{t.value=a}},fieldName:"role",label:s("page.wmsUsers.form.role"),rules:"selectRequired"},{component:"Input",componentProps:{"data-testid":"wms-user-form-email-input",id:"wms-user-form-email-input",placeholder:s("page.wmsUsers.form.emailPlaceholder"),type:"email",autocomplete:"off"},fieldName:"email",label:s("page.wmsUsers.form.email"),rules:h().min(1,s("page.wmsUsers.form.emailRequired")).email(s("page.wmsUsers.form.emailInvalid"))},{component:"InputPassword",componentProps:{"data-testid":"wms-user-form-password-input",autocomplete:"new-password",placeholder:o.mode==="edit"?s("page.wmsUsers.form.passwordPlaceholderEdit"):s("page.wmsUsers.form.passwordPlaceholder")},fieldName:"password",label:s("page.wmsUsers.form.password"),rules:o.mode==="edit"?h().optional().refine(a=>!a||a.length>=6,s("page.wmsUsers.form.passwordMinLength",{min:6})):h().min(1,s("page.wmsUsers.form.passwordRequired")).min(6,s("page.wmsUsers.form.passwordMinLength",{min:6}))},{component:"Input",componentProps:{"data-testid":"wms-user-form-name-input",placeholder:s("page.wmsUsers.form.namePlaceholder")},fieldName:"name",label:s("page.wmsUsers.form.name")},{component:"Input",componentProps:{"data-testid":"wms-user-form-phone-input",placeholder:s("page.wmsUsers.form.phonePlaceholder")},fieldName:"phone",label:s("page.wmsUsers.form.phone")},...t.value===m.CUSTOMER?[]:[{component:"Select",componentProps:{"data-testid":"wms-user-form-warehouses-select",allowClear:!0,disabled:t.value===m.SUPER_ADMIN,filterOption:(a,E)=>E.label.toLowerCase().includes(a.toLowerCase()),mode:"multiple",options:r.value,placeholder:s("page.wmsUsers.form.warehousesPlaceholder")||"Select one or more warehouses",showSearch:!0},fieldName:"warehouseIds",label:s("page.wmsUsers.form.warehouses")||"Warehouses",rules:"selectRequired"}]];return t.value===m.SALESMAN?[...e,{component:"InputNumber",componentProps:{"data-testid":"wms-user-form-commission-rate-input",min:0,max:100,step:.5,placeholder:s("page.wmsUsers.form.commissionRatePlaceholder"),addonAfter:"%",style:{width:"30%"}},fieldName:"commissionRate",label:s("page.wmsUsers.form.commissionRate")}]:e}),{Form:F,formApi:U,focus:y,isDirty:A,resetForm:N,submitForm:C,watchEntity:_}=T({emptyValues:I,firstInputId:"wms-user-form-email-input",schema:v,wrapperClass:"grid-cols-1"});return _(()=>o.user,()=>o.mode),g(()=>o.defaultRole,e=>p(null,null,function*(){o.mode==="create"&&!o.user&&(t.value=e!=null?e:m.STAFF,yield U.setValues({role:t.value}))}),{immediate:!0}),g(()=>{var e;return(e=o.user)==null?void 0:e.role},e=>{e&&(t.value=e)},{immediate:!0}),g([t,r],E=>p(null,[E],function*([e,a]){if(e===m.SUPER_ADMIN&&a.length>0){const O=a.map(L=>L.value);yield U.setValues({warehouseIds:O})}}),{immediate:!0}),d({cancel:()=>{n("cancel")},focus:y,isDirty:A,resetForm:N,submit:()=>p(null,null,function*(){const e=yield C();e&&n("submit",e)})}),(e,a)=>(k(),V(B(F)))}});export{X as _};
