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{aa as i,es as a,os as o,ss as s,za as c}from"./index-CsuVxiRQ.js";import{t as l}from"./page-content-DdXVZwik.js";import{t as u}from"./card-CBaLHc_f.js";import{t as d}from"./error-alert-b_a_jMDN.js";import{t as f}from"./form-field-4j0NqnRe.js";import{t as p}from"./server-search-select-Dg-Yw5Va.js";import{t as m}from"./validators-CNWWIxpC.js";import{t as h}from"./form-textarea-vnTsM02Z.js";import{P as g,Y as _,f as v}from"./serviceApi-RIObRUm3.js";var y=e(t(),1),b=n();function x(){let e=o(),{id:t}=s(),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)(a,{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)(i,{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)(i,{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)(i,{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};