import{e as k,t as S,u as z,a as A,i as E,r as x,j as e,L as p,d as _}from"./index-D4ZMoJFe.js";import{f as w,a as M}from"./formatters-jmx2pV9o.js";import{g as I,h as R}from"./entryUtils-BsW97-RW.js";import{A as T}from"./arrow-left-DvOngpmh.js";import{P as q}from"./pencil-UDXK7qaT.js";import{P as C}from"./paperclip-CGHUH2oL.js";/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const F=k("Copy",[["rect",{width:"14",height:"14",x:"8",y:"8",rx:"2",ry:"2",key:"17jyea"}],["path",{d:"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2",key:"zix9uf"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const H=k("CornerDownLeft",[["polyline",{points:"9 10 4 15 9 20",key:"r3jprv"}],["path",{d:"M20 4v7a4 4 0 0 1-4 4H4",key:"6o5b7l"}]]),l=({label:i,value:m,mono:u=!1})=>e.jsxs("div",{className:"rounded-2xl border border-border bg-surface2 p-4",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.18em] text-textMuted",children:i}),e.jsx("p",{className:`mt-2 text-sm text-text ${u?"font-mono":""}`,children:m||"—"})]});function K(){var g,y,v;const{id:i}=S(),m=z(),{selectedCompanyId:u,user:n}=A(),{pushToast:h}=E(),[L,b]=x.useState(!0),[D,j]=x.useState(!1),[t,P]=x.useState(null),[f,N]=x.useState(""),o=u||((g=n==null?void 0:n.company)==null?void 0:g.id)||(n==null?void 0:n.companyId)||"";x.useEffect(()=>{(async()=>{var r,d;if(!(!o||!i)){b(!0),N("");try{const a=await _.get(`/entries/${i}`,{params:{company_id:o}});P(a.data.item||null)}catch(a){N(((d=(r=a==null?void 0:a.response)==null?void 0:r.data)==null?void 0:d.message)||"Impossible de charger le détail de l'écriture.")}finally{b(!1)}}})()},[o,i]);const $=async()=>{var s,r,d,a;if(t!=null&&t.can_reverse){j(!0);try{const c=await _.post(`/entries/${t.id}/reverse`,{company_id:o});h({title:"Contre-passation générée",description:((s=c.data.item)==null?void 0:s.piece_ref)||((r=c.data.item)==null?void 0:r.entry_number),tone:"success"}),m(`/entries/${c.data.item.id}`)}catch(c){h({title:"Contre-passation impossible",description:((a=(d=c==null?void 0:c.response)==null?void 0:d.data)==null?void 0:a.message)||"La contre-passation n'a pas pu être générée.",tone:"danger"})}finally{j(!1)}}};return L?e.jsx("section",{className:"card-panel p-8",children:e.jsx("p",{className:"text-sm text-textMuted",children:"Chargement de l'écriture..."})}):f||!t?e.jsx("section",{className:"card-panel p-8",children:e.jsx("p",{className:"text-sm text-danger",children:f||"Écriture introuvable."})}):e.jsxs("div",{className:"space-y-6",children:[e.jsx("section",{className:"card-panel p-6 sm:p-8",children:e.jsxs("div",{className:"flex flex-col gap-4 xl:flex-row xl:items-start xl:justify-between",children:[e.jsxs("div",{children:[e.jsxs(p,{to:"/entries",className:"inline-flex items-center gap-2 text-sm font-medium text-accent transition hover:text-accent/80",children:[e.jsx(T,{size:16}),"Retour aux écritures"]}),e.jsxs("div",{className:"mt-4 flex flex-wrap items-center gap-3",children:[e.jsx("h1",{className:"text-3xl font-semibold text-text",children:t.piece_ref||t.entry_number}),e.jsx("span",{className:`status-pill ${I(t.status)}`,children:t.status_label})]}),e.jsx("p",{className:"mt-3 max-w-3xl text-sm leading-7 text-textMuted",children:t.label})]}),e.jsxs("div",{className:"flex flex-wrap gap-3",children:[t.can_edit?e.jsxs(p,{to:`/entries/${t.id}/edit`,className:"btn-secondary inline-flex items-center gap-2",children:[e.jsx(q,{size:16}),"Modifier"]}):null,e.jsxs(p,{to:`/entries/new?duplicate=${t.id}`,className:"btn-secondary inline-flex items-center gap-2",children:[e.jsx(F,{size:16}),"Dupliquer"]}),t.can_reverse?e.jsxs("button",{type:"button",className:"btn-primary inline-flex items-center gap-2",onClick:$,disabled:D,children:[e.jsx(H,{size:16}),"Contre-passer"]}):null]})]})}),e.jsxs("section",{className:"grid gap-4 lg:grid-cols-4",children:[e.jsx(l,{label:"Journal",value:`${t.journal.code} · ${t.journal.label}`}),e.jsx(l,{label:"Date",value:w(t.entry_date)}),e.jsx(l,{label:"Période",value:((y=t.period)==null?void 0:y.label)||"—",mono:!0}),e.jsx(l,{label:"Exercice",value:((v=t.fiscal_year)==null?void 0:v.label)||"—"}),e.jsx(l,{label:"N° écriture",value:t.entry_number,mono:!0}),e.jsx(l,{label:"Créée par",value:t.created_by_name||"Système"}),e.jsx(l,{label:"Validée par",value:t.validated_by_name||"—"}),e.jsx(l,{label:"Pièce jointe",value:t.attachment_url?"Attachée":"Aucune"})]}),t.attachment_url?e.jsxs("section",{className:"card-panel p-6",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(C,{size:18,className:"text-accent"}),e.jsx("h2",{className:"text-xl font-semibold text-text",children:"Pièce justificative"})]}),t.attachment_url.startsWith("data:image/")?e.jsx("img",{src:t.attachment_url,alt:"Pièce justificative",className:"mt-5 max-h-72 rounded-2xl border border-border object-contain"}):e.jsxs("a",{href:t.attachment_url,target:"_blank",rel:"noreferrer",className:"mt-5 inline-flex items-center gap-2 rounded-xl border border-border bg-surface2 px-4 py-3 text-sm font-medium text-accent transition hover:border-accent/30 hover:bg-accent/10",children:[e.jsx(C,{size:16}),"Ouvrir la pièce jointe"]})]}):null,e.jsxs("section",{className:"card-panel p-6",children:[e.jsxs("div",{className:"flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-xl font-semibold text-text",children:"Lignes comptables"}),e.jsx("p",{className:"mt-2 text-sm text-textMuted",children:"Lecture seule pour les écritures validées ou annulées."})]}),e.jsxs("div",{className:"flex flex-wrap gap-3",children:[e.jsxs("div",{className:"rounded-2xl border border-border bg-surface2 px-4 py-3",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.18em] text-textMuted",children:"Total débit"}),e.jsx("p",{className:"mt-1 font-mono text-danger",children:t.formatted_total_debit})]}),e.jsxs("div",{className:"rounded-2xl border border-border bg-surface2 px-4 py-3",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.18em] text-textMuted",children:"Total crédit"}),e.jsx("p",{className:"mt-1 font-mono text-accent2",children:t.formatted_total_credit})]}),e.jsxs("div",{className:"rounded-2xl border border-border bg-surface2 px-4 py-3",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.18em] text-textMuted",children:"Solde"}),e.jsx("p",{className:`mt-1 font-mono ${t.is_balanced?"text-accent":"text-danger"}`,children:t.formatted_balance_difference})]})]})]}),e.jsx("div",{className:"mt-6 overflow-x-auto",children:e.jsxs("table",{className:"min-w-full",children:[e.jsx("thead",{className:"table-head",children:e.jsxs("tr",{children:[e.jsx("th",{className:"table-cell",children:"#"}),e.jsx("th",{className:"table-cell",children:"Compte"}),e.jsx("th",{className:"table-cell",children:"Auxiliaire"}),e.jsx("th",{className:"table-cell",children:"Libellé"}),e.jsx("th",{className:"table-cell amount-cell",children:"Débit"}),e.jsx("th",{className:"table-cell amount-cell",children:"Crédit"}),e.jsx("th",{className:"table-cell",children:"Échéance"})]})}),e.jsx("tbody",{children:t.lines.map((s,r)=>e.jsxs("tr",{className:"table-row border-b border-border",children:[e.jsx("td",{className:"table-cell font-mono text-textMuted",children:r+1}),e.jsx("td",{className:"table-cell",children:e.jsxs("div",{children:[e.jsx("p",{className:"font-mono text-sm font-semibold text-text",children:s.account_code}),e.jsx("p",{className:"text-sm text-textMuted",children:s.account_label})]})}),e.jsx("td",{className:"table-cell",children:s.auxiliary_ref_code?e.jsxs("div",{children:[e.jsx("p",{className:"font-mono text-sm font-semibold text-text",children:s.auxiliary_ref_code}),e.jsx("p",{className:"text-sm text-textMuted",children:s.auxiliary_name})]}):e.jsx("span",{className:"text-textMuted",children:"—"})}),e.jsx("td",{className:"table-cell",children:s.label}),e.jsx("td",{className:"table-cell amount-cell amount-debit",children:s.debit?M(s.debit):"—"}),e.jsx("td",{className:"table-cell amount-cell amount-credit",children:s.credit?M(s.credit):"—"}),e.jsx("td",{className:"table-cell",children:s.due_date?w(s.due_date):"—"})]},s.id))})]})})]}),e.jsxs("section",{className:"grid gap-6 xl:grid-cols-[1.2fr_0.8fr]",children:[e.jsxs("div",{className:"card-panel p-6",children:[e.jsx("h2",{className:"text-xl font-semibold text-text",children:"Historique de statut"}),e.jsx("div",{className:"mt-6 space-y-4",children:t.history.length?t.history.map(s=>e.jsxs("div",{className:"flex gap-4",children:[e.jsx("div",{className:"mt-1 h-3 w-3 rounded-full bg-accent"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-text",children:s.label}),e.jsxs("p",{className:"mt-1 text-sm text-textMuted",children:[s.actor_name," · ",R(s.occurred_at)]}),e.jsx("p",{className:"mt-1 text-xs text-textMuted",children:new Date(s.occurred_at).toLocaleString("fr-FR")})]})]},s.id)):e.jsx("p",{className:"text-sm text-textMuted",children:"Aucun événement d'historique disponible."})})]}),e.jsxs("div",{className:"card-panel p-6",children:[e.jsx("h2",{className:"text-xl font-semibold text-text",children:"Synthèse"}),e.jsxs("div",{className:"mt-6 space-y-4",children:[e.jsxs("div",{className:"rounded-2xl border border-border bg-surface2 p-4",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.18em] text-textMuted",children:"État d'équilibre"}),e.jsx("p",{className:`mt-2 text-base font-semibold ${t.is_balanced?"text-accent2":"text-danger"}`,children:t.is_balanced?"Écriture équilibrée":"Écriture déséquilibrée"})]}),e.jsxs("div",{className:"rounded-2xl border border-border bg-surface2 p-4",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.18em] text-textMuted",children:"Nombre de lignes"}),e.jsx("p",{className:"mt-2 font-mono text-text",children:t.line_count})]}),e.jsxs("div",{className:"rounded-2xl border border-border bg-surface2 p-4",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.18em] text-textMuted",children:"Dernière mise à jour"}),e.jsx("p",{className:"mt-2 text-sm text-text",children:new Date(t.updated_at).toLocaleString("fr-FR")})]})]})]})]})]})}export{K as default};
