import{a as e,t}from"./react-DK-MTtD8.js";import{s as n}from"./utils-CXcsdpX-.js";import{t as r}from"./save-DstqMto4.js";import{Zo as i,aa as a,is as o,rs as s,za as c}from"./index-Ct5uU080.js";import{t as l}from"./page-content-Bh5Zxb_Y.js";import{t as u}from"./card-CCLQVS9k.js";import{t as d}from"./error-alert-eQ6ClPXs.js";import{t as f}from"./form-field-BuHvZ1sK.js";import{t as p}from"./server-search-select-Bxk6lpK3.js";import{t as m}from"./validators-CNWWIxpC.js";import{t as h}from"./form-textarea-DbADTssk.js";import{P as g,Y as _,f as v}from"./serviceApi-Bn3V4hja.js";var y=e(t(),1),b=n();function x(){let e=s(),{id:t}=o(),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)(c,{variant:`ghost`,size:`icon`,onClick:()=>e(`/service-requests`),children:(0,b.jsx)(i,{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)(a,{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)(a,{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)(a,{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)(c,{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};