import{p as w,q as J,g as d,j as r,G as u,h as W}from"./index-OkLaRrT4.js";import{r as s}from"./mui-icons-DSJWgROT.js";import{d as R}from"./mui-x-pickers-DVr_eQ3-.js";import{u as j,T as n}from"./mui-x-charts-BC4WZfuI.js";import{P as h,l as m}from"./mui-x-datagrid-Uj6xW2G5.js";const q=({open:c,onClose:G,brokerId:f,salespersonId:C,title:V})=>{const[b,y]=s.useState([]),[A,g]=s.useState(!1),[M,i]=s.useState(null),[v,p]=s.useState({page:0,pageSize:10}),[x,I]=s.useState(0),o=j().palette.mode==="dark",[O]=w(),[T]=J(),E=e=>new Intl.NumberFormat("en-IN",{style:"currency",currency:"INR"}).format(e||0),P=e=>{if(!e)return"N/A";try{return(typeof e=="string"?new Date(e):e).toLocaleDateString("en-IN")}catch{return"Invalid Date"}},L=e=>{switch(e.toUpperCase()){case"CANCELLED":return{bgColor:o?"#121212":"#fdecea",textColor:"#d32f2f"};case"REJECTED":return{bgColor:o?"#121212":"#fdecea",textColor:"#d32f2f"};case"APPROVED":return{bgColor:o?"#121212":"#e8f5e9",textColor:"#388e3c"};case"PENDING_APPROVAL":return{bgColor:o?"#121212":"#fff8e1",textColor:"#f57c00"};case"BILLED":return{bgColor:o?"#121212":"#e3f2fd",textColor:"#1976d2"};case"INITIATED":return{bgColor:o?"#121212":"#f3e5f5",textColor:"#7b1fa2"};case"ACTIVE":return{bgColor:o?"#121212":"#e8f5e9",textColor:"#388e3c"};case"COMPLETED":return{bgColor:o?"#121212":"#e3f2fd",textColor:"#1976d2"};case"PENDING":return{bgColor:o?"#121212":"#fff8e1",textColor:"#f57c00"};case"INVOICE_CREATED":return{bgColor:o?"#121212":"#e8f5e9",textColor:"#388e3c"};default:return{bgColor:o?"#121212":"#f5f5f5",textColor:"#9e9e9e"}}},D=async(e,a)=>{if(c){g(!0),i(null);try{let l;if(f)l=await O({brokerId:f,page:e,size:a});else if(C)l=await T({salespersonId:C,page:e,size:a});else{i("No ID provided");return}if("data"in l&&l.data){const t=l.data,N=Array.isArray(t)?t:t.data,B=t.totalCount||N.length;y(N||[]),I(B)}else"error"in l,i("Failed to fetch job orders")}catch{i("Error fetching job orders")}finally{g(!1)}}};s.useEffect(()=>{c&&(p({page:0,pageSize:10}),D(0,10))},[c,f,C]);const k=e=>{p(e),D(e.page,e.pageSize)},S=[{field:"companyCode",headerName:"Job Order No",flex:1,minWidth:150,renderCell:e=>r(n,{variant:"body2",fontWeight:500,children:e.value||e.row.id||"N/A"})},{field:"clientName",headerName:"Client Name",flex:1,minWidth:150,renderCell:e=>r(n,{variant:"body2",children:e.value||"N/A"})},{field:"status",headerName:"Status",flex:1,minWidth:120,renderCell:e=>{const a=e.value||"N/A",l=L(a);let t=a;switch(a.toUpperCase()){case"CANCELLED":t="Cancelled";break;case"REJECTED":t="Rejected";break;case"APPROVED":t="Approved";break;case"PENDING_APPROVAL":t="Pending Approval";break;case"BILLED":t="Billed";break;case"INITIATED":t="Created";break;case"ACTIVE":t="Active";break;case"COMPLETED":t="Completed";break;case"PENDING":t="Pending";break;case"INVOICE_CREATED":t="Invoice Created";break;default:t="Not set"}return r(m,{sx:{display:"inline-block",px:1.5,py:.25,borderRadius:1.5,backgroundColor:l.bgColor,color:l.textColor,fontWeight:600,fontSize:13,minWidth:60,textAlign:"center",boxShadow:"0 1px 4px 0 rgba(0,0,0,0.06)"},children:t})}},{field:"totalAmount",headerName:"Total Amount",flex:1,minWidth:130,type:"number",renderCell:e=>r(n,{variant:"body2",fontWeight:500,children:E(e.value||0)})},{field:"createdDate",headerName:"Created On",flex:1,renderCell:e=>R(e.value).format("M/D/YYYY h:mm A")},{field:"expectedDeliveryDate",headerName:"Expected Delivery",flex:1,minWidth:140,renderCell:e=>r(n,{variant:"body2",children:P(e.value)})}];return c?d(m,{children:[d(u,{container:!0,spacing:2,sx:{mb:3},children:[r(u,{item:!0,xs:12,md:4,children:d(h,{elevation:1,sx:{p:2,border:"1px solid #2196f3"},children:[r(n,{variant:"subtitle2",color:"#1565c0",gutterBottom:!0,children:"Total Job Orders"}),r(n,{variant:"h4",color:"#1565c0",fontWeight:"bold",children:x})]})}),r(u,{item:!0,xs:12,md:4,children:d(h,{elevation:1,sx:{p:2,border:"1px solid #9c27b0"},children:[r(n,{variant:"subtitle2",color:"#7b1fa2",gutterBottom:!0,children:"Total Amount"}),r(n,{variant:"h4",color:"#7b1fa2",fontWeight:"bold",children:E(b.reduce((e,a)=>e+(a.totalAmount||0),0))})]})}),r(u,{item:!0,xs:12,md:4,children:d(h,{elevation:1,sx:{p:2,border:"1px solid #4caf50"},children:[r(n,{variant:"subtitle2",color:"#2e7d32",gutterBottom:!0,children:"Active Orders"}),r(n,{variant:"h4",color:"#2e7d32",fontWeight:"bold",children:b.filter(e=>e.status==="INITIATED").length})]})})]}),r(m,{sx:{height:400,width:"100%"},children:r(W,{rows:b,columns:S,rowCount:x,loading:A,checkboxSelection:!1,paginationModel:v,handlePaginationChange:k,enableManageColumns:!0,defaultHiddenColumns:["id"],disableColumnFilter:!0,disableColumnSelector:!0,disableDensitySelector:!0})})]}):null};export{q as J};
