import{u as S,ci as j,a as A,ce as F,p as h,ah as N,d as P,f4 as C,F as m,r as E,e as $,h as k,ak as w,j as i,C as M,c2 as c,aK as T,k as I,bS as V,cz as z}from"./bundle.CHKSNOJF.js";import{b as B}from"./payments-C1sjF6lG.js";import{u as R}from"./usePaymentPageSaveAction-BqBYiwW1.js";import{u as q}from"./useApplyInvoiceTableColumns-YKGzWfxI.js";import"./useInvoiceColumns-CGbyIb88.js";import"./InvoiceStatus-1S0ZEgN9.js";import"./invoice-status-C0SI5VYC.js";function J(){const[a]=S(),{id:p}=j(),l=A(),[v]=F(),{data:e}=B({id:p}),[u,y]=h.useState(),f=N(),r=P(),g=(n,t)=>{if(e){const o=e?.amount-e?.applied;let d=0;return t.map(_=>{d=d+Number(_.amount)}),Math.min(o-d,n)}return n},x=()=>{if(e){const n=e?.amount-e?.applied;let t=0;return s.values.invoices.map(o=>{t=t+Number(o.amount)}),n-t}return 0},s=C({enableReinitialize:!0,initialValues:{invoices:[]},onSubmit:n=>{m.processing(),y(void 0),E("PUT",$("/api/v1/payments/:id",{id:p}),n).then(t=>{m.success("updated_payment"),f(k("/payments/:id/edit",{id:t.data.data.id}))}).catch(t=>{t.response?.status===422&&(y(t.response.data),m.dismiss())}).finally(()=>{s.setSubmitting(!1),w(["payments","invoices","clients","credits"])})}}),b=q({payment:e,errors:u,formik:s,isApplyPage:!0});return h.useEffect(()=>{let n=0;s.values.invoices.map(t=>{n=n+Number(t.amount)})},[s.values.invoices]),R({onClick:()=>s.submitForm(),disabled:s.isSubmitting},[s.values,s.isSubmitting]),i.jsxs(M,{title:a("apply_payment"),className:"shadow-sm",style:{borderColor:l.$24},headerStyle:{borderColor:l.$20},children:[i.jsx(c,{leftSide:a("number"),children:e?.number}),e&&e.client&&i.jsxs(i.Fragment,{children:[i.jsx(c,{leftSide:a("amount"),children:r(e?.amount,e.client?.country_id,e.client?.settings.currency_id)}),i.jsx(c,{leftSide:a("applied"),children:r(e?.applied,e.client?.country_id,e.client?.settings.currency_id)}),i.jsxs(c,{leftSide:a("unapplied"),children:[r(e?.amount-e?.applied,e.client?.country_id,e.client?.settings.currency_id),s.values.invoices.length>=1&&`  - (${r(x(),e.client?.country_id,e.client?.settings.currency_id)} ${a("remaining")})`]})]}),i.jsxs(i.Fragment,{children:[e?.client_id?i.jsxs("div",{className:"flex flex-col px-4 sm:px-6 gap-y-2 mt-4",children:[i.jsx(T,{children:a("invoices")}),i.jsx(I,{resource:"invoice",endpoint:`/api/v1/invoices?include=client&payable=${e?.client_id}&per_page=100&sort=date|desc&per_page=1000`,columns:b,onSelectedResourcesChange:n=>{if(n.length>0){const t=[];n.forEach(o=>{t.push({_id:V(),amount:g(o.balance,t),credit_id:"",invoice_id:o.id,number:o.number})}),s.setFieldValue("invoices",t)}else s.setFieldValue("invoices",[])},withoutPagination:!0,withoutStatusFilter:!0,withoutAllBulkActions:!0,preSelected:v.get("invoice_id")?[v.get("invoice_id")]:void 0,emptyState:i.jsx("div",{className:"flex items-center justify-center py-2",children:i.jsx("span",{className:"text-sm",style:{color:l.$17},children:a("no_invoices_found")})})})]}):null,u?.errors.invoices&&i.jsx("div",{className:"py-2",children:i.jsx(z,{children:u.errors.invoices})})]})]})}export{J as default};
