import{t as e}from"./subMonths-DwZ45mAW.js";import{An as t,Au as n,Bi as r,Ci as i,Dn as a,Eu as o,Fi as s,Fl as c,Ft as l,G as u,Ga as d,H as f,I as p,Iu as m,Ju as h,K as g,Mi as _,Nl as v,On as y,Pd as b,Pi as x,Ri as S,Ta as C,Tn as w,U as T,V as E,W as D,Zn as O,bi as k,dn as A,jn as j,kn as M,nc as N,oc as P,pn as F,ql as I,rc as L,rr as R,tr as z,wd as B,wn as V,xi as H,zd as U,zi as W}from"./index-Q5R6-AeA.js";import{r as G,t as K}from"./use-issue-monthly-documents-BJt-VmQg.js";var q=U(b(),1),J=o();function Y({issueMonth:e,clients:n,onAdd:i}){let[o,c]=(0,q.useState)(!1),[l,u]=(0,q.useState)(``),[{data:d},f]=v({query:N,variables:{issueMonth:e,clientId:l},pause:!o}),m=(0,q.useCallback)(e=>{u(e),f()},[f,u]);return(0,q.useEffect)(()=>{l!==``&&d&&(i(p(O(P,d.clientMonthlyChargeDraft))),c(!1))},[l,d,i]),(0,J.jsxs)(x,{open:o,onOpenChange:c,children:[(0,J.jsx)(r,{asChild:!0,children:(0,J.jsx)(I,{variant:`outline`,size:`icon`,children:(0,J.jsx)(h,{})})}),(0,J.jsxs)(s,{className:`w-[400px] max-w-screen-md`,children:[(0,J.jsx)(S,{children:(0,J.jsx)(W,{children:`Select document recipient`})}),(0,J.jsxs)(a,{onValueChange:m,children:[(0,J.jsx)(t,{children:(0,J.jsx)(j,{placeholder:`Recipient`})}),(0,J.jsx)(y,{children:n.map(e=>(0,J.jsx)(M,{value:e.id,children:e.name},e.id))})]})]})]})}var X=({drafts:t})=>{let[r,a]=(0,q.useState)(t.map(e=>p(O(P,e)))),[o,s]=(0,q.useState)(c(e(new Date,1),`yyyy-MM-dd`)),[{data:h},y]=v({query:L,variables:{issueMonth:o},pause:!0});(0,q.useEffect)(()=>{h?.periodicalDocumentDrafts&&a(h.periodicalDocumentDrafts.map(e=>p(O(P,e))))},[h,a]),(0,q.useEffect)(()=>{y()},[o,y]);let b=w({values:{generateDocumentsInfo:r}}),{issueDocuments:x}=K(),[{data:S}]=v({query:C}),{fields:j,append:M,remove:N,update:R}=V({control:b.control,name:`generateDocumentsInfo`}),z=b.watch(`generateDocumentsInfo`),U=(0,q.useMemo)(()=>j.map((e,t)=>({...e,...z[t],id:e.client?.id})),[j,z]),W=(0,q.useCallback)(e=>{x(e)},[x]),X=(0,q.useMemo)(()=>S?.allOpenContracts.filter(e=>e.billingCycle===d.Monthly&&!U.some(t=>t.client?.id===e.client.id)),[U,S]);return(0,J.jsx)(`div`,{className:`p-2`,children:(0,J.jsx)(F,{...b,children:(0,J.jsxs)(`form`,{onSubmit:b.handleSubmit(W),children:[(0,J.jsxs)(`div`,{className:`flex gap-2 items-center mb-4`,children:[(0,J.jsx)(_,{children:`Issue Month:`}),(0,J.jsx)(A,{value:new Date(o),onChange:e=>{s(c(new Date(e.getFullYear(),e.getMonth(),15),`yyyy-MM-dd`))},popoverProps:{withinPortal:!0}})]}),(0,J.jsxs)(E,{children:[(0,J.jsx)(u,{children:(0,J.jsxs)(g,{children:[(0,J.jsx)(D,{children:`Name`}),(0,J.jsx)(D,{children:`Document`}),(0,J.jsx)(D,{children:`Amount`}),(0,J.jsx)(D,{children:`Remark`}),(0,J.jsx)(D,{children:`Recipients`}),(0,J.jsx)(D,{})]})}),(0,J.jsx)(f,{children:U.map(({id:e,...t},r)=>(0,J.jsxs)(g,{children:[(0,J.jsx)(T,{children:(0,J.jsx)(B,{to:e?n.BUSINESSES.DETAIL(e):n.BUSINESSES.ALL,target:`_blank`,rel:`noreferrer`,onClick:e=>e.stopPropagation(),className:`inline-flex items-center font-semibold`,children:t.client?.name})}),(0,J.jsx)(T,{children:t.type}),(0,J.jsxs)(T,{children:[t.income?.[0]?.price,` `,t.income?.[0]?.currency]}),(0,J.jsx)(T,{children:(0,J.jsx)(`span`,{className:`text-sm text-gray-500 whitespace-normal max-w-50`,children:t.remarks})}),(0,J.jsx)(T,{children:(0,J.jsx)(`div`,{className:`flex flex-col`,children:t.client?.emails?.map(e=>(0,J.jsx)(`span`,{className:`text-sm text-gray-500`,children:e},e))})}),(0,J.jsx)(T,{children:(0,J.jsxs)(`div`,{className:`flex flex-col gap-2`,children:[(0,J.jsxs)(k,{children:[(0,J.jsx)(i,{asChild:!0,type:`button`,children:(0,J.jsx)(I,{className:`size-7.5`,variant:`secondary`,onClick:()=>N(r),children:(0,J.jsx)(m,{size:16})})}),(0,J.jsx)(H,{children:(0,J.jsx)(`p`,{children:`Remove document`})})]}),(0,J.jsx)(G,{draft:t,onApprove:e=>{b.setValue(`generateDocumentsInfo.${r}`,e,{shouldDirty:!0,shouldTouch:!0,shouldValidate:!0}),R(r,{...e})}})]})})]},e))})]}),(0,J.jsxs)(`div`,{className:`flex justify-between items-center mt-4`,children:[(0,J.jsx)(Y,{clients:X?.map(e=>({id:e.client.originalBusiness.id,name:e.client.originalBusiness.name}))??[],onAdd:M,issueMonth:o}),(0,J.jsx)(l,{onConfirm:b.handleSubmit(W),title:`Are you sure you want to issue ${U.length} documents?`,children:(0,J.jsx)(I,{children:`Issue`})})]})]})})})},Z=()=>{let{setFiltersContext:t}=(0,q.useContext)(z),[{data:n,fetching:r}]=v({query:L,variables:{issueMonth:c(e(new Date,1),`yyyy-MM-dd`)}});return(0,q.useEffect)(()=>{n?.periodicalDocumentDrafts||t(null)},[n,t]),r?(0,J.jsx)(R,{}):(0,J.jsx)(X,{drafts:n?.periodicalDocumentDrafts??[]})};export{Z as IssueDocuments};