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{Fo as i,Io as a,Ki as o,ga as s,ko as c}from"./index-BP3TYB4i.js";import{t as l}from"./page-content-DDtdeMxM.js";import{t as u}from"./card-BVFw-jgA.js";import{t as d}from"./error-alert-qe7c2K0-.js";import{t as f}from"./form-field-Co56F5gt.js";import{t as p}from"./server-search-select-DBsG1oIH.js";import{t as m}from"./validators-CNWWIxpC.js";import{t as h}from"./form-textarea-BNo2HuOu.js";import{P as g,Y as _,f as v}from"./serviceApi-BlxU9Cj1.js";var y=e(t(),1),b=n();function x(){let e=i(),{id:t}=a(),n=!!t,[x,S]=(0,y.useState)({client:``,branch:``,service_type:``,location:``,start_date:new Date().toISOString().split(`T`)[0],end_date:``,scope_of_work:``,remarks:``}),[C,w]=(0,y.useState)({}),[T,E]=(0,y.useState)({}),{data:D}=g(t,{skip:!n}),[O,{isLoading:k}]=v(),[A,{isLoading:j}]=_();(0,y.useEffect)(()=>{D&&n&&S({client:D.client||``,branch:D.branch||``,service_type:D.service_type||``,location:D.location||``,start_date:D.start_date||``,end_date:D.end_date||``,scope_of_work:D.scope_of_work||``,remarks:D.remarks||``})},[D,n]);let M=(e,t)=>{S(n=>({...n,[e]:t})),C[e]&&w(t=>({...t,[e]:``}))},N=()=>{let e={};return x.client||(e.client=`Client is required`),x.branch||(e.branch=`Branch is required`),x.service_type.trim()||(e.service_type=`Service type is required`),x.location||(e.location=`Location is required`),x.start_date||(e.start_date=`Start date is required`),x.end_date||(e.end_date=`End date is required`),x.start_date&&x.end_date&&x.start_date>x.end_date&&(e.end_date=`End date must be on or after start date`),x.scope_of_work.trim()||(e.scope_of_work=`Scope of work is required`),w(e),Object.keys(e).length===0};return(0,b.jsxs)(l,{children:[(0,b.jsxs)(`div`,{className:`flex items-center gap-4`,children:[(0,b.jsx)(s,{variant:`ghost`,size:`icon`,onClick:()=>e(`/service-requests`),children:(0,b.jsx)(c,{className:`h-4 w-4`})}),(0,b.jsx)(`h1`,{className:`text-3xl font-bold`,children:n?`Edit Service Request`:`New Service Request`})]}),(0,b.jsx)(`form`,{onSubmit:async r=>{if(r.preventDefault(),E({}),N())try{n?(await A({id:t,data:x}).unwrap(),e(`/service-requests/${t}`)):(await O(x).unwrap(),e(`/service-requests`))}catch(e){E(m(e))}},children:(0,b.jsxs)(u,{className:`p-6 space-y-4`,children:[(0,b.jsx)(d,{errors:T}),(0,b.jsxs)(`div`,{className:`space-y-2`,children:[(0,b.jsxs)(o,{children:[`Client `,(0,b.jsx)(`span`,{className:`text-destructive`,children:`*`})]}),(0,b.jsx)(p,{url:`/masters/clients/`,params:{status:`active`},value:x.client,onValueChange:e=>M(`client`,e),placeholder:`Select Client`,mapOption:e=>({value:e.id,label:e.name}),initialLabel:n?D?.client_name:void 0}),C.client&&(0,b.jsx)(`p`,{className:`text-xs text-destructive mt-1`,children:C.client})]}),(0,b.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,b.jsxs)(`div`,{className:`space-y-2`,children:[(0,b.jsxs)(o,{children:[`Branch `,(0,b.jsx)(`span`,{className:`text-destructive`,children:`*`})]}),(0,b.jsx)(p,{url:`/users/me/branches/`,value:x.branch,onValueChange:e=>M(`branch`,e),placeholder:`Select Branch`,mapOption:e=>({value:String(e.id),label:e.name}),initialLabel:n?D?.branch_name:void 0}),C.branch&&(0,b.jsx)(`p`,{className:`text-xs text-destructive mt-1`,children:C.branch})]}),(0,b.jsxs)(`div`,{className:`space-y-2`,children:[(0,b.jsxs)(o,{children:[`Location `,(0,b.jsx)(`span`,{className:`text-destructive`,children:`*`})]}),(0,b.jsx)(p,{url:`/masters/locations/`,params:{status:`active`},value:x.location,onValueChange:e=>M(`location`,e),placeholder:`Select Location`,mapOption:e=>({value:e.id,label:e.name}),initialLabel:n?D?.location_name:void 0}),C.location&&(0,b.jsx)(`p`,{className:`text-xs text-destructive mt-1`,children:C.location})]})]}),(0,b.jsx)(f,{label:`Service Type`,required:!0,value:x.service_type,onChange:e=>M(`service_type`,e.target.value),error:C.service_type,placeholder:`e.g. Pump Repair`}),(0,b.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,b.jsx)(f,{label:`Start Date`,required:!0,type:`date`,value:x.start_date,onChange:e=>M(`start_date`,e.target.value),error:C.start_date}),(0,b.jsx)(f,{label:`End Date`,required:!0,type:`date`,value:x.end_date,onChange:e=>M(`end_date`,e.target.value),error:C.end_date})]}),(0,b.jsx)(h,{label:`Scope of Work`,required:!0,value:x.scope_of_work,onChange:e=>M(`scope_of_work`,e.target.value),error:C.scope_of_work,rows:4}),(0,b.jsx)(h,{label:`Remarks`,value:x.remarks,onChange:e=>M(`remarks`,e.target.value),rows:2}),(0,b.jsxs)(s,{type:`submit`,disabled:k||j,children:[(0,b.jsx)(r,{className:`mr-2 h-4 w-4`}),` `,n?`Update`:`Create`,` Service Request`]})]})})]})}export{x as default};