import{u as v,j as e,H as f}from"./app-BQEFzdw6.js";import{A as g}from"./AppLayout-kozc03ce.js";import{P as _}from"./PageHeader-Bj1OQnqr.js";import{F as d}from"./FormField-HY4WrCzQ.js";import{F as w}from"./PlusIcon-ElxOOThC.js";import{F as C}from"./TrashIcon-D0d0jw01.js";import"./useThemeColor-qPCpc713.js";function k({requisition:r,projects:x,inventoryItems:m}){const{data:i,setData:n,put:h,processing:p,errors:c}=v({project_id:r.project_id??"",priority:r.priority,required_by:r.required_by??"",notes:r.notes??"",items:(r.items??[]).map(t=>({id:t.id,inventory_item_id:t.inventory_item_id??"",description:t.description,unit:t.unit,quantity:t.quantity,estimated_rate:t.estimated_rate??"",notes:t.notes??""}))});function j(){n("items",[...i.items,{inventory_item_id:"",description:"",unit:"pcs",quantity:1,estimated_rate:"",notes:""}])}function y(t){n("items",i.items.filter((a,s)=>s!==t))}function l(t,a,s){const o=[...i.items];if(o[t]={...o[t],[a]:s},a==="inventory_item_id"&&s){const u=m.find(b=>b.id===s);u&&(o[t].unit=u.unit)}n("items",o)}function N(t){t.preventDefault(),h(route("procurement.requisitions.update",r.id))}return e.jsxs(g,{children:[e.jsx(f,{title:`Edit ${r.code}`}),e.jsx(_,{title:`Edit Requisition: ${r.code}`,back:route("procurement.requisitions.show",r.id)}),e.jsx("div",{className:"p-4 sm:p-6 max-w-5xl",children:e.jsxs("form",{onSubmit:N,className:"space-y-6",children:[e.jsxs("div",{className:"card p-6",children:[e.jsx("h3",{className:"font-semibold text-gray-700 mb-4",children:"Request Details"}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[e.jsx(d,{label:"Project (optional)",error:c.project_id,children:e.jsxs("select",{className:"form-input",value:i.project_id,onChange:t=>n("project_id",t.target.value),children:[e.jsx("option",{value:"",children:"No project"}),x.map(t=>e.jsxs("option",{value:t.id,children:[t.name," (",t.code,")"]},t.id))]})}),e.jsx(d,{label:"Priority",error:c.priority,required:!0,children:e.jsx("select",{className:"form-input",value:i.priority,onChange:t=>n("priority",t.target.value),children:["low","normal","high","urgent"].map(t=>e.jsx("option",{value:t,className:"capitalize",children:t},t))})}),e.jsx(d,{label:"Required By",error:c.required_by,children:e.jsx("input",{type:"date",className:"form-input",value:i.required_by,onChange:t=>n("required_by",t.target.value)})}),e.jsx(d,{label:"Notes",error:c.notes,children:e.jsx("input",{className:"form-input",value:i.notes,onChange:t=>n("notes",t.target.value)})})]})]}),e.jsxs("div",{className:"card p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("h3",{className:"font-semibold text-gray-700",children:"Items"}),e.jsxs("button",{type:"button",onClick:j,className:"btn btn-secondary text-sm flex items-center gap-1",children:[e.jsx(w,{className:"w-4 h-4"})," Add Item"]})]}),e.jsxs("table",{className:"min-w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"text-left text-xs text-gray-500 uppercase",children:[e.jsx("th",{className:"pb-2 pr-3",children:"Item"}),e.jsx("th",{className:"pb-2 pr-3",children:"Description"}),e.jsx("th",{className:"pb-2 pr-3 w-20",children:"Unit"}),e.jsx("th",{className:"pb-2 pr-3 w-24",children:"Qty"}),e.jsx("th",{className:"pb-2 pr-3 w-28",children:"Est. Rate"}),e.jsx("th",{className:"pb-2 w-8"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-100",children:i.items.map((t,a)=>e.jsxs("tr",{children:[e.jsx("td",{className:"py-2 pr-3",children:e.jsxs("select",{className:"form-input text-xs",value:t.inventory_item_id,onChange:s=>l(a,"inventory_item_id",s.target.value),children:[e.jsx("option",{value:"",children:"Manual"}),m.map(s=>e.jsx("option",{value:s.id,children:s.name},s.id))]})}),e.jsx("td",{className:"py-2 pr-3",children:e.jsx("input",{className:"form-input text-xs",value:t.description,onChange:s=>l(a,"description",s.target.value),placeholder:"Description",required:!0})}),e.jsx("td",{className:"py-2 pr-3",children:e.jsx("input",{className:"form-input text-xs",value:t.unit,onChange:s=>l(a,"unit",s.target.value)})}),e.jsx("td",{className:"py-2 pr-3",children:e.jsx("input",{type:"number",className:"form-input text-xs",value:t.quantity,min:"0.01",step:"0.01",onChange:s=>l(a,"quantity",s.target.value)})}),e.jsx("td",{className:"py-2 pr-3",children:e.jsx("input",{type:"number",className:"form-input text-xs",value:t.estimated_rate,min:"0",step:"0.01",onChange:s=>l(a,"estimated_rate",s.target.value)})}),e.jsx("td",{className:"py-2",children:e.jsx("button",{type:"button",onClick:()=>y(a),className:"text-red-400 hover:text-red-600",children:e.jsx(C,{className:"w-4 h-4"})})})]},a))})]})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsx("button",{type:"submit",disabled:p,className:"btn btn-primary",children:p?"Saving…":"Save Changes"}),e.jsx("a",{href:route("procurement.requisitions.show",r.id),className:"btn",children:"Cancel"})]})]})})]})}export{k as default};
