import{e as L,j as e,a as q,r as u,C as z,f as K,W as Y,g as J,h as U,L as H,d as o}from"./index-D4ZMoJFe.js";import{S as j}from"./SkeletonBlock-BzjAsBLJ.js";import{E as G}from"./ErrorStateCard-Cx3jyhWy.js";import{T as g}from"./TableSkeleton-DEtZA9xr.js";import{f as V}from"./formatters-jmx2pV9o.js";import{R as W}from"./refresh-ccw-DqBcVzHT.js";import{R as _,T as k,L as X,B as w,C as Q}from"./generateCategoricalChart-3rcGS8p6.js";import{B as Z}from"./BarChart-78W7x31T.js";import{X as ee,Y as se}from"./YAxis-DOtzoTA4.js";import{P as te,a as ae}from"./PieChart-C9p1G6Gr.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 re=L("ArrowDownRight",[["path",{d:"m7 7 10 10",key:"1fmybs"}],["path",{d:"M17 7v10H7",key:"6fjiku"}]]);/**
 * @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 le=L("ArrowUpRight",[["path",{d:"M7 7h10v10",key:"1tivn9"}],["path",{d:"M7 17 17 7",key:"1vkiza"}]]);function A({kind:t="bars"}){return e.jsxs("div",{className:"card-panel p-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(j,{className:"h-5 w-40"}),e.jsx(j,{className:"h-4 w-20"})]}),e.jsx("div",{className:"mt-6 h-[280px] rounded-2xl border border-border bg-surface2/60 p-6",children:t==="donut"?e.jsx("div",{className:"flex h-full items-center justify-center",children:e.jsxs("div",{className:"relative",children:[e.jsx(j,{className:"h-40 w-40 rounded-full"}),e.jsx("div",{className:"absolute inset-[28px] rounded-full bg-background"})]})}):e.jsx("div",{className:"flex h-full items-end gap-4",children:[40,62,35,76,54,68,30,58,72,44,61,52].map((l,d)=>e.jsx(j,{className:"w-full rounded-t-2xl",style:{height:`${l}%`}},`${t}-${d}`))})})]})}const M={total_actif:{label:"Total Actif",Icon:J,tone:"blue"},total_passif:{label:"Total Passif",Icon:Y,tone:"purple"},chiffre_affaires_ytd:{label:"Chiffre d'Affaires YTD",Icon:K,tone:"green"},resultat_net:{label:"Résultat Net",Icon:z,tone:"amber"}},ne={blue:{badge:"bg-blue-50 text-blue-700 ring-1 ring-blue-200",trend:"text-blue-700"},purple:{badge:"bg-violet-50 text-violet-700 ring-1 ring-violet-200",trend:"text-violet-700"},green:{badge:"bg-emerald-50 text-emerald-700 ring-1 ring-emerald-200",trend:"text-emerald-700"},amber:{badge:"bg-amber-50 text-amber-700 ring-1 ring-amber-200",trend:"text-amber-700"},red:{badge:"bg-rose-50 text-rose-700 ring-1 ring-rose-200",trend:"text-rose-700"}},ie={periods_not_closed:{title:"Périodes non clôturées",description:"Des périodes mensuelles sont encore ouvertes et attendent validation."},unmatched_entries:{title:"Écritures non lettrées",description:"Des lignes tiers restent ouvertes et nécessitent un lettrage."},unreconciled_bank_movements:{title:"Mouvements bancaires non rapprochés",description:"Le rapprochement bancaire n'est pas complet sur tous les flux."},assets_to_depreciate:{title:"Immobilisations à amortir",description:"Des actifs actifs n'ont pas encore de ligne d'amortissement sur ce mois."}},C={warning:"border-amber-200 bg-amber-50 text-amber-800",danger:"border-rose-200 bg-rose-50 text-rose-800",info:"border-blue-200 bg-blue-50 text-blue-800"},de={purchase:"#60A5FA",sale:"#10B981",cash:"#F59E0B",bank:"#8B5CF6",misc:"#64748B",payroll:"#F97316",assets:"#14B8A6"},ce={purchase:"Achats",sale:"Ventes",cash:"Caisse",bank:"Banque",misc:"OD",payroll:"Paie",assets:"Immobilisations"},S=["Jan","Fév","Mar","Avr","Mai","Juin","Juil","Août","Sep","Oct","Nov","Déc"],oe={draft:"Brouillon",validated:"Validée",cancelled:"Annulée"},xe=t=>{if(t==null)return"Aucune base de comparaison";const l=Number(t);return`${l>0?"+":""}${l.toFixed(1)}% vs période précédente`},me=t=>t==="down"?re:le,ue=t=>{if(!t)return"À l'instant";const d=new Date(t).getTime()-Date.now(),n=Math.round(d/(1e3*60)),c=new Intl.RelativeTimeFormat("fr",{numeric:"auto"});if(Math.abs(n)<60)return c.format(n,"minute");const m=Math.round(n/60);if(Math.abs(m)<24)return c.format(m,"hour");const r=Math.round(m/24);return c.format(r,"day")},p=({title:t,description:l})=>e.jsx("div",{className:"flex min-h-[160px] items-center justify-center rounded-2xl border border-dashed border-border bg-surface2 px-6 text-center",children:e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-text",children:t}),e.jsx("p",{className:"mt-2 text-sm text-textMuted",children:l})]})}),pe=({active:t,payload:l,label:d})=>{var c;if(!t||!(l!=null&&l.length))return null;const n=(c=l[0])==null?void 0:c.payload;return e.jsxs("div",{className:"rounded-xl border border-border bg-surface px-4 py-3 shadow-panel",children:[e.jsx("p",{className:"text-sm font-semibold text-text",children:S[(d||1)-1]}),e.jsxs("div",{className:"mt-3 space-y-2 text-sm",children:[e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsx("span",{className:"text-textMuted",children:"Débit"}),e.jsx("span",{className:"font-mono text-blue-700",children:n==null?void 0:n.formatted_debit})]}),e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsx("span",{className:"text-textMuted",children:"Crédit"}),e.jsx("span",{className:"font-mono text-emerald-700",children:n==null?void 0:n.formatted_credit})]})]})]})};function we(){var v,y;const{user:t,selectedCompanyId:l}=q(),[d,n]=u.useState(!0),[c,m]=u.useState(""),[r,B]=u.useState({kpis:[],monthlyChart:[],journalDistribution:[],recentEntries:[],alerts:[],treasuryBalances:[],rapidBalance:[],activity:[]}),h=l||(t==null?void 0:t.companyId)||((v=t==null?void 0:t.company)==null?void 0:v.id)||"",N=((y=t==null?void 0:t.fiscalYear)==null?void 0:y.id)||"",f=async()=>{var s,a;if(h){n(!0),m("");try{const i={company_id:h},x={company_id:h,fiscal_year_id:N||void 0},[T,D,E,I,F,$,P,O]=await Promise.all([o.get("/dashboard/kpis",{params:x}),o.get("/dashboard/monthly-chart",{params:x}),o.get("/dashboard/journal-distribution",{params:x}),o.get("/dashboard/recent-entries",{params:{...i,limit:10}}),o.get("/dashboard/alerts",{params:i}),o.get("/dashboard/treasury-balances",{params:i}),o.get("/dashboard/rapid-balance",{params:{...x,limit:10}}),o.get("/dashboard/activity",{params:{...i,limit:10}})]);B({kpis:T.data.items||[],monthlyChart:D.data.items||[],journalDistribution:E.data.items||[],recentEntries:I.data.items||[],alerts:F.data.items||[],treasuryBalances:$.data.items||[],rapidBalance:P.data.items||[],activity:O.data.items||[]})}catch(i){m(((a=(s=i==null?void 0:i.response)==null?void 0:s.data)==null?void 0:a.message)||"Impossible de charger les données du tableau de bord.")}finally{n(!1)}}};u.useEffect(()=>{f()},[h,N]);const R=u.useMemo(()=>r.kpis.map(s=>{const a=M[s.key]||M.total_actif,i=s.key==="resultat_net"&&Number(s.value)<0?"red":a.tone;return{...s,...a,tone:i}}),[r.kpis]),b=u.useMemo(()=>r.journalDistribution.filter(s=>Number(s.entry_count)>0).map(s=>({...s,name:ce[s.journal_type]||s.journal_type,value:Number(s.entry_count),color:de[s.journal_type]||"#94A3B8"})),[r.journalDistribution]);return e.jsx("div",{className:"-m-4 min-h-[calc(100vh-64px)] px-4 py-6 sm:-m-6 sm:px-6 lg:px-8",children:e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-col gap-3 lg:flex-row lg:items-end lg:justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.26em] text-accent",children:"Pilotage financier"}),e.jsx("h1",{className:"mt-3 text-3xl font-semibold tracking-tight text-text",children:"Tableau de bord comptable"}),e.jsx("p",{className:"mt-2 max-w-3xl text-sm leading-7 text-textMuted",children:"Vue consolidée des indicateurs SCF, de la trésorerie, de la clôture et de l'activité opérationnelle."})]}),e.jsxs("button",{type:"button",className:"btn-secondary inline-flex w-fit items-center gap-2 rounded-xl",onClick:f,disabled:d,children:[d?e.jsx(U,{size:16,className:"animate-spin"}):e.jsx(W,{size:16}),"Actualiser"]})]}),c?e.jsx(G,{title:"Impossible de charger le tableau de bord",message:c,onRetry:f,compact:!0}):null,d?e.jsxs(e.Fragment,{children:[e.jsx("section",{className:"grid gap-4 md:grid-cols-2 xl:grid-cols-4",children:Array.from({length:4}).map((s,a)=>e.jsx("article",{className:"card-panel rounded-2xl p-5",children:e.jsxs("div",{className:"animate-pulse",children:[e.jsx("div",{className:"h-11 w-11 rounded-2xl bg-slate-200/80"}),e.jsx("div",{className:"mt-6 h-8 w-40 rounded-xl bg-slate-200/70"}),e.jsx("div",{className:"mt-3 h-4 w-24 rounded bg-slate-200/60"}),e.jsx("div",{className:"mt-4 h-4 w-48 rounded bg-slate-200/50"})]})},`kpi-skeleton-${a}`))}),e.jsxs("section",{className:"grid gap-6 xl:grid-cols-[1.6fr_1fr]",children:[e.jsx(A,{}),e.jsx(A,{kind:"donut"})]}),e.jsxs("section",{className:"grid gap-6 xl:grid-cols-2",children:[e.jsx(g,{columns:6,rows:6}),e.jsx("div",{className:"card-panel p-6",children:e.jsx("div",{className:"space-y-3 animate-pulse",children:Array.from({length:4}).map((s,a)=>e.jsx("div",{className:"h-24 rounded-2xl border border-border bg-surface2"},`alert-skeleton-${a}`))})})]}),e.jsxs("section",{className:"grid gap-6 xl:grid-cols-3",children:[e.jsx(g,{columns:2,rows:5}),e.jsx(g,{columns:3,rows:6}),e.jsx("div",{className:"card-panel p-6",children:e.jsx("div",{className:"space-y-4 animate-pulse",children:Array.from({length:5}).map((s,a)=>e.jsx("div",{className:"h-16 rounded-2xl bg-surface2"},`activity-skeleton-${a}`))})})]})]}):null,d?null:e.jsxs(e.Fragment,{children:[e.jsx("section",{className:"grid gap-4 md:grid-cols-2 xl:grid-cols-4",children:R.map(s=>{const a=s.Icon,i=me(s.trend_direction),x=ne[s.tone];return e.jsxs("article",{className:"card-panel rounded-2xl p-5",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsx("div",{className:`rounded-2xl p-3 ${x.badge}`,children:e.jsx(a,{size:20})}),e.jsxs("div",{className:`inline-flex items-center gap-1 text-xs font-medium ${x.trend}`,children:[e.jsx(i,{size:14}),e.jsx("span",{children:xe(s.trend_percentage)})]})]}),e.jsx("p",{className:"mt-5 font-mono text-[1.9rem] font-semibold tracking-tight text-text",children:s.formatted_value}),e.jsx("p",{className:"mt-2 text-sm text-textMuted",children:s.label})]},s.key)})}),e.jsxs("section",{className:"grid gap-6 xl:grid-cols-[1.6fr_1fr]",children:[e.jsxs("div",{className:"card-panel rounded-2xl p-5",children:[e.jsxs("div",{className:"mb-5",children:[e.jsx("h2",{className:"text-lg font-semibold text-text",children:"Évolution mensuelle"}),e.jsx("p",{className:"mt-1 text-sm text-textMuted",children:"Débits et crédits validés par mois sur l'exercice comptable."})]}),r.monthlyChart.length?e.jsx("div",{className:"h-[320px]",children:e.jsx(_,{width:"100%",height:"100%",children:e.jsxs(Z,{data:r.monthlyChart,barGap:10,children:[e.jsx(ee,{dataKey:"month_index",tickFormatter:s=>S[s-1],tick:{fill:"#64748B",fontSize:12},axisLine:{stroke:"#D7E0EA"},tickLine:!1}),e.jsx(se,{tick:{fill:"#64748B",fontSize:12},axisLine:!1,tickLine:!1}),e.jsx(k,{content:e.jsx(pe,{})}),e.jsx(X,{wrapperStyle:{color:"#475569",paddingTop:18},formatter:s=>s==="debit"?"Débit":"Crédit"}),e.jsx(w,{dataKey:"debit",name:"debit",radius:[10,10,0,0],fill:"#3B82F6"}),e.jsx(w,{dataKey:"credit",name:"credit",radius:[10,10,0,0],fill:"#10B981"})]})})}):e.jsx(p,{title:"Aucun mouvement sur l'exercice",description:"Les écritures validées alimenteront automatiquement cette vue."})]}),e.jsxs("div",{className:"card-panel rounded-2xl p-5",children:[e.jsxs("div",{className:"mb-5",children:[e.jsx("h2",{className:"text-lg font-semibold text-text",children:"Répartition par journal"}),e.jsx("p",{className:"mt-1 text-sm text-textMuted",children:"Nombre d'écritures par type de journal."})]}),b.length?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"h-[260px]",children:e.jsx(_,{width:"100%",height:"100%",children:e.jsxs(te,{children:[e.jsx(ae,{data:b,dataKey:"value",nameKey:"name",innerRadius:62,outerRadius:92,paddingAngle:2,children:b.map(s=>e.jsx(Q,{fill:s.color},s.journal_type))}),e.jsx(k,{formatter:(s,a)=>[`${s} écritures`,a],contentStyle:{backgroundColor:"#FFFFFF",borderColor:"#D7E0EA",borderRadius:"16px",color:"#0F172A",boxShadow:"0 18px 48px rgba(15, 23, 42, 0.08)"}})]})})}),e.jsx("div",{className:"mt-4 grid gap-3 sm:grid-cols-2",children:b.map(s=>e.jsxs("div",{className:"flex items-center justify-between rounded-xl border border-border bg-surface2 px-4 py-3",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("span",{className:"h-3 w-3 rounded-full",style:{backgroundColor:s.color}}),e.jsx("span",{className:"text-sm text-text",children:s.name})]}),e.jsx("span",{className:"font-mono text-sm text-textMuted",children:s.value})]},s.journal_type))})]}):e.jsx(p,{title:"Aucune donnée journalisée",description:"Les journaux apparaîtront ici dès que des écritures seront saisies."})]})]}),e.jsxs("section",{className:"grid gap-6 xl:grid-cols-2",children:[e.jsxs("div",{className:"card-panel rounded-2xl p-5",children:[e.jsx("div",{className:"mb-5 flex items-center justify-between",children:e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold text-text",children:"Dernières écritures"}),e.jsx("p",{className:"mt-1 text-sm text-textMuted",children:"Les 10 dernières écritures validées remontées par l'API."})]})}),r.recentEntries.length?e.jsx("div",{className:"table-shell overflow-x-auto",children:e.jsxs("table",{className:"min-w-full border-collapse",children:[e.jsx("thead",{className:"table-head",children:e.jsxs("tr",{className:"border-b border-border text-left text-xs uppercase tracking-[0.18em] text-textMuted",children:[e.jsx("th",{className:"px-3 py-3",children:"Date"}),e.jsx("th",{className:"px-3 py-3",children:"N° Pièce"}),e.jsx("th",{className:"px-3 py-3",children:"Journal"}),e.jsx("th",{className:"px-3 py-3",children:"Libellé"}),e.jsx("th",{className:"px-3 py-3 text-right",children:"Montant"}),e.jsx("th",{className:"px-3 py-3",children:"Statut"})]})}),e.jsx("tbody",{children:r.recentEntries.map(s=>{const a=s.status==="validated"?"border-emerald-200 bg-emerald-50 text-emerald-800":s.status==="cancelled"?"border-rose-200 bg-rose-50 text-rose-800":"border-amber-200 bg-amber-50 text-amber-800";return e.jsxs("tr",{className:"table-row border-b border-border text-sm",children:[e.jsx("td",{className:"table-cell text-textMuted",children:V(s.entry_date)}),e.jsx("td",{className:"table-cell font-mono text-text",children:s.piece_number}),e.jsxs("td",{className:"table-cell text-text",children:[s.journal_code," - ",s.journal_label]}),e.jsx("td",{className:"table-cell",children:s.label}),e.jsx("td",{className:"table-cell text-right font-mono text-text",children:s.formatted_amount}),e.jsx("td",{className:"table-cell",children:e.jsx("span",{className:`inline-flex rounded-full border px-2.5 py-1 text-xs font-semibold uppercase tracking-[0.16em] ${a}`,children:oe[s.status]||s.status})})]},s.id)})})]})}):e.jsx(p,{title:"Aucune écriture validée",description:"Les écritures validées s'afficheront ici dès leur publication."})]}),e.jsxs("div",{className:"card-panel rounded-2xl p-5",children:[e.jsxs("div",{className:"mb-5",children:[e.jsx("h2",{className:"text-lg font-semibold text-text",children:"Alertes & rappels"}),e.jsx("p",{className:"mt-1 text-sm text-textMuted",children:"Sujets opérationnels à traiter en priorité."})]}),e.jsx("div",{className:"space-y-3",children:r.alerts.map(s=>{const a=ie[s.key];return e.jsx(H,{to:s.route,className:`block rounded-2xl border px-4 py-4 transition hover:translate-y-[-1px] hover:border-accent/40 ${C[s.severity]||C.info}`,children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-semibold",children:(a==null?void 0:a.title)||s.key}),e.jsx("p",{className:"mt-2 text-sm leading-6 text-current/80",children:a==null?void 0:a.description})]}),e.jsx("div",{className:"rounded-xl bg-white/60 px-3 py-2 font-mono text-xl font-semibold",children:s.count})]})},s.key)})})]})]}),e.jsxs("section",{className:"grid gap-6 xl:grid-cols-3",children:[e.jsxs("div",{className:"card-panel rounded-2xl p-5",children:[e.jsxs("div",{className:"mb-5",children:[e.jsx("h2",{className:"text-lg font-semibold text-text",children:"Solde Trésorerie"}),e.jsx("p",{className:"mt-1 text-sm text-textMuted",children:"Soldes courants de caisse et de banque."})]}),e.jsx("div",{className:"space-y-3",children:r.treasuryBalances.length?r.treasuryBalances.map(s=>e.jsxs("div",{className:"flex items-center justify-between rounded-xl border border-border bg-surface2 px-4 py-3",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-text",children:s.label}),e.jsx("p",{className:"mt-1 text-xs uppercase tracking-[0.18em] text-textMuted",children:s.source_type==="bank"?"Banque":"Caisse"})]}),e.jsx("p",{className:"font-mono text-sm font-semibold text-text",children:s.formatted_balance})]},`${s.source_type}-${s.id}`)):e.jsx(p,{title:"Aucun compte de trésorerie",description:"Ajoutez des banques ou des caisses pour suivre les soldes."})})]}),e.jsxs("div",{className:"card-panel rounded-2xl p-5",children:[e.jsxs("div",{className:"mb-5",children:[e.jsx("h2",{className:"text-lg font-semibold text-text",children:"Balance Rapide"}),e.jsx("p",{className:"mt-1 text-sm text-textMuted",children:"Top 10 des comptes par volume de mouvements."})]}),r.rapidBalance.length?e.jsx("div",{className:"table-shell",children:e.jsxs("table",{className:"min-w-full",children:[e.jsx("thead",{className:"table-head text-left text-xs uppercase tracking-[0.18em] text-textMuted",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-3 py-3",children:"Code"}),e.jsx("th",{className:"px-3 py-3",children:"Libellé"}),e.jsx("th",{className:"px-3 py-3 text-right",children:"Solde"})]})}),e.jsx("tbody",{children:r.rapidBalance.map(s=>e.jsxs("tr",{className:"table-row border-t border-border text-sm",children:[e.jsx("td",{className:"table-cell font-mono text-textMuted",children:s.code}),e.jsx("td",{className:"table-cell",children:s.label}),e.jsx("td",{className:"table-cell text-right font-mono text-text",children:s.formatted_balance})]},s.id))})]})}):e.jsx(p,{title:"Aucune balance disponible",description:"Les comptes les plus actifs apparaîtront ici après validation."})]}),e.jsxs("div",{className:"card-panel rounded-2xl p-5",children:[e.jsxs("div",{className:"mb-5",children:[e.jsx("h2",{className:"text-lg font-semibold text-text",children:"Activité récente"}),e.jsx("p",{className:"mt-1 text-sm text-textMuted",children:"Historique synthétique des dernières actions tracées dans l'audit."})]}),r.activity.length?e.jsx("div",{className:"space-y-4",children:r.activity.map(s=>e.jsxs("div",{className:"relative pl-5",children:[e.jsx("div",{className:"absolute left-0 top-1.5 h-2.5 w-2.5 rounded-full bg-blue-500"}),e.jsxs("p",{className:"text-sm font-medium text-text",children:[s.user_name," · ",s.action]}),e.jsx("p",{className:"mt-1 text-sm text-textMuted",children:s.entity_type}),e.jsx("p",{className:"mt-2 text-xs font-mono text-textMuted",children:ue(s.created_at)})]},s.id))}):e.jsx(p,{title:"Aucune activité journalisée",description:"Le journal d'audit alimentera automatiquement cette timeline."})]})]})]})]})})}export{we as default};
