1import{d4 as Q,ax as W,az as Y,dy as X,r as a,ej as Z,ek as O,el as ee,j as e,as as A,ap as x,bf as te,q as se,b9 as re,K as ae,ao as ne,bT as oe,bv as z,bw as K,by as L,ar as R,em as U,aI as f,b5 as I}from"./index-CpeGhXo5.js";import{P as le}from"./Panel-Cf_wAUzv.js";import{F as D}from"./FinanceFormField-BQlXTOvW.js";import{u as ie}from"./useCurrencyHelpers-D5hUvgaW.js";import"./currency-CACMzR5Q.js";function B(l){if(!l)return"";const[i,n]=l.split("-");return new Date(parseInt(i),parseInt(n)-1).toLocaleDateString("en-US",{month:"short",year:"numeric"})}function de({isOpen:l,onClose:i,projectId:n,entry:s,projectName:v}){const[y]=O(),[r,N]=a.useState(s?.remarks||""),[p,w]=a.useState(!1);return a.useEffect(()=>{l&&N(s?.remarks||"")},[s?.remarks,l]),a.useEffect(()=>{if(!l||!s)return;const b=setTimeout(async()=>{if(r!==(s?.remarks||"")){w(!0);try{await y({projectId:n,entryId:s.id,body:{month:s.month,revenue:s.revenue,cost:s.cost,remarks:r}}).unwrap()}catch{f.error("Failed to autosave remarks")}finally{w(!1)}}},1e3);return()=>clearTimeout(b)},[r,l,s,n,y]),s?e.jsx(z,{open:l,onOpenChange:b=>!b&&i(),children:e.jsxs(K,{className:"max-w-2xl p-0 overflow-hidden bg-white rounded-xl shadow-lg border-0 gap-0",children:[e.jsxs("div",{className:"px-5 py-4 border-b border-border relative flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"text-[10px] uppercase font-semibold text-muted-foreground tracking-wider mb-1",children:["PROJECT: ",v]}),e.jsxs(L,{className:"text-lg font-bold",children:["Remarks â ",B(s.month)]})]}),p&&e.jsxs("span",{className:"text-xs font-semibold text-muted-foreground animate-pulse flex items-center gap-1",children:[e.jsx(I,{className:"w-3.5 h-3.5"})," Autosaving..."]}),!p&&r!==(s?.remarks||"")&&e.jsxs("span",{className:"text-xs font-semibold text-muted-foreground flex items-center gap-1",children:[e.jsx(I,{className:"w-3.5 h-3.5"})," Unsaved changes"]}),!p&&r===(s?.remarks||"")&&e.jsxs("span",{className:"text-xs font-semibold text-success flex items-center gap-1",children:[e.jsx(I,{className:"w-3.5 h-3.5"})," Saved"]})]}),e.jsx("div",{className:"p-0",children:e.jsx(U,{theme:"snow",value:r,onChange:N,className:"h-64 border-0 [&_.ql-toolbar]:border-x-0 [&_.ql-toolbar]:border-t-0 [&_.ql-container]:border-0 [&_.ql-editor]:text-sm",placeholder:"Type remarks here... (Autosaves automatically)"})})]})}):null}function pe(){const l=Q(),i=W(),{projectId:n}=Y(),{fmtConv:s}=ie(),{data:v=[],isLoading:y}=X(),r=a.useMemo(()=>v.find(t=>t.id===n),[v,n]),[N]=Z(),[p]=O(),[w]=ee(),[b,d]=a.useState(!1),[E,T]=a.useState(null),[g,_]=a.useState(null),[j,S]=a.useState(""),[c,M]=a.useState(""),[m,C]=a.useState(""),[u,h]=a.useState({}),[P,F]=a.useState(""),q=(parseFloat(c)||0)-(parseFloat(m)||0),H=()=>{_(null),S(""),M(""),C(""),F(""),h({}),d(!0)},V=t=>{_(t.id),S(t.month),M(t.revenue.toString()),C(t.cost.toString()),F(t.remarks||""),h({}),d(!0)},G=async t=>{if(t.preventDefault(),!n)return;const o={};if(j||(o.month="Month is required"),(!c||parseFloat(c)<0)&&(o.revenue="Enter a valid revenue >= 0"),(!m||parseFloat(m)<0)&&(o.cost="Enter a valid cost >= 0"),r?.monthly_entries.some(k=>k.month===j&&k.id!==g)&&(o.month="An entry for this month already exists"),Object.keys(o).length>0){h(o);return}try{g?(await p({projectId:n,entryId:g,body:{month:j,revenue:parseFloat(c),cost:parseFloat(m),remarks:P||void 0}}).unwrap(),f.success("Monthly entry updated")):(await N({projectId:n,body:{month:j,revenue:parseFloat(c),cost:parseFloat(m),remarks:P||void 0}}).unwrap(),f.success("Monthly entry added")),d(!1)}catch(k){f.error(k?.data?.message||"Failed to save entry")}},J=async t=>{if(n&&confirm("Delete this entry?"))try{await w({projectId:n,entryId:t}).unwrap(),f.success("Entry deleted")}catch{f.error("Failed to delete entry")}},$=a.useMemo(()=>[{key:"month",label:"MONTH",sortable:!0,render:t=>e.jsx("span",{className:"font-medium text-foreground",children:B(t.month)})},{key:"revenue",label:"REVENUE",sortable:!0,render:t=>s(t.revenue)},{key:"cost",label:"COST",sortable:!0,render:t=>s(t.cost)},{key:"profit",label:"PROFIT",sortable:!0,render:t=>e.jsx("span",{className:A("font-medium",t.profit>=0?"text-success":"text-danger"),children:s(t.profit)})},{key:"actions",label:"ACTIONS",align:"center",fixed:"right",sortable:!1,render:t=>e.jsxs("div",{className:"flex items-center justify-center gap-1.5",children:[e.jsx(x,{variant:"outline",size:"icon",className:"h-7 w-7 text-muted-foreground hover:text-foreground hover:bg-accent",onClick:()=>V(t),title:"Edit",children:e.jsx(te,{className:"w-3.5 h-3.5"})}),e.jsxs(x,{variant:"outline",size:"icon",className:"h-7 w-7 relative text-muted-foreground hover:text-foreground hover:bg-accent",onClick:()=>T({isOpen:!0,entry:t}),title:t.remarks?"View/Edit Remarks":"Add Remarks",children:[e.jsx(se,{className:"w-3.5 h-3.5"}),t.remarks&&e.jsx("span",{className:"absolute -top-1 -right-1 w-2.5 h-2.5 bg-blue-500 rounded-full ring-2 ring-white dark:ring-card"})]}),e.jsx(x,{variant:"outline",size:"icon",className:"h-7 w-7 text-danger border-danger/20 hover:bg-danger/10 hover:text-danger",onClick:()=>J(t.id),title:"Delete",children:e.jsx(re,{className:"w-3.5 h-3.5"})})]})}],[s]);return y?e.jsx("div",{className:"p-4 flex items-center justify-center min-h-[400px]",children:e.jsx("div",{className:"w-8 h-8 border-4 border-primary border-t-transparent roun
1ded-full animate-spin"})}):r?e.jsxs("div",{className:"flex flex-col gap-4 animate-in fade-in duration-500 h-full min-h-0 p-3",children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("button",{onClick:()=>i(`${l}/project-profitability`),className:"w-7 h-7 mr-1 rounded-full border border-border bg-card flex items-center justify-center hover:bg-accent transition-all",children:e.jsx(ae,{className:"w-3.5 h-3.5"})}),e.jsx("span",{className:"w-1 h-6 rounded-full",style:{background:"hsl(var(--mod-project))"}}),e.jsxs("div",{children:[e.jsxs("h1",{className:"text-[16px] font-bold leading-tight",children:[r.project_name," â Monthly Data"]}),e.jsxs("div",{className:"text-[11px] text-muted-foreground",children:["Company: ",r.company_name||"â"]})]})]}),e.jsx("div",{className:"flex items-center gap-1.5",children:e.jsxs("button",{onClick:H,className:"h-7 px-2.5 rounded-[4px] bg-primary text-primary-foreground hover:opacity-90 text-[11px] font-medium inline-flex items-center gap-1.5",children:[e.jsx(ne,{className:"w-3.5 h-3.5"})," Add Month Entry"]})})]}),e.jsx(le,{className:"flex-1 min-h-[400px]",children:e.jsx(oe,{rows:r.monthly_entries||[],rowKey:t=>t.id,columns:$,searchPlaceholder:"Search entries...",searchKeys:["month"],initialPageSize:10,storageKey:"project-monthly-entries",fillHeight:!0})}),e.jsx(z,{open:b,onOpenChange:t=>!t&&d(!1),children:e.jsx(K,{className:"max-w-lg p-0 overflow-hidden bg-white rounded-xl shadow-xl border-0 gap-0",children:e.jsxs("form",{onSubmit:G,className:"flex flex-col h-full max-h-[90vh]",children:[e.jsx("div",{className:"px-5 py-4 border-b border-border flex justify-between items-center bg-muted/10",children:e.jsxs("div",{children:[e.jsx(L,{className:"text-lg font-bold",children:g?"Edit Month Entry":"Add Month Entry"}),e.jsxs("p",{className:"text-[11px] font-semibold text-muted-foreground uppercase tracking-wider mt-0.5",children:["PROJECT: ",r.project_name]})]})}),e.jsxs("div",{className:"p-5 overflow-y-auto space-y-4",children:[e.jsx(D,{label:"Month *",error:u.month,children:e.jsx(R,{type:"month",value:j,onChange:t=>{S(t.target.value),u.month&&h(o=>({...o,month:""}))},className:"h-10 font-medium",required:!0})}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsx(D,{label:"Revenue (INR) *",error:u.revenue,children:e.jsxs("div",{className:"relative",children:[e.jsx("span",{className:"absolute left-3 top-1/2 -translate-y-1/2 font-medium text-muted-foreground",children:"â¹"}),e.jsx(R,{type:"number",min:"0",step:"0.01",placeholder:"Enter revenue",value:c,onChange:t=>{M(t.target.value),u.revenue&&h(o=>({...o,revenue:""}))},className:"h-10 pl-7 font-medium",required:!0})]})}),e.jsx(D,{label:"Cost (INR) *",error:u.cost,children:e.jsxs("div",{className:"relative",children:[e.jsx("span",{className:"absolute left-3 top-1/2 -translate-y-1/2 font-medium text-muted-foreground",children:"â¹"}),e.jsx(R,{type:"number",min:"0",step:"0.01",placeholder:"Enter cost",value:m,onChange:t=>{C(t.target.value),u.cost&&h(o=>({...o,cost:""}))},className:"h-10 pl-7 font-medium",required:!0})]})})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-[12px] font-bold text-muted-foreground mb-1 block",children:"PROFIT PREVIEW"}),e.jsxs("div",{className:A("text-lg font-bold",q>=0?"text-success":"text-danger"),children:["â¹",q.toLocaleString("en-IN",{maximumFractionDigits:2})]})]}),!g&&e.jsxs("div",{className:"mt-4 pt-4 border-t border-border",children:[e.jsx("label",{className:"text-[12px] font-bold text-foreground mb-2 block",children:"Remarks (optional)"}),e.jsx(U,{theme:"snow",value:P,onChange:F,className:"h-40 border border-border rounded-lg overflow-hidden [&_.ql-toolbar]:border-x-0 [&_.ql-toolbar]:border-t-0 [&_.ql-toolbar]:bg-muted/30 [&_.ql-container]:border-0 [&_.ql-editor]:text-sm bg-white",placeholder:"Add any context or notes here..."})]})]}),e.jsxs("div",{className:"px-5 py-4 border-t border-border bg-muted/20 flex justify-between mt-12",children:[e.jsx(x,{type:"button",variant:"outline",onClick:()=>d(!1),children:"Cancel"}),e.jsx(x,{type:"submit",className:"font-semibold shadow-sm",children:"Save Entry"})]})]})})}),E&&e.jsx(de,{isOpen:E.isOpen,onClose:()=>T(null),projectId:n,entry:E.entry,projectName:r.project_name})]}):e.jsxs("div",{className:"p-4 text-center",children:[e.jsx("p",{className:"text-muted-foreground mb-4",children:"Project not found"}),e.jsx(x,{onClick:()=>i(`${l}/project-profitability`),children:"Go Back"})]})}
1export{pe as ProjectMonthlyDataPage};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.