1import{r as d,j as t}from"./vendor-react-DnPnnzcV.js";import{u as T,r as B,s as N,l as $,p as a,b as G,t as P}from"./index-BWFVt2sG.js";import{s as U,b as Y,f as z,a as W,u as H,v as J}from"./loans-Ckwl4JXe.js";import{E as K,a as Q}from"./ExportCsvButton-Brr4npAw.js";import"./vendor-query-CvVCWwM3.js";import"./vendor-supabase-2Rdar6Gf.js";import"./loader-circle-D9bK8qqy.js";import"./download-IgN3g1Fl.js";const X="id, partner_id, farm_id, reference, purpose, requested_amount, approved_amount, currency, term_months, monitoring_interval_days, status, decision_note, decided_at, monitoring_starts_on, created_at";function ie(){const{lang:s}=T(),{userId:A,isLoading:S}=B(),[v,y]=d.useState("loading"),[h,O]=d.useState([]),[f,R]=d.useState("open"),[m,F]=d.useState(null),[L,Z]=d.useState({}),[E,M]=d.useState({}),[D,_]=d.useState(null),j=d.useCallback(async()=>{if(!A)return;y("loading");const{data:e,error:n}=await N.from("finance_loan_cases").select(X).order("created_at",{ascending:!1}).limit(300);if(n){$.warn("partner loan cases load failed",n),y(n.code==="42501"?"denied":"error");return}const o=e??[],x=o.map(i=>i.id);let p=[];if(x.length>0){const{data:i}=await N.from("finance_disbursements").select("id, loan_case_id, amount, currency, disbursed_on, method, reference").in("loan_case_id",x);p=i??[]}const b=[...new Set(o.map(i=>i.farm_id))];let g={};if(b.length>0){const{data:i}=await N.from("farms").select("id, name").in("id",b);g=Object.fromEntries((i??[]).map(I=>[I.id,I.name]))}O(U(Y(o,p,g))),y("ready")},[A]);d.useEffect(()=>{S||j()},[S,j]);const c=d.useMemo(()=>z(h,f),[h,f]),l=d.useMemo(()=>W(h),[h]),r={title:a({en:"Loan cases",ar:"Ù
ÙÙØ§Øª Ø§ÙØªÙ
ÙÙÙ"},s),sub:a({en:"Your institution's cases on shared farms. Zr3i shows field evidence only â the decision is yours.",ar:"Ù
ÙÙØ§Øª Ù
ؤسست٠عÙ٠اÙÙ
زارع اÙÙ
Ø´Ø§Ø±ÙØ©. Zr3i بتعرض Ø£Ø¯ÙØ© Ø§ÙØÙ٠بس â Ø§ÙÙØ±Ø§Ø± ÙØ±Ø§Ø±Ù."},s),loading:a({en:"Loadingâ¦",ar:"Ø¬Ø§Ø±Ù Ø§ÙØªØÙ
ÙÙâ¦"},s),denied:a({en:"You don't have partner access.",ar:"Ù
اÙÙØ´ ØµÙØ§ØÙØ© شرÙÙ."},s),error:a({en:"Couldn't load the cases.",ar:"ØªØ¹Ø°ÙØ± تØÙ
Ù٠اÙÙ
ÙÙØ§Øª."},s),retry:a({en:"Try again",ar:"ØØ§Ù٠تاÙÙ"},s),empty:a({en:"No cases in this view.",ar:"Ù
ÙÙØ´ Ù
ÙÙØ§Øª ÙÙ Ø§ÙØ¹Ø±Ø¶ دÙ."},s),open:a({en:"Under review",ar:"ØªØØª اÙÙ
راجعة"},s),approved:a({en:"Approved",ar:"Ù
ÙØ§Ù٠عÙÙÙ"},s),due:a({en:"Re-check due",ar:"Ù
راجعة Ù
ستØÙØ©"},s),all:a({en:"All",ar:"اÙÙÙ"},s),total:a({en:"Cases",ar:"اÙÙ
ÙÙØ§Øª"},s),approvedValue:a({en:"Approved value",ar:"ÙÙÙ
Ø© اÙÙ
ÙØ§ÙÙØ§Øª"},s),disbursed:a({en:"Paid out",ar:"اÙÙ
صرÙÙ"},s),outstanding:a({en:"Not yet paid",ar:"ÙØ³Ù Ù
ا Ø§ØªØµØ±ÙØ´"},s),farm:a({en:"Farm",ar:"اÙÙ
زرعة"},s),requested:a({en:"Requested",ar:"اÙÙ
Ø·ÙÙØ¨"},s),nextCheck:a({en:"Next farm re-check",ar:"اÙÙ
راجعة Ø§ÙØ¬Ø§ÙØ© ÙÙÙ
زرعة"},s),overdue:a({en:"Re-check overdue",ar:"اÙÙ
راجعة Ù
تأخرة"},s),every:a({en:"Every",ar:"ÙÙ"},s),days:a({en:"days",ar:"ÙÙÙ
"},s),amount:a({en:"Approved amount",ar:"اÙÙ
Ø¨ÙØº اÙÙ
ÙØ§Ù٠عÙÙÙ"},s),note:a({en:"Decision note",ar:"Ù
ÙØ§ØØ¸Ø© اÙÙØ±Ø§Ø±"},s),approve:a({en:"Record approval",ar:"تسجÙ٠اÙÙ
ÙØ§ÙÙØ©"},s),decline:a({en:"Record decline",ar:"تسجÙÙ Ø§ÙØ±Ùض"},s),close:a({en:"Close case",ar:"Ø¥ØºÙØ§Ù اÙÙ
ÙÙ"},s),saving:a({en:"Savingâ¦",ar:"Ø¬Ø§Ø±Ù Ø§ÙØÙØ¸â¦"},s),saveFailed:a({en:"Couldn't save that.",ar:"ØªØ¹Ø°ÙØ± Ø§ÙØÙØ¸."},s),needAmount:a({en:"Enter an approved amount above zero.",ar:"Ø§ÙØªØ¨ Ù
Ø¨ÙØº Ù
ÙØ§ÙÙØ© Ø£ÙØ¨Ø± Ù
Ù ØµÙØ±."},s),needNote:a({en:"Add a note explaining the decline.",ar:"Ø§ÙØªØ¨ سبب Ø§ÙØ±Ùض."},s),governance:a({en:"Zr3i never recommends approving or declining. Field signals are evidence to check.",ar:"Zr3i عÙ
Ø±ÙØ§ Ù
ا Ø¨ØªÙØµÙ باÙÙ
ÙØ§ÙÙØ© Ø£Ù Ø§ÙØ±Ùض. إشارات Ø§ÙØÙ٠دÙÙÙ ÙÙÙ
راجعة بس."},s)},w=s==="ar"?"ar-EG":"en-GB",q=new Intl.DateTimeFormat(w,{dateStyle:"medium"}),k=new Intl.NumberFormat(w,{maximumFractionDigits:0}
1),u=(e,n)=>new Intl.NumberFormat(w,{style:"currency",currency:n,maximumFractionDigits:0}).format(e),V={under_review:r.open,approved:r.approved,declined:a({en:"Declined",ar:"Ù
رÙÙØ¶"},s),withdrawn:a({en:"Withdrawn",ar:"Ù
ÙØ³ØØ¨"},s),closed:a({en:"Closed",ar:"Ù
غÙÙ"},s)};async function C(e,n){const o=E[e.id],x=n==="approved"?o?Number(o):e.approvedAmount??e.requestedAmount??null:null,p=L[e.id]??e.decisionNote??"",b=J({status:n,approvedAmount:x,note:p});if(b.length>0){_(b.includes("note_required")?r.needNote:r.needAmount);return}_(null),F(e.id);const{error:g}=await N.rpc("finance_record_decision",{_case:e.id,_status:n,_approved_amount:x,_note:p.trim()||null,_monitoring_interval_days:null});if(F(null),g){$.warn("loan decision failed",g),_(r.saveFailed);return}P("partner_loan_decision",{case_id:e.id}),await j()}return t.jsxs("div",{className:"mx-auto w-full max-w-4xl px-4 py-6",children:[t.jsx(G,{title:`${r.title} | Zr3i`,description:r.sub,noIndex:!0}),t.jsx("h1",{className:"text-2xl font-semibold",children:r.title}),t.jsx("p",{className:"mt-1 text-sm text-muted-foreground",children:r.sub}),v==="loading"&&t.jsx("p",{className:"mt-6 text-sm text-muted-foreground",children:r.loading}),v==="denied"&&t.jsx("p",{className:"mt-6 rounded-2xl border bg-card p-4 text-sm",children:r.denied}),v==="error"&&t.jsxs("div",{className:"mt-6 rounded-2xl border bg-card p-4",children:[t.jsx("p",{className:"text-sm",children:r.error}),t.jsx("button",{type:"button",onClick:()=>void j(),className:"mt-3 h-11 rounded-xl bg-primary px-5 text-sm font-semibold text-primary-foreground",children:r.retry})]}),v==="ready"&&t.jsxs(t.Fragment,{children:[t.jsx("dl",{className:"mt-4 grid grid-cols-2 gap-3 sm:grid-cols-4",children:[{k:r.total,v:k.format(l.total)},{k:r.due,v:k.format(l.overdueChecks)},{k:r.approvedValue,v:u(l.approvedAmount,l.currency)},{k:r.disbursed,v:u(l.disbursedAmount,l.currency)}].map(e=>t.jsxs("div",{className:"rounded-2xl border bg-card p-4",children:[t.jsx("dt",{className:"text-xs text-muted-foreground",children:e.k}),t.jsx("dd",{className:"mt-1 text-lg font-semibold",children:e.v})]},e.k))}),t.jsx("div",{className:"mt-4",children:t.jsx(K,{kind:"loan-book",build:()=>Q(c),partnerId:c[0]?.partnerId??null,purpose:"loan_monitoring",disabled:c.length===0,label:{en:"Download loan book (CSV)",ar:"ØªÙØ²ÙÙ Ø¯ÙØªØ± اÙÙØ±Ùض (CSV)"}})}),t.jsx("div",{className:"mt-4 flex flex-wrap gap-2",children:["open","approved","due","all"].map(e=>t.jsx("button",{type:"button",onClick:()=>R(e),"aria-pressed":f===e,className:`h-11 rounded-xl px-4 text-sm font-medium ${f===e?"bg-primary text-primary-foreground":"border border-border"}`,children:e==="open"?r.open:e==="approved"?r.approved:e==="due"?r.due:r.all},e))}),D&&t.jsx("p",{className:"mt-3 text-sm text-destructive",children:D}),c.length===0?t.jsx("p",{className:"mt-6 rounded-2xl border bg-card p-4 text-sm text-muted-foreground",children:r.empty}):t.jsx("ul",{className:"mt-4 space-y-3",children:c.map(e=>t.jsxs("li",{className:"rounded-2xl border bg-card p-4",children:[t.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-2",children:[t.jsxs("div",{children:[t.jsx("p",{className:"font-semibold",children:e.farmName??r.farm}),t.jsxs("p",{className:"text-xs text-muted-foreground",children:[e.reference??e.id.slice(0,8)," · ",q.format(new Date(e.createdAt))]})]}),t.jsx("span",{className:"rounded-full bg-muted px-3 py-1 text-xs font-medium",children:V[e.status]})]}),t.jsxs("dl",{className:"mt-3 grid grid-cols-2 gap-3 text-sm sm:grid-cols-4",children:[t.jsxs("div",{children:[t.jsx("dt",{className:"text-xs text-muted-foreground",children:r.requested}),t.jsx("dd",{children:e.requestedAmount==null?"â":u(e.requestedAmount,e.currency)})]}),t.jsxs("div",{children:[t.jsx("dt",{className:"text-xs text-muted-foreground",children:r.disbursed}),t.jsx("dd",{children:u(e.disbursedAmount,e.currency)})]}),t.jsxs("div",{children:[t.jsx("dt",{className:"text-xs text-muted-foreground",children:r.outstanding}),t.jsx("dd",{children:u(H(e),e.currency)})]}),t.jsxs("div",{children:[t.jsx("dt",{className:"text-xs text-muted-foreground",children:r.nextCheck}),t.jsxs("dd",{children:[e.nextCheckOn?q.format(new Date(`${e.nextCheckOn}T00:00:00Z`)):"â",t.jsxs("span",{className:"block text-xs text-muted-foreground",children:[r.every," ",k.format(e.monitoringIntervalDays)," ",r.days]})]})]})]}),e.checkOverdue&&t.jsx("p",{className:"mt-3 rounded-xl bg-destructive/10 px-3 py-2 text-xs text-destructive",children:r.overdue}),e.status==="under_review"&&t.jsxs("div",{className:"mt-4 space-y-3",children:[t.jsx("label",{className:"block text-xs text-muted-foreground",htmlFor:`amt-${e.id}`,children:r.amount}),t.jsx("input",{id:`amt-${e.id}`,inputMode:"decimal",value:E[e.id]??(e.requestedAmount??"").toString(),onChange:n=>M(o=>({...o,[e.id]:n.target.value})),className:"h-11 w-full rounded-xl border border-border bg-background px-3 text-sm"}),t.jsx("label",{className:"block text-xs text-muted-foreground",htmlFor:`note-${e.id}`,children:r.note}),t.jsx("textarea",{id:`note-${e.id}`,rows:2,value:L[e.id]??"",onChange:n=>Z(o=>({...o,[e.id]:n.target.value})),className:"w-full rounded-xl border border-border bg-background p-3 text-sm"}),t.jsxs("div",{className:"flex flex-wrap gap-2",children:[t.jsx("button",{type:"button",disabled:m===e.id,onClick:()=>void C(e,"approved"),className:"h-11 rounded-xl bg-primary px-4 text-sm font-semibold text-primary-foreground disabled:opacity-60",children:m===e.id?r.saving:r.approve}),t.jsx("button",{type:"button",disabled:m===e.id,onClick:()=>void C(e,"declined"),className:"h-11 rounded-xl border border-border px-4 text-sm font-medium disabled:opacity-60",children:r.decline})]})]}),e.status==="approved"&&t.jsx("button",{type:"button",disabled:m===e.id,onClick:()=>void C(e,"closed"),className:"mt-4 h-11 rounded-xl border border-border px-4 text-sm font-medium disabled:opacity-60",children:m===e.id?r.saving:r.close}),e.decisionNote&&t.jsx("p",{className:"mt-3 text-xs text-muted-foreground",children:e.decisionNote})]},e.id))}),t.jsx("p",{className:"mt-6 text-xs text-muted-foreground",children:r.governance})]})]})}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.