import{i as e}from"./rolldown-runtime-Dd_uD5pT.js";import{t}from"./react-BatatxyT.js";import{s as n}from"./utils-BO1RPuol.js";import{t as r}from"./save-D-WHGflg.js";import{H as i,Hr as a,aa as o,es as s,et as c,os as l,ss as u,ut as d,za as f}from"./index-CsuVxiRQ.js";import{t as p}from"./page-content-DdXVZwik.js";import{t as m}from"./card-CBaLHc_f.js";import{t as h}from"./error-alert-b_a_jMDN.js";import{t as g}from"./form-field-4j0NqnRe.js";import{t as _}from"./form-select-ZXmIlaw9.js";import{t as v}from"./validators-CNWWIxpC.js";var y=e(t(),1),b=n();function x(){let e=l(),{id:t}=u(),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}=c(t,{skip:!n}),{data:O=[]}=a(),[k,{isLoading:A}]=i(),[j,{isLoading:M}]=d();(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)(f,{variant:`ghost`,size:`icon`,onClick:()=>e(`/masters/margin-thresholds`),children:(0,b.jsx)(s,{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)(o,{htmlFor:`escalation`,children:`Escalation Required`})]}),(0,b.jsxs)(`div`,{className:`flex justify-end gap-4 pt-4`,children:[(0,b.jsx)(f,{type:`button`,variant:`outline`,size:`sm`,onClick:()=>e(`/masters/margin-thresholds`),children:`Cancel`}),(0,b.jsxs)(f,{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};