import{r as de,j as e,i as he,b as u,B as h,T as a,S as _,l as xe,C as J,e as pe,p as ue,A as ge,n as re,M as C,I as M}from"./index-D7LVXeIK.js";import{d as me}from"./EditOutlined-DS7hq7Cq.js";import{T as fe,d as ye}from"./DeleteOutline-Dhk3Cr2d.js";import{d as se,D as je}from"./PhoneOutlined-Uvrb40S4.js";import{c as H,a as be,b as ve,d as N}from"./StorefrontOutlined-Be07_1-Y.js";import{d as _e}from"./LocalShippingOutlined-C1uYlQnS.js";import{d as Ce}from"./MapOutlined-CMNX914Q.js";import{d as q,D as te,a as ie,b as ne,c as oe,e as X}from"./Close-D3HB9j5y.js";import{P as Se,G as i,A as ae,C as T}from"./PageHeader-naLAT9xr.js";import{S as U,E as ke}from"./EmptyState-B9FcYx_n.js";import{C as le}from"./Link-BEif5YS2.js";import{T as g}from"./TextField-Sd_pshci.js";import{I as Ie}from"./InputAdornment-CMIlL2rx.js";import{B as k}from"./Button-Bqp5awT1.js";import{S as Pe}from"./Snackbar-DOheZfWW.js";import"./TableCell-DreGbAeW.js";var G={},ze=he;Object.defineProperty(G,"__esModule",{value:!0});var D=G.default=void 0,We=ze(de()),Ee=e;D=G.default=(0,We.default)((0,Ee.jsx)("path",{d:"M7 15h7v2H7zm0-4h10v2H7zm0-4h10v2H7zm12-4h-4.18C14.4 1.84 13.3 1 12 1c-1.3 0-2.4.84-2.82 2H5c-.14 0-.27.01-.4.04-.39.08-.74.28-1.01.55-.18.18-.33.4-.43.64-.1.23-.16.49-.16.77v14c0 .27.06.54.16.78s.25.45.43.64c.27.27.62.47 1.01.55.13.02.26.03.4.03h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2m-7-.25c.41 0 .75.34.75.75s-.34.75-.75.75-.75-.34-.75-.75.34-.75.75-.75M19 19H5V5h14z"}),"AssignmentOutlined");const O="https://api.doormile.com/api/v1",Y=()=>{const r=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...r?{Authorization:`Bearer ${r}`}:{}}},Ae={textTransform:"uppercase",letterSpacing:.4,fontSize:"0.68rem",fontWeight:700,color:"grey.700"};function ee({label:r,color:l="default"}){return r?e.jsx(T,{size:"small",label:r,sx:{fontWeight:600,...l==="default"?{bgcolor:"grey.100",color:"grey.700"}:{bgcolor:`${l}.lighter`,color:`${l}.dark`}}}):e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:"grey.500"},children:"—"})}function f({label:r,children:l}){return e.jsxs(h,{children:[e.jsx(a,{variant:"caption",color:"text.secondary",sx:Ae,children:r}),e.jsx(h,{sx:{mt:.5},children:l})]})}function B({icon:r,title:l,children:x}){return e.jsxs(h,{sx:{height:"100%",borderRadius:2,border:1,borderColor:"divider",bgcolor:"background.paper",overflow:"hidden"},children:[e.jsxs(_,{direction:"row",spacing:1,alignItems:"center",sx:{px:2,py:1.25,borderBottom:1,borderColor:"divider",bgcolor:"primary.lighter"},children:[e.jsx(r,{sx:{fontSize:18,color:"primary.main"}}),e.jsx(a,{variant:"overline",sx:{fontWeight:700,color:"grey.800",letterSpacing:.6,lineHeight:1},children:l})]}),e.jsx(_,{spacing:1.75,sx:{p:2},children:x})]})}async function we(){const r=await fetch(`${O}/admin/competitor-branches?limit=1000`,{headers:Y()});if(!r.ok)throw new Error("Failed to fetch surveys");const l=await r.json(),x=Array.isArray(l)?l:l.data||[];return{data:x,total:x.length}}async function Re(r){const l=!!r.id,x=l?`${O}/admin/competitor-branches/${r.id}`:`${O}/admin/competitor-branches`,s=await fetch(x,{method:l?"PUT":"POST",headers:Y(),body:JSON.stringify(r)});if(!s.ok){const c=await s.json().catch(()=>({}));throw new Error(c.error||c.message||"Failed to save survey")}return s.status===204?{}:s.json().catch(()=>({}))}async function Te(r){const l=!!r.id,x=l?`${O}/admin/carrier-pricing/${r.id}`:`${O}/admin/carrier-pricing`,s=await fetch(x,{method:l?"PUT":"POST",headers:Y(),body:JSON.stringify({company:r.company,weight_slab:r.weight_slab,zone:r.zone||"",service_type:r.service_type||"",rate:String(r.rate)})});if(!s.ok){const c=await s.json().catch(()=>({}));throw new Error(c.error||c.message||"Failed to save pricing")}return s.status===204?{}:s.json().catch(()=>({}))}const De=["Local / Same","Within Tamilnadu","Interstate India"],Oe=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];function $e({open:r,onClose:l,onSave:x,initialData:m}){const[s,c]=u.useState({}),[y,v]=u.useState(!1),[p,I]=u.useState(null);u.useEffect(()=>{r&&(c(m?{...m,pincodes:m.pincodes?m.pincodes.split(",").map(n=>n.trim()).filter(Boolean):[]}:{company:"",area:"",phone:"",rate_per_kg:"",offers_pickup:"no",offers_drop:"no",packing_charge:"",time_in_days:"",plus_code:"",address:"",frequency:"",pincodes:[],slabs:[]}),I(null))},[r,m]);const j=n=>t=>{c(d=>({...d,[n]:t.target.value}))},z=(n,t)=>d=>{const W=[...s.slabs||[]];W[n]={...W[n],[t]:d.target.value},c(w=>({...w,slabs:W}))},F=()=>{c(n=>({...n,slabs:[...n.slabs||[],{weight_slab:"",zone:"",service_type:"",rate:""}]}))},$=n=>{const t=[...s.slabs||[]];t.splice(n,1),c(d=>({...d,slabs:t}))},S=n=>t=>{const d=[...s.pincodes||[]];d[n]=t.target.value,c(W=>({...W,pincodes:d}))},E=()=>{c(n=>({...n,pincodes:[...n.pincodes||[],""]}))},A=n=>{const t=[...s.pincodes||[]];t.splice(n,1),c(d=>({...d,pincodes:t}))},L=async()=>{v(!0),I(null);try{if(s.pincodes&&s.pincodes.length>0){const t=s.pincodes.filter(d=>d.trim()&&!/^\d{6}$/.test(d.trim()));if(t.length>0)throw new Error(`Invalid pincodes detected: ${t.join(", ")}. All pincodes must be exactly 6 numeric digits.`)}const n={...s.id?{id:s.id}:{},company:s.company||"",area:s.area||"",phone:s.phone||"",rate_per_kg:s.rate_per_kg||"",offers_pickup:s.offers_pickup||"no",offers_drop:s.offers_drop||"no",packing_charge:s.packing_charge||"",time_in_days:s.time_in_days||"",plus_code:s.plus_code||"",address:s.address||"",frequency:s.frequency||"",pincodes:s.pincodes?s.pincodes.join(","):""};if(await Re(n),s.slabs&&s.slabs.length>0&&s.company)try{await Promise.all(s.slabs.map(t=>t.weight_slab&&t.rate?Te({company:s.company,weight_slab:t.weight_slab,zone:t.zone||"",service_type:t.service_type||"",rate:t.rate}):Promise.resolve()))}catch(t){console.error("Failed to save pricing slabs",t)}x(),l()}catch(n){console.error(n),I(n.message||"Failed to save. Please try again.")}finally{v(!1)}};return e.jsxs(e.Fragment,{children:[e.jsx(Pe,{open:!!p,autoHideDuration:6e3,onClose:()=>I(null),anchorOrigin:{vertical:"top",horizontal:"right"},sx:{zIndex:9999},children:e.jsx(ae,{onClose:()=>I(null),severity:"error",variant:"filled",sx:{width:"100%",boxShadow:3},children:p})}),e.jsxs(te,{open:r,onClose:y?void 0:l,maxWidth:"sm",fullWidth:!0,children:[e.jsx(ie,{sx:{fontWeight:800},children:m?"Edit Survey Record":"Add New Survey"}),e.jsx(ne,{dividers:!0,children:e.jsxs(i,{container:!0,spacing:2,children:[e.jsx(i,{item:!0,xs:12,sm:6,children:e.jsx(g,{fullWidth:!0,label:"Company",value:s.company||"",onChange:j("company"),size:"small"})}),e.jsx(i,{item:!0,xs:12,sm:6,children:e.jsx(g,{fullWidth:!0,label:"Area / Zone",value:s.area||"",onChange:j("area"),size:"small"})}),e.jsx(i,{item:!0,xs:12,sm:6,children:e.jsx(g,{fullWidth:!0,label:"Phone",value:s.phone||"",onChange:j("phone"),size:"small"})}),e.jsx(i,{item:!0,xs:12,sm:6,children:e.jsx(g,{fullWidth:!0,label:"Rate Per KG",value:s.rate_per_kg||"",onChange:j("rate_per_kg"),size:"small"})}),e.jsx(i,{item:!0,xs:12,sm:6,children:e.jsxs(g,{select:!0,fullWidth:!0,label:"Offers Pickup",value:s.offers_pickup||"no",onChange:j("offers_pickup"),size:"small",children:[e.jsx(C,{value:"yes",children:"Yes"}),e.jsx(C,{value:"no",children:"No"})]})}),e.jsx(i,{item:!0,xs:12,sm:6,children:e.jsxs(g,{select:!0,fullWidth:!0,label:"Offers Drop",value:s.offers_drop||"no",onChange:j("offers_drop"),size:"small",children:[e.jsx(C,{value:"yes",children:"Yes"}),e.jsx(C,{value:"no",children:"No"})]})}),e.jsx(i,{item:!0,xs:12,sm:6,children:e.jsx(g,{fullWidth:!0,label:"Time in Days",value:s.time_in_days||"",onChange:j("time_in_days"),size:"small"})}),e.jsx(i,{item:!0,xs:12,sm:6,children:e.jsx(g,{fullWidth:!0,label:"Packing Charge",value:s.packing_charge||"",onChange:j("packing_charge"),size:"small"})}),e.jsx(i,{item:!0,xs:12,sm:6,children:e.jsxs(g,{select:!0,fullWidth:!0,label:"Frequency",value:s.frequency||"",onChange:j("frequency"),size:"small",children:[e.jsx(C,{value:"Daily",children:"Daily"}),e.jsx(C,{value:"Weekly",children:"Weekly"}),e.jsx(C,{value:"Bi-weekly",children:"Bi-weekly"}),e.jsx(C,{value:"Monthly",children:"Monthly"}),e.jsx(C,{value:"On-demand",children:"On-demand"})]})}),e.jsx(i,{item:!0,xs:12,children:e.jsx(g,{fullWidth:!0,label:"Plus Code",value:s.plus_code||"",onChange:j("plus_code"),size:"small"})}),e.jsx(i,{item:!0,xs:12,children:e.jsxs(h,{sx:{mt:1,pt:2,borderTop:"1px dashed",borderColor:"divider"},children:[e.jsxs(h,{sx:{display:"flex",justifyContent:"space-between",alignItems:"center",mb:2},children:[e.jsx(a,{variant:"subtitle2",sx:{fontWeight:700,color:"primary.main",letterSpacing:.5},children:"SERVICEABLE PINCODES"}),e.jsx(k,{size:"small",startIcon:e.jsx(q,{}),onClick:E,sx:{fontWeight:600},children:"Add Pincode"})]}),(s.pincodes||[]).map((n,t)=>e.jsxs(i,{container:!0,spacing:1.5,sx:{mb:1.5,alignItems:"center"},children:[e.jsx(i,{item:!0,xs:10,sm:11,children:e.jsx(g,{fullWidth:!0,label:`Pincode ${t+1}`,value:n,onChange:S(t),size:"small",placeholder:"e.g. 600001",inputProps:{maxLength:6}})}),e.jsx(i,{item:!0,xs:2,sm:1,children:e.jsx(M,{size:"small",color:"error",onClick:()=>A(t),children:e.jsx(X,{fontSize:"small"})})})]},t)),(!s.pincodes||s.pincodes.length===0)&&e.jsx(a,{variant:"body2",color:"text.secondary",sx:{textAlign:"center",py:2,bgcolor:"grey.50",borderRadius:1,border:"1px dashed",borderColor:"grey.300"},children:"Click 'Add Pincode' to assign locations to this competitor."})]})}),e.jsx(i,{item:!0,xs:12,children:e.jsx(g,{fullWidth:!0,multiline:!0,rows:2,label:"Address",value:s.address||"",onChange:j("address"),size:"small"})}),e.jsx(i,{item:!0,xs:12,children:e.jsxs(h,{sx:{mt:1,pt:2,borderTop:"1px dashed",borderColor:"divider"},children:[e.jsxs(h,{sx:{display:"flex",justifyContent:"space-between",alignItems:"center",mb:2},children:[e.jsx(a,{variant:"subtitle2",sx:{fontWeight:700,color:"primary.main",letterSpacing:.5},children:"PRICING SLABS (OPTIONAL)"}),e.jsx(k,{size:"small",startIcon:e.jsx(q,{}),onClick:F,sx:{fontWeight:600},children:"Add Slab"})]}),(s.slabs||[]).map((n,t)=>e.jsxs(i,{container:!0,spacing:1.5,sx:{mb:1.5,alignItems:"center"},children:[e.jsx(i,{item:!0,xs:6,sm:3,children:e.jsx(g,{select:!0,fullWidth:!0,label:"Weight Slab",value:n.weight_slab||"",onChange:z(t,"weight_slab"),size:"small",children:Oe.map(d=>e.jsx(C,{value:d,children:d},d))})}),e.jsx(i,{item:!0,xs:6,sm:3,children:e.jsx(g,{select:!0,fullWidth:!0,label:"Zone",value:n.zone||"",onChange:z(t,"zone"),size:"small",children:De.map(d=>e.jsx(C,{value:d,children:d},d))})}),e.jsx(i,{item:!0,xs:6,sm:3,children:e.jsx(g,{fullWidth:!0,label:"Service Type",value:n.service_type||"",onChange:z(t,"service_type"),size:"small",placeholder:"e.g. Air"})}),e.jsx(i,{item:!0,xs:4,sm:2,children:e.jsx(g,{fullWidth:!0,label:"Rate (₹)",value:n.rate||"",onChange:z(t,"rate"),size:"small",placeholder:"e.g. 25"})}),e.jsx(i,{item:!0,xs:2,sm:1,children:e.jsx(M,{size:"small",color:"error",onClick:()=>$(t),children:e.jsx(X,{fontSize:"small"})})})]},t)),(!s.slabs||s.slabs.length===0)&&e.jsx(a,{variant:"body2",color:"text.secondary",sx:{textAlign:"center",py:2,bgcolor:"grey.50",borderRadius:1,border:"1px dashed",borderColor:"grey.300"},children:"Click 'Add Slab' to add pricing entries for this competitor."})]})})]})}),e.jsxs(oe,{sx:{px:3,py:2},children:[e.jsx(k,{onClick:l,disabled:y,color:"inherit",children:"Cancel"}),e.jsx(k,{onClick:L,disabled:y,variant:"contained",children:y?"Saving...":"Save Record"})]})]})]})}function Le({row:r,onEdit:l,onDelete:x,users:m}){const[s,c]=u.useState(!1),y=m.find(p=>p.id===r.created_by),v=m.find(p=>p.id===r.updated_by);return e.jsxs(h,{sx:{p:2,borderRadius:2,border:"1px solid",borderColor:"divider",bgcolor:"background.paper",transition:"border-color 0.2s ease, box-shadow 0.2s ease","&:hover":{borderColor:"primary.main",boxShadow:"0 4px 12px rgba(192, 18, 39, 0.05)"}},children:[e.jsxs(i,{container:!0,spacing:2,alignItems:"center",onClick:()=>c(!s),sx:{cursor:"pointer",userSelect:"none"},children:[e.jsx(i,{item:!0,xs:12,md:4.5,children:e.jsxs(_,{direction:"row",spacing:1.5,alignItems:"center",children:[e.jsx(h,{sx:{p:1,borderRadius:1.5,bgcolor:"primary.lighter",display:"flex",alignItems:"center",justifyContent:"center"},children:e.jsx(N,{sx:{color:"primary.main",fontSize:18}})}),e.jsxs(h,{children:[e.jsx(a,{variant:"subtitle2",sx:{fontWeight:700,color:"grey.900",lineHeight:1.2},children:r.area||"Unknown Area"}),e.jsxs(a,{variant:"caption",sx:{color:"grey.600",display:"flex",alignItems:"center",gap:.5,mt:.25},children:[e.jsx(se,{sx:{fontSize:12}})," ",r.phone||"—"]})]})]})}),e.jsx(i,{item:!0,xs:6,md:3.5,children:e.jsxs(h,{children:[e.jsx(a,{variant:"caption",sx:{color:"grey.500",fontWeight:700,letterSpacing:.5,display:"block",mb:.5},children:"RATE PER KG"}),e.jsx(ee,{label:r.rate_per_kg,color:!r.rate_per_kg||r.rate_per_kg.toLowerCase().includes("not answered")||r.rate_per_kg.toLowerCase().includes("wrong number")?"warning":"success"})]})}),e.jsx(i,{item:!0,xs:6,md:2,children:e.jsxs(h,{children:[e.jsx(a,{variant:"caption",sx:{color:"grey.500",fontWeight:700,letterSpacing:.5,display:"block",mb:.5},children:"LOGISTICS"}),e.jsx(a,{variant:"body2",sx:{fontWeight:600,color:"grey.800"},children:r.offers_pickup==="yes"||r.offers_drop==="yes"?`${r.offers_pickup==="yes"?"Pickup":""} ${r.offers_drop==="yes"?"Drop":""}`:"—"})]})}),e.jsxs(i,{item:!0,xs:12,md:2,sx:{display:"flex",justifyContent:{xs:"flex-start",md:"flex-end"},alignItems:"center",gap:.5},children:[e.jsx(k,{size:"small",sx:{borderRadius:1.5,color:s?"primary.main":"grey.700",bgcolor:s?"primary.lighter":"transparent","&:hover":{bgcolor:"primary.lighter",color:"primary.main"}},children:s?"Hide Info":"More Info"}),e.jsx(M,{size:"small",onClick:p=>{p.stopPropagation(),l(r)},sx:{color:"grey.400","&:hover":{color:"primary.main",bgcolor:"primary.lighter"}},children:e.jsx(me,{fontSize:"small"})}),e.jsx(M,{size:"small",onClick:p=>{p.stopPropagation(),x(r)},sx:{color:"grey.400","&:hover":{color:"error.main",bgcolor:"error.lighter"}},children:e.jsx(ye,{fontSize:"small"})})]})]}),e.jsx(re,{in:s,timeout:"auto",unmountOnExit:!0,children:e.jsx(h,{sx:{mt:2,pt:2,borderTop:"1px dashed",borderColor:"divider"},children:e.jsxs(i,{container:!0,spacing:2,alignItems:"stretch",children:[e.jsx(i,{item:!0,xs:12,md:4,children:e.jsxs(B,{icon:H,title:"ENQUIRY DETAILS",children:[e.jsx(f,{label:"RATE PER KG",children:e.jsx(ee,{label:r.rate_per_kg,color:"success"})}),e.jsx(f,{label:"PICKUP",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:r.offers_pickup?"grey.800":"grey.500"},children:r.offers_pickup||"—"})}),e.jsx(f,{label:"DROP",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:r.offers_drop?"grey.800":"grey.500"},children:r.offers_drop||"—"})}),e.jsx(f,{label:"PACKING",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:r.packing_charge?"grey.800":"grey.500"},children:r.packing_charge||"—"})})]})}),e.jsx(i,{item:!0,xs:12,md:4,children:e.jsxs(B,{icon:_e,title:"LOGISTICS & OPS",children:[e.jsx(f,{label:"TIME IN DAYS",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:r.time_in_days?"grey.800":"grey.500"},children:r.time_in_days||"—"})}),e.jsx(f,{label:"COMPANY",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:r.company||"—"})}),e.jsx(f,{label:"FREQUENCY",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:r.frequency||"—"})}),e.jsx(f,{label:"CONTACT NUMBER",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:r.phone||"—"})})]})}),e.jsx(i,{item:!0,xs:12,md:4,children:e.jsxs(B,{icon:N,title:"LOCATION & PINCODE",children:[e.jsx(f,{label:"AREA / ZONE",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:r.area||"—"})}),e.jsx(f,{label:"SERVICEABLE PINCODES",children:r.pincodes?e.jsx(h,{sx:{display:"flex",gap:.5,flexWrap:"wrap",mt:.5},children:r.pincodes.split(",").map((p,I)=>e.jsx(T,{label:p.trim(),size:"small",sx:{fontSize:"0.7rem",height:20}},I))}):e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:"grey.500"},children:"—"})}),e.jsx(f,{label:"PLUS CODE",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:r.plus_code?"grey.800":"grey.500"},children:r.plus_code||"—"})}),e.jsx(f,{label:"FULL ADDRESS",children:e.jsxs(_,{spacing:1.5,children:[e.jsxs(_,{direction:"row",spacing:1,sx:{p:1.5,borderRadius:1.5,bgcolor:"grey.50",border:1,borderColor:"divider"},children:[e.jsx(N,{sx:{fontSize:16,color:"grey.400",mt:"2px"}}),e.jsx(a,{variant:"body2",sx:{color:"grey.800",lineHeight:1.5},children:r.address||"—"})]}),(r.address||r.plus_code)&&e.jsx(k,{component:"a",href:`https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(r.plus_code?r.plus_code+" "+(r.address||""):r.address)}`,target:"_blank",rel:"noopener",size:"small",variant:"outlined",startIcon:e.jsx(Ce,{sx:{fontSize:16}}),sx:{alignSelf:"flex-start",py:.5,px:1.5,fontSize:"0.75rem",fontWeight:600,borderRadius:2,color:"primary.main",borderColor:"primary.light",bgcolor:"primary.lighter","&:hover":{borderColor:"primary.main",bgcolor:"primary.lighter"}},children:"View on map"})]})})]})}),e.jsx(i,{item:!0,xs:12,children:e.jsx(B,{icon:D,title:"RECORD METADATA",children:e.jsxs(i,{container:!0,spacing:2,children:[e.jsx(i,{item:!0,xs:6,children:e.jsx(f,{label:"CREATED BY",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:y?y.first_name:r.created_by?`User ID: ${r.created_by}`:"System"})})}),e.jsx(i,{item:!0,xs:6,children:e.jsx(f,{label:"LAST EDITED BY",children:e.jsx(a,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:v?v.first_name:r.updated_by?`User ID: ${r.updated_by}`:"—"})})})]})})})]})})})]})}function Be({companyName:r,branches:l,onEdit:x,onDelete:m,users:s}){const[c,y]=u.useState(!1);return e.jsxs(le,{elevation:0,sx:{borderColor:c?"primary.main":"grey.200",p:2,mb:2,transition:"border-color 0.3s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.3s cubic-bezier(0.4, 0, 0.2, 1)","&:hover":{borderColor:"primary.main",boxShadow:"0 8px 30px rgba(192, 18, 39, 0.08)"}},children:[e.jsxs(_,{direction:{xs:"column",md:"row"},spacing:3,alignItems:{xs:"flex-start",md:"center"},justifyContent:"space-between",onClick:()=>y(!c),sx:{cursor:"pointer",userSelect:"none"},children:[e.jsxs(_,{direction:"row",spacing:2,alignItems:"center",children:[e.jsx(ge,{sx:{width:42,height:42,bgcolor:"primary.lighter",color:"primary.main",fontWeight:800,borderRadius:2},children:(r||"A")[0].toUpperCase()}),e.jsxs(h,{children:[e.jsx(a,{variant:"h5",sx:{fontWeight:800,lineHeight:1.2,color:"grey.900"},children:r||"Unknown Company"}),e.jsxs(a,{variant:"body2",sx:{color:"grey.600",mt:.5,display:"flex",alignItems:"center",gap:.5},children:[e.jsx(H,{sx:{fontSize:16}})," ",l.length," Location",l.length!==1&&"s"," Surveyed"]})]})]}),e.jsxs(_,{direction:"row",spacing:3,alignItems:"center",children:[e.jsxs(_,{direction:"row",spacing:1,sx:{display:{xs:"none",md:"flex"}},children:[e.jsx(T,{size:"small",label:"First Mile",sx:{bgcolor:"primary.lighter",color:"primary.main",fontWeight:700}}),e.jsx(T,{size:"small",label:"Mid Mile",sx:{bgcolor:"primary.lighter",color:"primary.main",fontWeight:700}}),e.jsx(T,{size:"small",label:"Last Mile",sx:{bgcolor:"primary.lighter",color:"primary.main",fontWeight:700}})]}),e.jsx(_,{direction:"row",spacing:1.5,alignItems:"center",children:e.jsx(k,{sx:{borderRadius:2,py:1,px:3,fontWeight:700,color:c?"primary.main":"grey.700",bgcolor:c?"primary.lighter":"grey.100","&:hover":{bgcolor:"primary.lighter",color:"primary.main"}},endIcon:c?e.jsx(be,{}):e.jsx(ve,{}),children:c?"Hide Locations":"View Locations"})})]})]}),e.jsx(re,{in:c,timeout:"auto",unmountOnExit:!0,children:e.jsx(h,{sx:{mt:2,pt:2,borderTop:"2px dashed",borderColor:"grey.200"},children:e.jsx(_,{spacing:1.5,children:l.map((v,p)=>e.jsx(Le,{row:v,onEdit:x,onDelete:m,users:s},v.id??p))})})})]})}function sr(){const[r,l]=u.useState([]),[x,m]=u.useState(!0),[s,c]=u.useState(""),[y,v]=u.useState(0),[p,I]=u.useState(5),[j,z]=u.useState(!1),[F,$]=u.useState(null),[S,E]=u.useState(null),[A,L]=u.useState(!1),[n,t]=u.useState(null),[d,W]=u.useState([]),w=()=>{let o=!1;return m(!0),Promise.all([we(),pe().catch(()=>[])]).then(([b,R])=>{o||(l(b.data||b||[]),W(R||[]))}).catch(b=>console.error("Error fetching data:",b)).finally(()=>{o||m(!1)}),()=>{o=!0}};u.useEffect(()=>w(),[]);const ce=async()=>{L(!0),t(null);try{await ue(S.id),E(null),w()}catch(o){t(o.message||"Failed to delete. Please try again.")}finally{L(!1)}},K=r.filter(o=>(o.company||"").toLowerCase().includes(s.toLowerCase())||(o.area||"").toLowerCase().includes(s.toLowerCase())||(o.phone||"").toLowerCase().includes(s.toLowerCase())),V=K.reduce((o,b)=>{const R=b.company||"Unknown Company";return o[R]||(o[R]=[]),o[R].push(b),o},{}),Z=Object.keys(V).sort(),Q=Z.slice(y*p,y*p+p),P={total:r.length,quoted:r.filter(o=>o.rate_per_kg&&!String(o.rate_per_kg).toLowerCase().includes("not answered")).length,missing:r.filter(o=>!o.rate_per_kg||String(o.rate_per_kg).toLowerCase().includes("not answered")).length};return e.jsxs(e.Fragment,{children:[e.jsx(Se,{title:"Field Surveys",breadcrumbs:[{label:"Survey Management"}]}),e.jsxs(i,{container:!0,spacing:2.5,sx:{mb:3},children:[e.jsx(i,{item:!0,xs:12,md:4,children:e.jsx(U,{accent:!0,title:"Total Enquiries",value:x?"…":P.total,icon:D,color:"primary",caption:"All field surveys"})}),e.jsx(i,{item:!0,xs:12,md:4,children:e.jsx(U,{accent:!0,title:"Rates Quoted",value:x?"…":P.quoted,icon:H,color:"success",caption:P.total?`${Math.round(P.quoted/P.total*100)}% of total`:"0% of total"})}),e.jsx(i,{item:!0,xs:12,md:4,children:e.jsx(U,{accent:!0,title:"Missing Info",value:x?"…":P.missing,icon:se,color:"warning",caption:P.total?`${Math.round(P.missing/P.total*100)}% of total`:"0% of total"})})]}),e.jsxs(le,{sx:{overflow:"hidden"},children:[e.jsxs(h,{sx:{px:{xs:2,sm:2.5},py:2,borderBottom:1,borderColor:"divider",display:"flex",alignItems:"center",gap:1.5,background:o=>`linear-gradient(90deg, ${o.palette.primary.lighter}66 0%, ${o.palette.background.paper} 70%)`},children:[e.jsx(h,{sx:{width:40,height:40,borderRadius:2,bgcolor:"primary.lighter",color:"primary.main",display:"inline-flex",alignItems:"center",justifyContent:"center"},children:e.jsx(D,{fontSize:"small"})}),e.jsxs(h,{children:[e.jsx(a,{variant:"subtitle1",sx:{fontWeight:700,color:"grey.800",lineHeight:1.2},children:"Survey Records"}),e.jsx(a,{variant:"caption",color:"text.secondary",children:"Manage business enquiry and survey data from the field"})]})]}),e.jsxs(_,{direction:{xs:"column",md:"row"},spacing:1.5,sx:{p:2},alignItems:{md:"center"},children:[e.jsx(g,{size:"small",placeholder:"Search by company, area or phone...",value:s,onChange:o=>{c(o.target.value),v(0)},sx:{width:{xs:"100%",md:300}},InputProps:{startAdornment:e.jsx(Ie,{position:"start",children:e.jsx(xe,{fontSize:"small"})})}}),e.jsx(h,{sx:{flexGrow:1}}),!x&&e.jsxs(a,{variant:"body2",color:"text.secondary",children:[K.length," records"]}),e.jsx(k,{variant:"contained",startIcon:e.jsx(q,{}),onClick:()=>{$(null),z(!0)},sx:{flex:{xs:1,md:"none"}},children:"Add New Survey"})]}),e.jsx(h,{sx:{p:2,bgcolor:"grey.50"},children:x?e.jsx(h,{sx:{display:"flex",justifyContent:"center",py:6},children:e.jsx(J,{})}):Q.length>0?e.jsx(h,{children:Q.map(o=>e.jsx(Be,{companyName:o,branches:V[o],onEdit:b=>{$(b),z(!0)},onDelete:b=>E(b),users:d},o))}):e.jsx(ke,{icon:D,title:"No survey records found.",caption:"Try adjusting your search filters."})}),e.jsx(fe,{component:"div",count:Z.length,page:y,onPageChange:(o,b)=>v(b),rowsPerPage:p,onRowsPerPageChange:o=>{I(parseInt(o.target.value,10)),v(0)},rowsPerPageOptions:[5,10,25],labelRowsPerPage:"Companies per page:"})]}),e.jsx($e,{open:j,onClose:()=>z(!1),onSave:()=>w(),initialData:F}),e.jsxs(te,{open:!!S,onClose:A?void 0:()=>{E(null),t(null)},children:[e.jsx(ie,{children:"Delete survey record?"}),e.jsxs(ne,{children:[e.jsxs(je,{children:["This will permanently remove the record for ",e.jsx("strong",{children:(S==null?void 0:S.area)||(S==null?void 0:S.company)}),". This cannot be undone."]}),n&&e.jsx(ae,{severity:"error",sx:{mt:2},children:n})]}),e.jsxs(oe,{sx:{px:3,py:2},children:[e.jsx(k,{onClick:()=>{E(null),t(null)},disabled:A,color:"inherit",children:"Cancel"}),e.jsx(k,{color:"error",variant:"contained",onClick:ce,disabled:A,startIcon:A?e.jsx(J,{size:16,color:"inherit"}):null,children:"Delete"})]})]})]})}export{sr as default};