import{a as e,t}from"./react-D0JuimcS.js";import{o as n}from"./utils-Dac180Gn.js";import{t as r}from"./save-Cm-Rqwuf.js";import{$ as i,Fo as a,Io as o,Ki as s,V as c,ga as l,ko as u,kr as d,lt as f}from"./index-8M5AnKdQ.js";import{t as p}from"./page-content-DDtdeMxM.js";import{t as m}from"./card-BVFw-jgA.js";import{t as h}from"./error-alert-qe7c2K0-.js";import{t as g}from"./form-field-BvCE9I3t.js";import{t as _}from"./form-select-RAg3eCTA.js";import{t as v}from"./validators-CNWWIxpC.js";var y=e(t(),1),b=n();function x(){let e=a(),{id:t}=o(),n=!!t,[x,S]=(0,y.useState)({name:``,scope_level:`company`,branch:``,project_reference:``,minimum_margin_percentage:`10`,escalation_required:!1,status:`active`}),[C,w]=(0,y.useState)({}),[T,E]=(0,y.useState)({}),{data:D}=i(t,{skip:!n}),{data:O=[]}=d(),[k,{isLoading:A}]=c(),[j,{isLoading:M}]=f();(0,y.useEffect)(()=>{D&&n&&S({name:D.name||``,scope_level:D.scope_level||`company`,branch:D.branch||``,project_reference:D.project_reference||``,minimum_margin_percentage:D.minimum_margin_percentage||`10`,escalation_required:D.escalation_required||!1,status:D.status||`active`})},[D,n]);let N=()=>{let e={};return x.name||(e.name=`Name is required`),x.scope_level===`branch`&&!x.branch&&(e.branch=`Branch is required for branch scope`),x.minimum_margin_percentage||(e.minimum_margin_percentage=`Minimum margin is required`),w(e),Object.keys(e).length===0},P=(e,t)=>{S(n=>({...n,[e]:t})),C[e]&&w(t=>({...t,[e]:``}))};return(0,b.jsxs)(p,{children:[(0,b.jsxs)(`div`,{className:`flex items-center gap-4`,children:[(0,b.jsx)(l,{variant:`ghost`,size:`icon`,onClick:()=>e(`/masters/margin-thresholds`),children:(0,b.jsx)(u,{className:`h-4 w-4`})}),(0,b.jsxs)(`div`,{children:[(0,b.jsx)(`h1`,{className:`text-3xl font-bold`,children:n?`Edit Margin Threshold`:`New Margin Threshold`}),(0,b.jsx)(`p`,{className:`text-muted-foreground`,children:n?`Editing ${D?.code||``}`:`Configure a new margin threshold`})]})]}),(0,b.jsx)(`form`,{onSubmit:async r=>{if(r.preventDefault(),E({}),N())try{let r={...x};x.scope_level!==`branch`&&delete r.branch,x.scope_level!==`project`&&delete r.project_reference,n?await j({id:t,data:r}).unwrap():await k(r).unwrap(),e(`/masters/margin-thresholds`)}catch(e){E(v(e))}},children:(0,b.jsxs)(m,{className:`p-6 space-y-4 max-w-2xl`,children:[(0,b.jsx)(h,{errors:T}),(0,b.jsx)(g,{label:`Name`,required:!0,placeholder:`Enter threshold name`,value:x.name,onChange:e=>P(`name`,e.target.value),error:C.name}),(0,b.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,b.jsx)(_,{label:`Scope Level`,value:x.scope_level,onChange:e=>P(`scope_level`,e.target.value),options:[{value:`company`,label:`Company`},{value:`branch`,label:`Branch`},{value:`project`,label:`Project`}]}),x.scope_level===`branch`&&(0,b.jsx)(_,{label:`Branch`,required:!0,value:x.branch,onChange:e=>P(`branch`,e.target.value),error:C.branch,options:O.map(e=>({value:e.id,label:e.name})),placeholder:`Select Branch`}),x.scope_level===`project`&&(0,b.jsx)(g,{label:`Project Reference`,placeholder:`Enter project reference`,value:x.project_reference,onChange:e=>P(`project_reference`,e.target.value)})]}),(0,b.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,b.jsx)(g,{label:`Minimum Margin %`,required:!0,type:`number`,step:`0.01`,value:x.minimum_margin_percentage,onChange:e=>P(`minimum_margin_percentage`,e.target.value),error:C.minimum_margin_percentage}),(0,b.jsx)(_,{label:`Status`,value:x.status,onChange:e=>P(`status`,e.target.value),options:[{value:`active`,label:`Active`},{value:`inactive`,label:`Inactive`}]})]}),(0,b.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,b.jsx)(`input`,{type:`checkbox`,id:`escalation`,checked:x.escalation_required,onChange:e=>P(`escalation_required`,e.target.checked),className:`rounded`}),(0,b.jsx)(s,{htmlFor:`escalation`,children:`Escalation Required`})]}),(0,b.jsxs)(`div`,{className:`flex justify-end gap-4 pt-4`,children:[(0,b.jsx)(l,{type:`button`,variant:`outline`,size:`sm`,onClick:()=>e(`/masters/margin-thresholds`),children:`Cancel`}),(0,b.jsxs)(l,{type:`submit`,size:`sm`,disabled:A||M,children:[(0,b.jsx)(r,{className:`mr-2 h-4 w-4`}),` `,A||M?`Saving...`:`Save`]})]})]})})]})}export{x as default};