import{B as e,Bi as t,Ci as n,Eu as r,Fi as i,Gl as a,Hl as o,Kl as s,Ml as c,Pd as l,Pi as u,R as d,Uu as f,Vl as p,Ys as m,ad as h,bi as g,ed as _,in as v,od as y,ql as b,rt as x,tt as S,vd as C,vi as w,xi as T,yi as E,zd as D}from"./index-Q5R6-AeA.js";var O=D(l(),1),k=r();function A({onApprove:e,draft:r}){let[a,o]=(0,O.useState)(!1);return(0,k.jsxs)(u,{open:a,onOpenChange:o,children:[(0,k.jsx)(t,{asChild:!0,type:`button`,children:(0,k.jsxs)(g,{children:[(0,k.jsx)(n,{asChild:!0,type:`button`,children:(0,k.jsx)(b,{className:`size-7.5`,variant:`secondary`,onClick:()=>o(e=>!e),type:`button`,children:(0,k.jsx)(f,{className:`size-5`})})}),(0,k.jsx)(T,{children:(0,k.jsx)(`p`,{children:`Edit document`})})]})}),(0,k.jsx)(i,{className:`w-7xl sm:max-w-[95%] max-h-[90vh] overflow-y-auto`,children:(0,k.jsx)(`div`,{className:`min-h-screen bg-gray-50 p-4`,children:(0,k.jsxs)(`div`,{className:`max-w-7xl mx-auto`,children:[(0,k.jsxs)(`div`,{className:`mb-6`,children:[(0,k.jsx)(`h1`,{className:`text-3xl font-bold text-gray-900`,children:`Edit Document`}),(0,k.jsx)(`p`,{className:`text-gray-600 mt-2`,children:`Modify and preview your accounting document before issuing`})]}),(0,k.jsx)(j,{onApprove:t=>{e(t),o(!1)},draft:r})]})})})]})}function j({onApprove:t,draft:n}){let[r,i]=(0,O.useState)(!1),[c,l]=(0,O.useState)(null),{previewDocument:u,fetching:f}=x(),[m,g]=(0,O.useState)(n),w=(0,O.useCallback)((e,t)=>{g(n=>({...n,[e]:t}))},[]);(0,O.useEffect)(()=>{i(!1)},[m]);let T=(0,O.useCallback)(async()=>{try{let e=await u({input:m});if(!e)throw Error(`No preview data returned`);l(e),i(!0)}catch(e){console.error(`Failed to generate preview:`,e)}},[m,u]);(0,O.useEffect)(()=>{c||T()},[c,T]);let E=m.income?.reduce((e,t)=>{let n=t.price*t.quantity,r=n*((t.vatRate||0)/100);return e+n+r},0)||0;return(0,k.jsxs)(`div`,{className:`grid lg:grid-cols-2 gap-6`,children:[(0,k.jsxs)(`div`,{className:`space-y-6`,children:[(0,k.jsx)(v,{formData:m,updateFormData:w}),(0,k.jsx)(p,{children:(0,k.jsxs)(o,{className:`pt-6`,children:[(0,k.jsxs)(`div`,{className:`flex gap-3`,children:[(0,k.jsx)(b,{onClick:T,disabled:f||r,variant:`outline`,className:`flex-1 bg-transparent`,children:f?(0,k.jsxs)(k.Fragment,{children:[(0,k.jsx)(_,{className:`w-4 h-4 mr-2 animate-spin`}),`Generating...`]}):(0,k.jsxs)(k.Fragment,{children:[(0,k.jsx)(y,{className:`w-4 h-4 mr-2`}),`Preview`]})}),(0,k.jsxs)(b,{onClick:()=>t(m),className:`flex-1`,children:[(0,k.jsx)(C,{className:`w-4 h-4 mr-2`}),`Accept Changes`]})]}),E>0&&(0,k.jsx)(`div`,{className:`mt-4 p-3 bg-blue-50 rounded-lg`,children:(0,k.jsxs)(`div`,{className:`flex justify-between items-center font-medium text-blue-900`,children:[(0,k.jsx)(`span`,{children:`Total Document Amount:`}),(0,k.jsxs)(`span`,{children:[E.toFixed(2),` `,m.currency]})]})})]})})]}),(0,k.jsxs)(`div`,{className:`space-y-6`,children:[(0,k.jsxs)(p,{className:`h-fit`,children:[(0,k.jsx)(a,{children:(0,k.jsx)(s,{children:`Document Preview`})}),(0,k.jsx)(o,{children:(0,k.jsx)(`div`,{className:`border-2 border-dashed border-gray-200 rounded-lg min-h-[600px] flex items-center justify-center bg-gray-50`,children:f?(0,k.jsxs)(`div`,{className:`text-center`,children:[(0,k.jsx)(_,{className:`w-8 h-8 animate-spin mx-auto mb-4 text-gray-400`}),(0,k.jsx)(`p`,{className:`text-gray-500`,children:`Generating document preview...`})]}):c?(0,k.jsx)(`div`,{className:`w-full`,children:(0,k.jsx)(`div`,{className:`bg-white shadow-lg rounded-lg overflow-hidden`,children:(0,k.jsx)(`div`,{className:`aspect-[8.5/11] bg-white border`,children:(0,k.jsx)(`div`,{className:`h-full flex items-center justify-center`,children:(0,k.jsxs)(`div`,{className:`text-center p-6`,children:[(0,k.jsx)(S,{src:c}),!r&&(0,k.jsx)(`p`,{className:`text-xs text-amber-600 mt-3 bg-amber-50 px-2 py-1 rounded`,children:`Preview may be outdated`})]})})})})}):(0,k.jsxs)(`div`,{className:`text-center`,children:[(0,k.jsx)(h,{className:`w-16 h-16 mx-auto mb-4 text-gray-300`}),(0,k.jsx)(`p`,{className:`text-gray-500 mb-2`,children:`No preview available`}),(0,k.jsx)(`p`,{className:`text-sm text-gray-400`,children:`Click Preview to generate document`})]})})})]}),m.client&&(0,k.jsx)(e,{businessId:m.client?.id,linkedDocumentIds:m.linkedDocumentIds??[]}),(0,k.jsx)(d,{documentType:m.type})]})]})}var M=`issue-monthly-documents`,N=()=>{let[{fetching:e},t]=c(m);return{fetching:e,issueDocuments:(0,O.useCallback)(async e=>{let n=`Error issuing monthly documents`,r=M;E.loading(`Issuing documents`,{id:r});try{let i=w(await t(e),n,r);if(i?.issueGreenInvoiceDocuments?.success){E.success(`Success`,{id:r,description:`Documents issued successfully`});return}i?.issueGreenInvoiceDocuments?.errors&&console.error(i.issueGreenInvoiceDocuments.errors),E.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}catch(e){console.error(`${n}: ${e}`),E.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}};export{j as n,A as r,N as t};