import{a as U,M as Y,r as t,N as J,b as a,q as T,B as o,j as s,Q as K,n as B,o as V,T as X,A as Z,ah as _,H as ee,e as ae,D as te,f as ne,h as le,g as se,i as oe,ai as E,I as P,S as ie}from"./index-DlNZgIG9.js";import{d as re}from"./DotStream-BRQMdCJj.js";import{u as de,a as ce,L as ue}from"./LeadSampleDialog-_oWuGpDJ.js";import{C as he}from"./Close-DYYvLTb2.js";import{S as pe}from"./Search-Bc43mxWD.js";import"./useSalesPersonScope-CNYMl7b2.js";import"./LeadsApi-B3XDImUD.js";function De(){const{openSnackbar:c}=U(),{read:m,write:D,create:j,loading:F}=Y("FollowUp"),[w,y]=t.useState([]),[O,v]=t.useState(0),[R,N]=t.useState(!1),[f,A]=t.useState(""),i=J(f,500),[l,I]=t.useState({page:0,pageSize:10}),[Q,g]=t.useState(!1),[$,S]=t.useState(null),[n,u]=t.useState(null),[r,x]=t.useState(!1),[k]=de(),[q]=ce(),C=t.useCallback(async(e,h,d)=>{var W,L,M;N(!0);const p=await k({page:e,size:h,searchText:d||void 0});if("data"in p)y(((W=p.data)==null?void 0:W.data)??[]),v(((L=p.data)==null?void 0:L.totalElements)??0);else{y([]),v(0);const b=p.error;c(((M=b==null?void 0:b.data)==null?void 0:M.message)??"Failed to load lead samples","error")}N(!1)},[k,c]);t.useEffect(()=>{m&&C(l.page,l.pageSize,i)},[m,C,l.page,l.pageSize,i]),t.useEffect(()=>{I(e=>({...e,page:0}))},[i]);const z=()=>C(l.page,l.pageSize,i),G=async()=>{var h;if(!(n!=null&&n.id)||r)return;x(!0);const e=await q(n.id);if("data"in e)c("Sample deleted successfully","success"),u(null),x(!1),z();else{const d=e.error;c(((h=d==null?void 0:d.data)==null?void 0:h.message)??"An error occurred","error"),x(!1)}},H=[{field:"leadName",headerName:"Lead",flex:2,minWidth:180,renderCell:e=>e.value||(e.row.leadId?`#${e.row.leadId}`:"—")},{field:"batchNumber",headerName:"Batch",flex:1,minWidth:130,renderCell:e=>e.value||"—"},{field:"sampleQuantity",headerName:"No. of Samples",flex:1,minWidth:130,renderCell:e=>e.value??"—"},{field:"sampleDate",headerName:"Sample Date",flex:1,minWidth:130,renderCell:e=>e.value?_(e.value):"—"},{field:"salesPersonName",headerName:"Sales Person",flex:1.2,minWidth:150,renderCell:e=>e.value||"—"},{field:"createdDate",headerName:"Created Date",flex:1.4,minWidth:170,renderCell:e=>e.value?ee(e.value):"—"},{field:"actions",headerName:"Actions",sortable:!1,filterable:!1,minWidth:120,renderCell:e=>s(ie,{direction:"row",spacing:.5,alignItems:"center",children:[D&&a(P,{type:"Edit",onClick:()=>{S(e.row),g(!0)}}),D&&a(P,{type:"Delete",onClick:()=>u(e.row)})]})}];return!F&&!m?a(o,{p:3,children:a(T,{color:"error",children:"You do not have permission to view lead samples. Contact your administrator if this is unexpected."})}):s(o,{p:2,width:"100%",children:[s(K,{children:[s(o,{display:"flex",gap:1,flexWrap:"wrap",alignItems:"center",mb:2,children:[a(X,{size:"small",value:f,onChange:e=>A(e.target.value),placeholder:"Search lead or batch ...",sx:{width:{xs:"100%",sm:200},"& .MuiOutlinedInput-root":{borderRadius:"8px",fontSize:"0.85rem",height:36}},InputProps:{startAdornment:a(B,{position:"start",children:a(pe,{sx:{fontSize:14,color:"text.secondary"}})}),endAdornment:f?a(B,{position:"end",children:a(V,{size:"small",onClick:()=>A(""),sx:{p:.3},children:a(he,{sx:{fontSize:14}})})}):null}}),j&&a(o,{sx:{ml:{sm:"auto"},width:{xs:"100%",sm:"auto"}},children:a(Z,{btnType:"create",onClick:()=>{S(null),g(!0)},children:"Add Sample"})})]}),R?a(o,{sx:{height:500,display:"flex",alignItems:"center",justifyContent:"center",width:"100%"},children:a(re,{size:"60",speed:"2.5",color:"black"})}):w.length===0?a(o,{py:6,textAlign:"center",children:a(T,{variant:"body2",color:"text.secondary",children:i?"No matching samples.":"No samples recorded yet. Add one using the button above."})}):a(ae,{rows:w,columns:H,rowCount:O,loading:!1,checkboxSelection:!1,paginationModel:l,handlePaginationChange:e=>I(e),enableManageColumns:!0})]}),a(ue,{open:Q,sample:$,onClose:()=>{g(!1),S(null)},onSaved:z}),s(te,{open:!!n,onClose:()=>!r&&u(null),maxWidth:"xs",fullWidth:!0,children:[a(ne,{children:"Delete Sample"}),a(se,{children:s(le,{children:["Are you sure you want to delete this sample record",n!=null&&n.batchNumber?` (batch ${n.batchNumber})`:"","?",a("br",{}),"This action cannot be undone."]})}),s(oe,{children:[a(E,{onClick:()=>u(null),color:"inherit",disabled:r,children:"Cancel"}),a(E,{onClick:G,color:"error",variant:"contained",disabled:r,children:r?"Deleting...":"Delete"})]})]})]})}export{ue as LeadSampleDialog,De as default};
