1import{ay as ae,g as i,j as e,bJ as ne,bq as ie,br as re,az as V}from"./vendor-B15GE7q_.js";import{M as ce}from"./MilkContainer-CGGQPQnV.js";import{T as de}from"./milkCollectionEnums-DTJhPwZH.js";import{be as oe,aw as xe,ax as me,ay as pe,az as ue,aA as fe,B as w}from"./index-C82LlEg8.js";import{M as he}from"./MilkTestDispatchModal-WbEL2v-z.js";import{D as ge}from"./DeliverySignOffStep-DrY-dbS0.js";import{u as ye}from"./useMilkDelivery-yZlNH43_.js";const Ie=()=>{const y=ae(),{submitDirectFarmerDelivery:B,sendCollectorOtp:z,verifyCollectorOtp:q,searchFarms:U,searchFarmersByHolding:k}=ye(),[S,H]=i.useState(1),[r,I]=i.useState(null),[o,j]=i.useState(""),[C,b]=i.useState([]),[_,M]=i.useState(""),[L,D]=i.useState(!1),F=i.useMemo(()=>({currentLevel:0,id:"new-lot"}),[]),[p,G]=i.useState(null),[Y,J]=i.useState(null),[c,u]=i.useState([]),[x,f]=i.useState(null),[E,h]=i.useState(!1),[T,N]=i.useState(null),[g,O]=i.useState(!1),v=i.useMemo(()=>{const s=c.length,l=c.filter(a=>{var n;return((n=a.decision)==null?void 0:n.status)==="accepted"}).reduce((a,n)=>a+(n.totalQuantity||0),0),t=c.filter(a=>{var n;return((n=a.decision)==null?void 0:n.status)==="rejected"}).reduce((a,n)=>a+(n.totalQuantity||0),0);return{totalLots:s,acceptedQuantity:l,rejectedQuantity:t}},[c]);i.useEffect(()=>{r!=null&&r.holdingId?(async()=>{D(!0);try{const l=await k(r.holdingId,{size:100});b(l.data||[])}catch(l){console.error("Failed to load farmers:",l),b([])}finally{D(!1)}})():(b([]),j(""))},[r,k]);const $=()=>{f(null),h(!0)},K=s=>{f(s),h(!0),N(null)},W=s=>{u(l=>l.filter(t=>t.id!==s).map((t,a)=>({...t,lotNumber:a+1}))),N(null)},X=s=>{if(x)u(l=>l.map(t=>t.id===x.id?{...t,...s}:t));else{const l={id:Date.now(),...s,lotNumber:c.length+1};u(t=>[...t,l])}h(!1),f(null)},Z=async()=>{var s,l;if(c.length>0&&o&&!g){O(!0);try{const t={holdingId:r==null?void 0:r.holdingId,supplierPersonId:o,lots:c.map(n=>{var d,P,A,R,Q;return{quantityLitres:n.totalQuantity,decision:((d=n.decision)==null?void 0:d.status)==="accepted"?"ACCEPTED":"REJECTED",tests:((P=n.tests)==null?void 0:P.map(m=>({testType:m.name.toUpperCase().replace(/\s+/g,"_"),value:m.value.toString()})))||[],containerAllocations:((R=(A=n.decision)==null?void 0:A.allocations)==null?void 0:R.map(m=>({containerId:m.containerId,quantityLitres:m.quantity,containerVersion:m.containerVersion??0})))||[],...((Q=n.decision)==null?void 0:Q.status)==="rejected"&&{rejectionReason:n.decision.rejectionReason||"Quality test failed"}}})},a=await B(t);if(a.success)G(a.data.deliveryReceptionId),J(a.data.farmer),H(2);else{const n=a.error||a.message||"Failed to submit direct farmer delivery";console.error("Failed to submit direct farmer delivery:",a),V.error(n)}}catch(t){console.error("Error submitting direct farmer delivery:",t);const a=((l=(s=t==null?void 0:t.response)==null?void 0:s.data)==null?void 0:l.message)||(t==null?void 0:t.message)||"An unexpected error occurred while submitting delivery";V.error(a)}finally{O(!1)}}},ee=S===1?C.find(s=>s.amisId===o):Y,se=async()=>{if(!p)return console.error("No delivery reception ID available"),{success:!1};try{return await z({entityType:"DELIVERY_RECEPTION",entityId:p})}catch(s){return console.error("Error sending OTP:",s),{success:!1}}},te=async(s,l)=>{if(!p)return console.error("No delivery reception ID available"),{success:!1};if(!l||l.length!==6)return console.error("Invalid OTP code"),{success:!1};try{return await q({entityType:"DELIVERY_RECEPTION",entityId:p,otp:l})}catch(t){return console.error("Error verifying OTP:",t),{success:!1}}},le=()=>e.jsxs("div",{className:"flex flex-col gap-5",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("p",{className:"text-xs font-normal text-brandGreen",children:"Step 1 of 2"}),e.jsx("div",{className:"bg-gray-300 w-full h-2.5 rounded-md",children:e.jsx("div",{className:"bg-brandGreen h-2.5 rounded-md",style:{width:"50%"}})})]}),e.jsxs("div",{children:[e.jsx("h2",{className:"text-base font-semibold text-black",children:"Delivery testing and dispatch"}),e.jsx("p",{className:"text-xs text-gray-500",children:"Record the results of tests performed on the delivered milk"})]}),e.jsxs("div",{className:"box-border flex flex-col gap-3 px-[5px] py-[5px] w-full",children:[e.jsx("div",{className:"flex items-center justify-between w-full",children:e.jsxs("div",{className:"flex-1 flex gap-1 items-start",children:[e.jsx("span",{className:"text-base font-semibold text-black leading-6",children:"1."}),e.jsxs("div",{className:"flex-1 flex flex-col",children:[e.jsx("h3",{className:"text-base font-semibold text-black leading-6",children:"Basic Information"}),e.jsx("p",{className:"text-xs text-gray-500 leading-4",children:"Select the farm and farmer from whom you are collecting milk"})]})]})}),e.jsxs("div",{className:"flex flex-col gap-2 w-full",children:[e.jsxs("div",{className:"flex flex-col gap-0.5 w-full",children:[e.jsxs("label",{className:"text-sm font-normal text-black leading-5",children:["Farm",e.jsx("span",{className:"text-[#e72418] tracking-[0.28px]",children:"*"})]}),r?e.jsxs("div",{className:"border border-[#d9d9d9] rounded-md px-3 py-[15p
1x] flex items-center justify-between",children:[e.jsx("div",{className:"flex-1 flex items-center",children:e.jsxs("div",{className:"flex gap-[5px] flex-1 items-start",children:[e.jsx("div",{className:"bg-neutral-100 flex gap-[5px] items-center justify-center px-3 py-2.5 rounded-md",children:e.jsx(ne,{className:"w-4 h-4 text-black"})}),e.jsxs("div",{className:"flex-1 flex flex-col gap-0.5 px-1 py-0",children:[e.jsx("p",{className:"text-sm font-semibold text-black leading-5",children:r.name}),e.jsx("div",{className:"flex gap-5 items-start",children:e.jsxs("div",{className:"flex gap-1 items-center",children:[e.jsx(ie,{className:"w-[15px] h-[15px] text-gray-500"}),e.jsx("span",{className:"text-xs text-gray-500 leading-4",children:r.location})]})})]})]})}),e.jsx("div",{className:"flex gap-2.5 items-center justify-end",children:e.jsx("button",{onClick:()=>{I(null),j(""),u([])},className:"flex gap-[5px] items-center justify-center px-3 py-2.5 rounded-md hover:bg-gray-100 transition-colors",children:e.jsx(re,{className:"w-4 h-4 text-black"})})})]}):e.jsx(oe,{searchCriteria:[{field:"holdingName",label:"Farm"},{field:"holdingCode",label:"Holding Code"},{field:"keyword",label:"Keyword"},{field:"name",label:"Farmer name"}],onSearch:U,placeholder:"Search for farms...",displayField:"name",onSelect:s=>{const l={holdingId:s.holdingId||s.id,name:s.holdingName||s.name,location:s.adminUnitDisplay||s.adminUnitCode?s.adminUnitDisplay||s.adminUnitCode:"N/A",_raw:s};console.log("[Normalized farm]}}}}",s),I(l),M(l.name||"")},inputValue:_,setInputValue:M,returnFields:["holdingName","adminUnitDisplay","adminUnitCode","holdingId"],genericSearch:!0,defaultSearch:!1})]}),r&&e.jsxs("div",{className:"flex flex-col gap-0.5 w-full",children:[e.jsxs("label",{className:"text-sm font-normal text-black leading-5",children:["Animal holding",e.jsx("span",{className:"text-[#e72418] tracking-[0.28px]",children:"*"})]}),e.jsxs(xe,{value:o,onValueChange:j,disabled:L,children:[e.jsx(me,{className:"h-[42px] border-gray-500",children:e.jsx(pe,{placeholder:L?"Loading farmers...":"Select a farmer from the selected farm"})}),e.jsx(ue,{children:C.map(s=>e.jsx(fe,{value:s.amisId,children:s.ownerName},s.amisId))})]})]})]})]}),e.jsx("div",{className:"box-border flex flex-col gap-[14px] px-[5px] py-[5px] w-full",children:e.jsxs("div",{className:"flex flex-col gap-[15px] w-full",children:[e.jsxs("div",{className:"flex items-center justify-between w-full",children:[e.jsxs("div",{className:"flex gap-1 items-start",children:[e.jsx("span",{className:"text-base font-semibold text-black leading-6",children:"2."}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("h3",{className:"text-base font-semibold text-black leading-6",children:"Milk Quality Assessment and Pickup"}),e.jsx("p",{className:"text-xs text-gray-500 leading-4 w-[648px]",children:"Group milk into logical lots based on quality. Perform tests and record results for each lot."})]})]}),e.jsx(w,{onClick:$,className:"bg-[#099773] hover:bg-[#088a66] text-white px-3 py-2.5 rounded-md text-sm font-semibold",children:"Add Milk lot"})]}),e.jsx("div",{className:"flex flex-col gap-2 w-full",children:c.length===0?e.jsx("div",{className:"flex-1 border border-[#d9d9d9] rounded-md flex items-center justify-center px-3 py-2.5 w-full min-h-[329px]",children:e.jsxs("div",{className:"flex flex-col gap-0.5 items-center justify-center text-center",children:[e.jsx("p",{className:"text-sm font-semibold text-black leading-5",children:"No information yet"}),e.jsx("p",{className:"text-xs text-gray-500 leading-4",children:'Click "Add Milk Lot" to proceed'})]})}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex flex-col gap-2 w-full",children:c.map(s=>{var l,t,a,n;return e.jsx("div",{className:"w-full",children:e.jsx(ce,{variant:"lot",name:`Lot ${s.lotNumber}`,description:"true",currentLevel:s.totalQuantity,maxLevel:((t=(l=s.decision)==null?void 0:l.allocations)==null?void 0:t.length)||1,status:((a=s.decision)==null?void 0:a.status)==="accepted"?"Accepted":"Rejected",icon:"milk",showStatusBadge:!0,onMenuClick:()=>N(T===s.id?null:s.id),metrics:((n=s.tests)==null?void 0:n.map(d=>({label:de[d.name]||d.name,value:d.value})))||[],menuContent:T===s.id&&e.jsxs("div",{className:"absolute right-0 top-10 bg-white rounded shadow-md p-[5px] z-10 flex flex-col gap-0.5",children:[e.jsx("button",{onClick:d=>{d.stopPropagation(),K(s)},className:"px-2 py-0 h-5 text-[10px] text-black hover:bg-gray-100 rounded text-left",children:"Edit"}),e.jsx("button",{onClick:d=>{d.stopPropagation(),W(s.id)},className:"px-2 py-0 h-5 text-[10px] text-black hover:bg-gray-100 rounded text-left",children:"Delete"})]})})},s.id)})}),e.jsx("div",{className:"bg-[#f7f7f8] rounded-lg py-2.5 pl-5 w-full",children:e.jsxs("div",{className:"flex gap-24 items-center",children:[e.jsxs("div",{className:"flex-1 flex flex-col gap-0.5",children:[e.jsx("span",{className:"text-sm text-[#666666] leading-5",children:"Total lots"}),e.jsx("span",{className:"text-sm font-semibold text-black leading-5",children:v.totalLots})]}),e.jsxs("div",{className:"flex-1 flex flex-col gap-0.5",children:[e.jsx("span",{className:"text-sm text-[#666666] leading-5",children:"Accepted quantity"}),e.jsxs("span",{className:"text-sm font-semibold text-black leading-5",children:[v.acceptedQuantity,"L"]})]}),e.jsxs("div",{className:"flex-1 flex flex-col gap-0.5",children:[e.jsx("span",{className:"text-sm text-[#666666] leading-5",children:"Rejected quantity"}),e.jsxs("span",{className:"text-sm font-semibold text-black leading-5",children:[v.rejectedQuantity,"L"]})]})]})})]})})]})}),e.jsxs("div",{className:"box-border flex gap-3 items-center justify-end w-full",children:[e.jsx(w,{variant:"secondary",onClick:()=>y(-1),className:"flex gap-[5px] items-center justify-center px-3 py-2.5 rounded-md hover:bg-gray-100 transition-colors",children:e.jsx("span",{className:"text-sm font-semibold text-black leading-5",children:"Cancel"})}),e.jsx(w,{onClick:Z,disabled:c.length===0||!o||g,className:`flex gap-[5px] items-center justify-center px-3 py-2.5 rounded-md transition-colors ${c.length>0&&o&&!g?"bg-[#099773] hover:bg-[#088a66]":"bg-[#d9d9d9] cursor-not-allowed"}`,children:g?e.jsxs(e.Fragment,{children:[e.jsxs("svg",{className:"animate-spin h-4 w-4 text-white",xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",children:[e.jsx("circle",{className:"opacity-25",cx:"12",cy:"12",r:"10",stroke:"currentColor",strokeWidth:"4"}),e.jsx("path",{className:"opacity-75",fill:"currentColor",d:"M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"})]}),e.jsx("span",{className:"text-sm font-semibold text-white leading-5",children:"Submitting..."})]}):e.jsx("span",{className:"text-sm font-semibold text-white leading-5",children:"Submit to sign off"})})]})]});return e.jsxs("div",{className:"flex flex-col gap-2.5 min-h-screen",children:[e.jsxs("div",{className:"bg-white px-[30px] py-3 rounded-md",children:[e.jsx("h1",{className:"text-xl font-bold text-black",children:"New Direct Farmer Delivery"}),e.jsxs("p",{className:"text-xs text-gray-500",children:["Milk collection / Milk collection / ",e.jsx("span",{className:"text-gray-400",children:"New direct delivery"})]})]}),e.jsx("div",{className:"bg-white px-[30px] py-6 rounded-md",children:S===1?le():e.jsx(ge,{person:ee,personType:"farmer",onSendOtp:se,onVerifyOtp:te,showRemoveButton:!1,successMessage:"Delivery Signed-off!",successDescription:"This confirms that both parties agreed to the delivery results....",backButtonText:"Back to Deliveries",onBack:()=>y(-1),onGoToReception:()=>y("/value-chain/milk-delivery/reception-history")})}),E&&e.jsx(he,{isOpen:E,onClose:()=>{h(!1),f(null)},container:x||F,onSubmit:X,mode:"lot"}
1,(x==null?void 0:x.id)||F.id)]})};export{Ie as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.