import{n as ie,r as a,al as ne,j as e,G as g,S as _,ad as u,B as P,q as le,A as oe,t as k}from"./vendor-BuNhBR-q.js";import{B as ce,P as ue}from"./ParentCard-DFHA8fzx.js";import{P as de,C as f,e as S,g as j,U as b,f as me}from"./index-DzxWaMzc.js";import{C as A}from"./CustomSelect-87zOsDzy.js";import{L as he}from"./LogoLoader-B_2Vvdpp.js";import{c as pe}from"./compliance-CA2YZvq4.js";const o=({label:m,minLabelWidth:v=180,children:c})=>e.jsxs(k,{sx:{display:"flex",alignItems:"center",gap:1.5},children:[e.jsx(k,{sx:{minWidth:v},children:e.jsx(me,{sx:{mt:0,mb:.5},children:m})}),e.jsx(k,{sx:{flex:1},children:c})]}),ye=()=>{const m=ie(),v=a.useMemo(()=>{try{return JSON.parse(sessionStorage.getItem("userProfile")||"{}")}catch{return{}}},[]),c=String(v.id??""),[h,q]=a.useState(""),[C,w]=a.useState(""),[y,B]=a.useState(""),[N,E]=a.useState(""),[p,L]=a.useState(""),[W,T]=a.useState(""),[M,R]=a.useState(""),[xe,ge]=a.useState("");ne.useRef(null);const[U,z]=a.useState([]),[G,H]=a.useState([]),[J,$]=a.useState([]),[K,Q]=a.useState(!0),[V,I]=a.useState(!1),[X,O]=a.useState(!1),[Y,D]=a.useState(""),[Z,F]=a.useState("success"),x=t=>{D(t),F("error"),O(!0)},ee=t=>{D(t),F("success"),O(!0)},te=async()=>{var t,i,d;try{const[l,s,n]=await Promise.all([S.get(j(b.getStateMaster,c)),S.get(j(b.getNatureComplianceMaster,c)),S.get(j(b.getSubmissionAuthorityMaster,c))]);(t=l==null?void 0:l.data)!=null&&t.success&&z(l.data.data.map(r=>({value:String(r.id),label:r.name}))),(i=s==null?void 0:s.data)!=null&&i.success&&H(s.data.data.map(r=>({value:String(r.id),label:r.name}))),(d=n==null?void 0:n.data)!=null&&d.success&&$(n.data.data.map(r=>({value:String(r.id),label:r.authority_name})))}catch{x("Failed to load master data")}finally{Q(!1)}};a.useEffect(()=>{te()},[]);const ae=()=>h?C?y?N.trim()?W?M?null:"Submission authority is required":"Nature of compliance is required":"Statute name is required":"State is required":"Due date is required":"Compliance month is required",se=async t=>{var d,l;t.preventDefault();const i=ae();if(i){x(i);return}I(!0);try{const s={compliance_month:h,due_date:C,state_id:Number(y),statute_name:N.trim(),form_name:(p==null?void 0:p.trim())||null,nature_of_compliance_id:Number(W),submission_authority_id:Number(M)},n=j(b.createCompliance,c),r=await S.post(n,s);(d=r==null?void 0:r.data)!=null&&d.success?(ee("Compliance created successfully"),setTimeout(()=>m("/compliance"),1200)):x(((l=r==null?void 0:r.data)==null?void 0:l.message)||"Failed to create compliance")}catch(s){const n=(s==null?void 0:s.message)||(s!=null&&s.errors?s.errors[Object.keys(s.errors)[0]][0]:null);x(n||"Service error")}finally{I(!1)}};if(K||V)return e.jsx(he,{});const re=[{to:"/",title:"Dashboard"},{to:"/compliance",title:"Compliance"},{title:"Add"}];return e.jsxs(de,{title:"Compliance Add",children:[e.jsx(ce,{title:"Compliance",items:re,children:e.jsx("img",{src:pe,alt:"logo",style:{width:100,height:75,objectFit:"contain",padding:0}})}),e.jsx(ue,{title:"",children:e.jsx("form",{onSubmit:se,children:e.jsxs(g,{container:!0,spacing:6,children:[e.jsx(g,{item:!0,xs:12,sm:6,children:e.jsxs(_,{spacing:3,children:[e.jsx(o,{label:"Compliance Month*",children:e.jsx(f,{type:"month",fullWidth:!0,value:h?h.slice(0,7):"",onChange:t=>{const i=t.target.value;i&&q(`${i}-01`)},inputProps:{style:{cursor:"pointer"}}})}),e.jsx(o,{label:"Due Date*",children:e.jsx(f,{type:"date",value:C,onChange:t=>w(t.target.value),fullWidth:!0})}),e.jsx(o,{label:"State*",children:e.jsxs(A,{value:y,onChange:t=>B(t.target.value),fullWidth:!0,children:[e.jsx(u,{value:"",children:"Select State"}),U.map(t=>e.jsx(u,{value:t.value,children:t.label},t.value))]})}),e.jsx(o,{label:"Statute Name*",children:e.jsx(f,{value:N,onChange:t=>E(t.target.value),fullWidth:!0})})]})}),e.jsx(g,{item:!0,xs:12,sm:6,children:e.jsxs(_,{spacing:3,children:[e.jsx(o,{label:"Form Name",children:e.jsx(f,{value:p,onChange:t=>L(t.target.value),fullWidth:!0})}),e.jsx(o,{label:"Nature of Compliance*",children:e.jsxs(A,{value:W,onChange:t=>T(t.target.value),fullWidth:!0,children:[e.jsx(u,{value:"",children:"Select"}),G.map(t=>e.jsx(u,{value:t.value,children:t.label},t.value))]})}),e.jsx(o,{label:"Submission Authority*",children:e.jsxs(A,{value:M,onChange:t=>R(t.target.value),fullWidth:!0,children:[e.jsx(u,{value:"",children:"Select"}),J.map(t=>e.jsx(u,{value:t.value,children:t.label},t.value))]})})]})}),e.jsx(g,{item:!0,xs:12,children:e.jsxs(_,{direction:"row",spacing:2,justifyContent:"center",children:[e.jsx(P,{variant:"contained",type:"submit",sx:{minWidth:135,px:2,fontWeight:501},children:"Submit"}),e.jsx(P,{color:"error",sx:{minWidth:135,px:2,fontWeight:501},onClick:()=>m("/compliance"),children:"Cancel"})]})})]})})}),e.jsx(le,{open:X,autoHideDuration:3e3,onClose:()=>O(!1),anchorOrigin:{vertical:"top",horizontal:"right"},children:e.jsx(oe,{severity:Z,variant:"filled",children:Y})})]})};export{ye as default};
