import{a as P,r as n,j as t,J as A,d as F}from"./index-D4ZMoJFe.js";import{E as M}from"./EmptyStateCard-CTb0IPeU.js";import{E as G}from"./ErrorStateCard-Cx3jyhWy.js";import{P as S}from"./PaginationBar-VjpdrgpZ.js";import{T as D}from"./TableSkeleton-DEtZA9xr.js";import{a as l,b as p}from"./formatters-jmx2pV9o.js";import{F as T}from"./FiscalIfuSubnav-DHWBwGaH.js";import{c as m,e as $,d as I}from"./fiscalG50EngineUtils-x5GyB6aR.js";import{F as k}from"./file-down-CSh4yUNI.js";import{P as L}from"./pencil-line-Nlny-B3g.js";import"./refresh-ccw-DqBcVzHT.js";import"./SkeletonBlock-BzjAsBLJ.js";const h=50;function V(){var N;const{selectedCompanyId:g,user:r}=P(),c=g||((N=r==null?void 0:r.company)==null?void 0:N.id)||(r==null?void 0:r.companyId)||"",[_,i]=n.useState(!0),[u,j]=n.useState(""),[o,v]=n.useState({stats:{},items:[]}),[d,f]=n.useState({periodFrom:"",periodTo:""}),[x,y]=n.useState(1),b=async()=>{var s,a;if(!c){i(!1);return}i(!0),j("");try{const e=await F.get("/fiscal/g50-engine/corrections",{params:{company_id:c,period_from:d.periodFrom||void 0,period_to:d.periodTo||void 0}});v({stats:e.data.stats||{},items:e.data.items||[]})}catch(e){j(((a=(s=e==null?void 0:e.response)==null?void 0:s.data)==null?void 0:a.message)||"Impossible de charger le journal des corrections G50.")}finally{i(!1)}};n.useEffect(()=>{b()},[c,d.periodFrom,d.periodTo]);const w=n.useMemo(()=>{const s=(x-1)*h;return(o.items||[]).slice(s,s+h)},[o.items,x]);n.useEffect(()=>{y(1)},[o.items.length]);const C=()=>{const s=[["Période G50","Code","Libellé","Avant","Après","Delta","Raison","Justification","Utilisateur","Date"],...(o.items||[]).map(e=>[e.period_label,e.code,e.label,l(e.amount_before),l(e.amount_after),l(e.delta),m[e.reason_type]||e.reason_type,e.justification,e.adjusted_by_name||"",p(e.adjusted_at)])],a=$(s);I(new Blob([a],{type:"text/csv;charset=utf-8;"}),"g50-corrections.csv")},E=()=>{const s=`
      <html>
        <head>
          <meta charset="utf-8" />
          <title>Journal des corrections G50</title>
          <style>
            body { font-family: Arial, sans-serif; padding: 24px; color: #0f172a; }
            h1 { font-size: 22px; margin-bottom: 16px; }
            table { width: 100%; border-collapse: collapse; font-size: 12px; }
            th, td { border: 1px solid #cbd5e1; padding: 8px; text-align: left; }
            th { background: #e2e8f0; }
          </style>
        </head>
        <body>
          <h1>Journal des corrections manuelles G50</h1>
          <table>
            <thead>
              <tr>
                <th>Période</th>
                <th>Code</th>
                <th>Libellé</th>
                <th>Avant</th>
                <th>Après</th>
                <th>Delta</th>
                <th>Raison</th>
                <th>Utilisateur</th>
                <th>Date</th>
              </tr>
            </thead>
            <tbody>
              ${(o.items||[]).map(e=>`
                    <tr>
                      <td>${e.period_label}</td>
                      <td>${e.code}</td>
                      <td>${e.label}</td>
                      <td>${l(e.amount_before)}</td>
                      <td>${l(e.amount_after)}</td>
                      <td>${l(e.delta)}</td>
                      <td>${m[e.reason_type]||e.reason_type}</td>
                      <td>${e.adjusted_by_name||""}</td>
                      <td>${p(e.adjusted_at)}</td>
                    </tr>`).join("")}
            </tbody>
          </table>
        </body>
      </html>
    `,a=window.open("","_blank","width=1200,height=800");a&&(a.document.write(s),a.document.close(),a.focus(),a.print())};return t.jsxs("div",{className:"space-y-6",children:[t.jsx(T,{}),t.jsx("section",{className:"card-panel p-6 sm:p-8",children:t.jsxs("div",{className:"flex flex-col gap-4 xl:flex-row xl:items-start xl:justify-between",children:[t.jsxs("div",{children:[t.jsx("p",{className:"text-xs uppercase tracking-[0.2em] text-accent",children:"Moteur G50"}),t.jsx("h1",{className:"mt-3 text-3xl font-semibold text-text",children:"Journal des Corrections Manuelles"}),t.jsx("p",{className:"mt-3 max-w-3xl text-sm leading-7 text-textMuted",children:"Historique centralisé de tous les ajustements manuels effectués sur les brouillons G50."})]}),t.jsxs("div",{className:"flex flex-wrap gap-3",children:[t.jsxs("button",{type:"button",className:"btn-secondary inline-flex items-center gap-2",onClick:E,children:[t.jsx(A,{size:16}),"PDF rapport des corrections"]}),t.jsxs("button",{type:"button",className:"btn-secondary inline-flex items-center gap-2",onClick:C,children:[t.jsx(k,{size:16}),"Excel"]})]})]})}),t.jsxs("section",{className:"grid gap-4 md:grid-cols-3",children:[t.jsxs("div",{className:"card-panel p-5",children:[t.jsx("p",{className:"text-sm text-textMuted",children:"Total corrections"}),t.jsx("p",{className:"mt-3 text-3xl font-semibold text-text",children:Number(o.stats.total_corrections||0)})]}),t.jsxs("div",{className:"card-panel p-5",children:[t.jsx("p",{className:"text-sm text-textMuted",children:"Montant total ajusté"}),t.jsx("p",{className:"mt-3 font-mono text-3xl font-semibold text-gold",children:l(o.stats.total_adjusted_amount||0)})]}),t.jsxs("div",{className:"card-panel p-5",children:[t.jsx("p",{className:"text-sm text-textMuted",children:"Nb G50 avec corrections"}),t.jsx("p",{className:"mt-3 text-3xl font-semibold text-text",children:Number(o.stats.g50_count||0)})]})]}),t.jsx("section",{className:"card-panel p-5",children:t.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[t.jsxs("label",{className:"space-y-2",children:[t.jsx("span",{className:"text-sm font-medium text-text",children:"Période du"}),t.jsx("input",{type:"date",className:"input-base",value:d.periodFrom,onChange:s=>f(a=>({...a,periodFrom:s.target.value}))})]}),t.jsxs("label",{className:"space-y-2",children:[t.jsx("span",{className:"text-sm font-medium text-text",children:"Période au"}),t.jsx("input",{type:"date",className:"input-base",value:d.periodTo,onChange:s=>f(a=>({...a,periodTo:s.target.value}))})]})]})}),_?t.jsx(D,{columns:8,rows:10}):u?t.jsx(G,{message:u,onRetry:b}):(o.items||[]).length?t.jsxs(t.Fragment,{children:[t.jsx("section",{className:"card-panel overflow-hidden",children:t.jsx("div",{className:"overflow-x-auto",children:t.jsxs("table",{className:"min-w-full text-sm",children:[t.jsx("thead",{className:"bg-surface2 text-left text-xs uppercase tracking-[0.16em] text-textMuted",children:t.jsxs("tr",{children:[t.jsx("th",{className:"px-4 py-3",children:"Période G50"}),t.jsx("th",{className:"px-4 py-3",children:"Code"}),t.jsx("th",{className:"px-4 py-3",children:"Libellé"}),t.jsx("th",{className:"px-4 py-3 text-right",children:"Avant"}),t.jsx("th",{className:"px-4 py-3 text-right",children:"Après"}),t.jsx("th",{className:"px-4 py-3 text-right",children:"Δ"}),t.jsx("th",{className:"px-4 py-3",children:"Raison"}),t.jsx("th",{className:"px-4 py-3",children:"Justification"}),t.jsx("th",{className:"px-4 py-3",children:"Utilisateur"}),t.jsx("th",{className:"px-4 py-3",children:"Date"})]})}),t.jsx("tbody",{className:"divide-y divide-border",children:w.map(s=>t.jsxs("tr",{children:[t.jsx("td",{className:"px-4 py-3 text-text",children:s.period_label}),t.jsx("td",{className:"px-4 py-3 font-mono text-text",children:s.code}),t.jsx("td",{className:"px-4 py-3 text-text",children:s.label}),t.jsx("td",{className:"px-4 py-3 text-right font-mono text-textMuted",children:l(s.amount_before)}),t.jsx("td",{className:"px-4 py-3 text-right font-mono text-text",children:l(s.amount_after)}),t.jsx("td",{className:"px-4 py-3 text-right font-mono text-gold",children:l(s.delta)}),t.jsx("td",{className:"px-4 py-3 text-textMuted",children:m[s.reason_type]||s.reason_type}),t.jsx("td",{className:"px-4 py-3 text-textMuted",children:s.justification}),t.jsx("td",{className:"px-4 py-3 text-textMuted",children:s.adjusted_by_name||"—"}),t.jsx("td",{className:"px-4 py-3 text-textMuted",children:p(s.adjusted_at)})]},s.id))})]})})}),t.jsx(S,{page:x,pageSize:h,total:(o.items||[]).length,onPageChange:y})]}):t.jsx(M,{icon:L,title:"Aucune correction manuelle",description:"Aucun ajustement G50 ne correspond aux filtres sélectionnés."})]})}export{V as default};
