import{r as xe,j as e,i as he,a as p,B as c,x as j,T as o,h as ge,S as f,C as X,c as ue,y as pe,A as me,m as se,M as _,I as M}from"./index-q3Tisdnr.js";import{d as fe}from"./EditOutlined-CZAx7RBQ.js";import{d as je}from"./DeleteOutline-D9KY4TEB.js";import{d as te,T as be,D as ye}from"./PhoneOutlined-B1Tyd8uC.js";import{c as G,d as ve,a as Se,b as U,e as Ce}from"./StorefrontOutlined-CL5n-1RH.js";import{d as _e}from"./MapOutlined-D-SVmliy.js";import{d as q,D as ne,a as oe,b as ie,c as le,e as ee}from"./Close-DL7iP88x.js";import{P as Ie,G as n,A as ae,C as A}from"./PageHeader-BdVI3_ZB.js";import{C as ce}from"./Link-CshtP915.js";import{T as m}from"./TextField-CVNMv7Sa.js";import{I as ke}from"./InputAdornment-NDKbvoTZ.js";import{B as k}from"./Button-BmCu8mk5.js";import{S as ze}from"./Snackbar-Cz2M_lq2.js";import"./TableCell-DV7KoMLs.js";var Y={},we=he;Object.defineProperty(Y,"__esModule",{value:!0});var D=Y.default=void 0,We=we(xe()),Pe=e;D=Y.default=(0,We.default)((0,Pe.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 L="https://api.doormile.com/api/v1",K=()=>{const r=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...r?{Authorization:`Bearer ${r}`}:{}}},Ee={textTransform:"uppercase",letterSpacing:.4,fontSize:"0.68rem",fontWeight:700,color:"grey.700"};function re({label:r,color:l="default"}){return r?e.jsx(A,{size:"small",label:r,sx:{fontWeight:600,...l==="default"?{bgcolor:"grey.100",color:"grey.700"}:{bgcolor:`${l}.lighter`,color:`${l}.dark`}}}):e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.500"},children:"—"})}function b({label:r,children:l}){return e.jsxs(c,{children:[e.jsx(o,{variant:"caption",color:"text.secondary",sx:Ee,children:r}),e.jsx(c,{sx:{mt:.5},children:l})]})}function H({title:r,value:l,icon:x,gradient:u,percentage:s}){return e.jsxs(c,{sx:{position:"relative",overflow:"hidden",borderRadius:3.5,p:3,color:"white",background:u,boxShadow:"0 10px 30px rgba(0,0,0,0.08)",transition:"box-shadow 0.3s cubic-bezier(0.4, 0, 0.2, 1)","&:hover":{boxShadow:"0 20px 40px rgba(0,0,0,0.12)"}},children:[e.jsxs(c,{sx:{position:"relative",zIndex:2},children:[e.jsxs(f,{direction:"row",justifyContent:"space-between",alignItems:"center",children:[e.jsx(o,{variant:"overline",sx:{fontWeight:700,letterSpacing:1.2,opacity:.9},children:r}),e.jsx(c,{sx:{width:36,height:36,borderRadius:"50%",bgcolor:"rgba(255,255,255,0.2)",display:"flex",alignItems:"center",justifyContent:"center",backdropFilter:"blur(4px)"},children:e.jsx(x,{sx:{fontSize:20}})})]}),e.jsxs(f,{direction:"row",alignItems:"baseline",spacing:1.5,sx:{mt:2},children:[e.jsx(o,{variant:"h3",sx:{fontWeight:800,lineHeight:1},children:l}),s&&e.jsx(A,{size:"small",label:s,sx:{height:22,bgcolor:"rgba(255,255,255,0.25)",color:"white",fontWeight:700,borderRadius:1.5,backdropFilter:"blur(4px)"}})]})]}),e.jsx(x,{sx:{position:"absolute",right:-24,bottom:-24,fontSize:160,opacity:.1,zIndex:1,transform:"rotate(-15deg)"}})]})}function F({icon:r,title:l,children:x}){return e.jsxs(c,{sx:{height:"100%",borderRadius:2,border:1,borderColor:"divider",bgcolor:"background.paper",overflow:"hidden"},children:[e.jsxs(f,{direction:"row",spacing:1,alignItems:"center",sx:{px:2,py:1.25,borderBottom:1,borderColor:"divider",bgcolor:j("#c01227",.08)},children:[e.jsx(r,{sx:{fontSize:18,color:"#c01227"}}),e.jsx(o,{variant:"overline",sx:{fontWeight:700,color:"grey.800",letterSpacing:.6,lineHeight:1},children:l})]}),e.jsx(f,{spacing:2,sx:{p:2},children:x})]})}async function Re(){const r=await fetch(`${L}/admin/competitor-branches?limit=1000`,{headers:K()});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 Ae(r){const l=!!r.id,x=l?`${L}/admin/competitor-branches/${r.id}`:`${L}/admin/competitor-branches`,s=await fetch(x,{method:l?"PUT":"POST",headers:K(),body:JSON.stringify(r)});if(!s.ok){const d=await s.json().catch(()=>({}));throw new Error(d.error||d.message||"Failed to save survey")}return s.status===204?{}:s.json().catch(()=>({}))}async function Te(r){const l=!!r.id,x=l?`${L}/admin/carrier-pricing/${r.id}`:`${L}/admin/carrier-pricing`,s=await fetch(x,{method:l?"PUT":"POST",headers:K(),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 d=await s.json().catch(()=>({}));throw new Error(d.error||d.message||"Failed to save pricing")}return s.status===204?{}:s.json().catch(()=>({}))}const Oe=["Local / Same","Within Tamilnadu","Interstate India"],De=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];function Le({open:r,onClose:l,onSave:x,initialData:u}){const[s,d]=p.useState({}),[y,C]=p.useState(!1),[g,z]=p.useState(null);p.useEffect(()=>{r&&(d(u?{...u,pincodes:u.pincodes?u.pincodes.split(",").map(i=>i.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:[]}),z(null))},[r,u]);const v=i=>t=>{d(h=>({...h,[i]:t.target.value}))},W=(i,t)=>h=>{const P=[...s.slabs||[]];P[i]={...P[i],[t]:h.target.value},d(T=>({...T,slabs:P}))},N=()=>{d(i=>({...i,slabs:[...i.slabs||[],{weight_slab:"",zone:"",service_type:"",rate:""}]}))},$=i=>{const t=[...s.slabs||[]];t.splice(i,1),d(h=>({...h,slabs:t}))},I=i=>t=>{const h=[...s.pincodes||[]];h[i]=t.target.value,d(P=>({...P,pincodes:h}))},E=()=>{d(i=>({...i,pincodes:[...i.pincodes||[],""]}))},R=i=>{const t=[...s.pincodes||[]];t.splice(i,1),d(h=>({...h,pincodes:t}))},B=async()=>{C(!0),z(null);try{if(s.pincodes&&s.pincodes.length>0){const t=s.pincodes.filter(h=>h.trim()&&!/^\d{6}$/.test(h.trim()));if(t.length>0)throw new Error(`Invalid pincodes detected: ${t.join(", ")}. All pincodes must be exactly 6 numeric digits.`)}const i={...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 Ae(i),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(i){console.error(i),z(i.message||"Failed to save. Please try again.")}finally{C(!1)}};return e.jsxs(e.Fragment,{children:[e.jsx(ze,{open:!!g,autoHideDuration:6e3,onClose:()=>z(null),anchorOrigin:{vertical:"top",horizontal:"right"},sx:{zIndex:9999},children:e.jsx(ae,{onClose:()=>z(null),severity:"error",variant:"filled",sx:{width:"100%",boxShadow:3},children:g})}),e.jsxs(ne,{open:r,onClose:y?void 0:l,maxWidth:"sm",fullWidth:!0,children:[e.jsx(oe,{sx:{fontWeight:800},children:u?"Edit Survey Record":"Add New Survey"}),e.jsx(ie,{dividers:!0,children:e.jsxs(n,{container:!0,spacing:2,children:[e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsx(m,{fullWidth:!0,label:"Company",value:s.company||"",onChange:v("company"),size:"small"})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsx(m,{fullWidth:!0,label:"Area / Zone",value:s.area||"",onChange:v("area"),size:"small"})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsx(m,{fullWidth:!0,label:"Phone",value:s.phone||"",onChange:v("phone"),size:"small"})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsx(m,{fullWidth:!0,label:"Rate Per KG",value:s.rate_per_kg||"",onChange:v("rate_per_kg"),size:"small"})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsxs(m,{select:!0,fullWidth:!0,label:"Offers Pickup",value:s.offers_pickup||"no",onChange:v("offers_pickup"),size:"small",children:[e.jsx(_,{value:"yes",children:"Yes"}),e.jsx(_,{value:"no",children:"No"})]})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsxs(m,{select:!0,fullWidth:!0,label:"Offers Drop",value:s.offers_drop||"no",onChange:v("offers_drop"),size:"small",children:[e.jsx(_,{value:"yes",children:"Yes"}),e.jsx(_,{value:"no",children:"No"})]})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsx(m,{fullWidth:!0,label:"Time in Days",value:s.time_in_days||"",onChange:v("time_in_days"),size:"small"})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsx(m,{fullWidth:!0,label:"Packing Charge",value:s.packing_charge||"",onChange:v("packing_charge"),size:"small"})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsxs(m,{select:!0,fullWidth:!0,label:"Frequency",value:s.frequency||"",onChange:v("frequency"),size:"small",children:[e.jsx(_,{value:"Daily",children:"Daily"}),e.jsx(_,{value:"Weekly",children:"Weekly"}),e.jsx(_,{value:"Bi-weekly",children:"Bi-weekly"}),e.jsx(_,{value:"Monthly",children:"Monthly"}),e.jsx(_,{value:"On-demand",children:"On-demand"})]})}),e.jsx(n,{item:!0,xs:12,children:e.jsx(m,{fullWidth:!0,label:"Plus Code",value:s.plus_code||"",onChange:v("plus_code"),size:"small"})}),e.jsx(n,{item:!0,xs:12,children:e.jsxs(c,{sx:{mt:1,pt:2,borderTop:"1px dashed",borderColor:"divider"},children:[e.jsxs(c,{sx:{display:"flex",justifyContent:"space-between",alignItems:"center",mb:2},children:[e.jsx(o,{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((i,t)=>e.jsxs(n,{container:!0,spacing:1.5,sx:{mb:1.5,alignItems:"center"},children:[e.jsx(n,{item:!0,xs:10,sm:11,children:e.jsx(m,{fullWidth:!0,label:`Pincode ${t+1}`,value:i,onChange:I(t),size:"small",placeholder:"e.g. 600001",inputProps:{maxLength:6}})}),e.jsx(n,{item:!0,xs:2,sm:1,children:e.jsx(M,{size:"small",color:"error",onClick:()=>R(t),children:e.jsx(ee,{fontSize:"small"})})})]},t)),(!s.pincodes||s.pincodes.length===0)&&e.jsx(o,{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(n,{item:!0,xs:12,children:e.jsx(m,{fullWidth:!0,multiline:!0,rows:2,label:"Address",value:s.address||"",onChange:v("address"),size:"small"})}),e.jsx(n,{item:!0,xs:12,children:e.jsxs(c,{sx:{mt:1,pt:2,borderTop:"1px dashed",borderColor:"divider"},children:[e.jsxs(c,{sx:{display:"flex",justifyContent:"space-between",alignItems:"center",mb:2},children:[e.jsx(o,{variant:"subtitle2",sx:{fontWeight:700,color:"primary.main",letterSpacing:.5},children:"PRICING SLABS (OPTIONAL)"}),e.jsx(k,{size:"small",startIcon:e.jsx(q,{}),onClick:N,sx:{fontWeight:600},children:"Add Slab"})]}),(s.slabs||[]).map((i,t)=>e.jsxs(n,{container:!0,spacing:1.5,sx:{mb:1.5,alignItems:"center"},children:[e.jsx(n,{item:!0,xs:6,sm:3,children:e.jsx(m,{select:!0,fullWidth:!0,label:"Weight Slab",value:i.weight_slab||"",onChange:W(t,"weight_slab"),size:"small",children:De.map(h=>e.jsx(_,{value:h,children:h},h))})}),e.jsx(n,{item:!0,xs:6,sm:3,children:e.jsx(m,{select:!0,fullWidth:!0,label:"Zone",value:i.zone||"",onChange:W(t,"zone"),size:"small",children:Oe.map(h=>e.jsx(_,{value:h,children:h},h))})}),e.jsx(n,{item:!0,xs:6,sm:3,children:e.jsx(m,{fullWidth:!0,label:"Service Type",value:i.service_type||"",onChange:W(t,"service_type"),size:"small",placeholder:"e.g. Air"})}),e.jsx(n,{item:!0,xs:4,sm:2,children:e.jsx(m,{fullWidth:!0,label:"Rate (₹)",value:i.rate||"",onChange:W(t,"rate"),size:"small",placeholder:"e.g. 25"})}),e.jsx(n,{item:!0,xs:2,sm:1,children:e.jsx(M,{size:"small",color:"error",onClick:()=>$(t),children:e.jsx(ee,{fontSize:"small"})})})]},t)),(!s.slabs||s.slabs.length===0)&&e.jsx(o,{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(le,{sx:{p:2,bgcolor:"grey.50"},children:[e.jsx(k,{onClick:l,disabled:y,color:"inherit",children:"Cancel"}),e.jsx(k,{onClick:B,disabled:y,variant:"contained",sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2},children:y?"Saving...":"Save Record"})]})]})]})}function $e({row:r,onEdit:l,onDelete:x,users:u}){const[s,d]=p.useState(!1),y=u.find(g=>g.id===r.created_by),C=u.find(g=>g.id===r.updated_by);return e.jsxs(c,{sx:{p:2,borderRadius:2.5,border:"1px solid",borderColor:"divider",bgcolor:"background.paper",transition:"border-color 0.2s ease, box-shadow 0.2s ease","&:hover":{borderColor:"#c01227",boxShadow:"0 4px 12px rgba(192, 18, 39, 0.05)"}},children:[e.jsxs(f,{direction:{xs:"column",md:"row"},spacing:3,alignItems:{xs:"flex-start",md:"center"},justifyContent:"space-between",onClick:()=>d(!s),sx:{cursor:"pointer",userSelect:"none"},children:[e.jsxs(f,{direction:"row",spacing:2,alignItems:"center",sx:{minWidth:240},children:[e.jsx(c,{sx:{p:1.5,borderRadius:2,bgcolor:j("#c01227",.05)},children:e.jsx(U,{sx:{color:"#c01227"}})}),e.jsxs(c,{children:[e.jsx(o,{variant:"subtitle1",sx:{fontWeight:700,color:"grey.900",lineHeight:1.2},children:r.area||"Unknown Area"}),e.jsxs(o,{variant:"body2",sx:{color:"grey.600",display:"flex",alignItems:"center",gap:.5,mt:.5},children:[e.jsx(te,{sx:{fontSize:14}})," ",r.phone||"—"]})]})]}),e.jsxs(f,{direction:"row",spacing:4,sx:{flex:1},justifyContent:{xs:"flex-start",md:"center"},children:[e.jsxs(c,{children:[e.jsx(o,{variant:"caption",sx:{color:"grey.500",fontWeight:700,letterSpacing:.5,display:"block",mb:.5},children:"RATE PER KG"}),e.jsx(re,{label:r.rate_per_kg,color:"success"})]}),e.jsxs(c,{children:[e.jsx(o,{variant:"caption",sx:{color:"grey.500",fontWeight:700,letterSpacing:.5,display:"block",mb:.5},children:"LOGISTICS"}),e.jsx(o,{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(f,{direction:"row",spacing:1,alignItems:"center",children:[e.jsx(k,{size:"small",sx:{borderRadius:1.5,color:s?"#c01227":"grey.700",bgcolor:s?j("#c01227",.05):"transparent","&:hover":{bgcolor:j("#c01227",.1),color:"#c01227"}},children:s?"Hide Info":"More Info"}),e.jsx(M,{size:"small",onClick:g=>{g.stopPropagation(),l(r)},sx:{color:"grey.400","&:hover":{color:"#c01227",bgcolor:j("#c01227",.1)}},children:e.jsx(fe,{fontSize:"small"})}),e.jsx(M,{size:"small",onClick:g=>{g.stopPropagation(),x(r)},sx:{color:"grey.400","&:hover":{color:"#ef4444",bgcolor:j("#ef4444",.1)}},children:e.jsx(je,{fontSize:"small"})})]})]}),e.jsx(se,{in:s,timeout:"auto",unmountOnExit:!0,children:e.jsx(c,{sx:{mt:2.5,pt:2.5,borderTop:"1px dashed",borderColor:"divider"},children:e.jsxs(n,{container:!0,spacing:2,alignItems:"stretch",children:[e.jsx(n,{item:!0,xs:12,md:4,children:e.jsxs(F,{icon:G,title:"ENQUIRY DETAILS",children:[e.jsx(b,{label:"RATE PER KG",children:e.jsx(re,{label:r.rate_per_kg,color:"success"})}),e.jsx(b,{label:"PICKUP",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:r.offers_pickup?"grey.800":"grey.500"},children:r.offers_pickup||"—"})}),e.jsx(b,{label:"DROP",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:r.offers_drop?"grey.800":"grey.500"},children:r.offers_drop||"—"})}),e.jsx(b,{label:"PACKING",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:r.packing_charge?"grey.800":"grey.500"},children:r.packing_charge||"—"})})]})}),e.jsx(n,{item:!0,xs:12,md:4,children:e.jsxs(F,{icon:Ce,title:"LOGISTICS & OPS",children:[e.jsx(b,{label:"TIME IN DAYS",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:r.time_in_days?"grey.800":"grey.500"},children:r.time_in_days||"—"})}),e.jsx(b,{label:"COMPANY",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:r.company||"—"})}),e.jsx(b,{label:"FREQUENCY",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:r.frequency||"—"})}),e.jsx(b,{label:"CONTACT NUMBER",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:r.phone||"—"})})]})}),e.jsx(n,{item:!0,xs:12,md:4,children:e.jsxs(F,{icon:U,title:"LOCATION & PINCODE",children:[e.jsx(b,{label:"AREA / ZONE",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:r.area||"—"})}),e.jsx(b,{label:"SERVICEABLE PINCODES",children:r.pincodes?e.jsx(c,{sx:{display:"flex",gap:.5,flexWrap:"wrap",mt:.5},children:r.pincodes.split(",").map((g,z)=>e.jsx(A,{label:g.trim(),size:"small",sx:{fontSize:"0.7rem",height:20}},z))}):e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.500"},children:"—"})}),e.jsx(b,{label:"PLUS CODE",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:r.plus_code?"grey.800":"grey.500"},children:r.plus_code||"—"})}),e.jsx(b,{label:"FULL ADDRESS",children:e.jsxs(f,{spacing:1.5,children:[e.jsxs(f,{direction:"row",spacing:1,sx:{p:1.5,borderRadius:1.5,bgcolor:"grey.50",border:1,borderColor:"divider"},children:[e.jsx(U,{sx:{fontSize:16,color:"grey.400",mt:"2px"}}),e.jsx(o,{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(_e,{sx:{fontSize:16}}),sx:{alignSelf:"flex-start",py:.5,px:1.5,fontSize:"0.75rem",fontWeight:600,borderRadius:2,color:"#c01227",borderColor:j("#c01227",.5),bgcolor:j("#c01227",.05),"&:hover":{borderColor:"#c01227",bgcolor:j("#c01227",.1)}},children:"View on map"})]})})]})}),e.jsx(n,{item:!0,xs:12,children:e.jsx(F,{icon:D,title:"RECORD METADATA",children:e.jsxs(n,{container:!0,spacing:2,children:[e.jsx(n,{item:!0,xs:6,children:e.jsx(b,{label:"CREATED BY",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:y?y.first_name:r.created_by?`User ID: ${r.created_by}`:"System"})})}),e.jsx(n,{item:!0,xs:6,children:e.jsx(b,{label:"LAST EDITED BY",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:C?C.first_name:r.updated_by?`User ID: ${r.updated_by}`:"—"})})})]})})})]})})})]})}function Be({companyName:r,branches:l,onEdit:x,onDelete:u,users:s}){const[d,y]=p.useState(!1);return e.jsxs(ce,{elevation:0,sx:{borderRadius:3,border:"1px solid",borderColor:d?"#c01227":"grey.200",p:{xs:2.5,md:3},mb:2.5,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:"#c01227",boxShadow:"0 8px 30px rgba(192, 18, 39, 0.08)"}},children:[e.jsxs(f,{direction:{xs:"column",md:"row"},spacing:3,alignItems:{xs:"flex-start",md:"center"},justifyContent:"space-between",onClick:()=>y(!d),sx:{cursor:"pointer",userSelect:"none"},children:[e.jsxs(f,{direction:"row",spacing:2.5,alignItems:"center",children:[e.jsx(me,{sx:{width:64,height:64,bgcolor:j("#c01227",.1),color:"#c01227",fontWeight:800,fontSize:"1.5rem",borderRadius:2.5},children:(r||"A")[0].toUpperCase()}),e.jsxs(c,{children:[e.jsx(o,{variant:"h5",sx:{fontWeight:800,lineHeight:1.2,color:"grey.900"},children:r||"Unknown Company"}),e.jsxs(o,{variant:"body2",sx:{color:"grey.600",mt:.5,display:"flex",alignItems:"center",gap:.5},children:[e.jsx(G,{sx:{fontSize:16}})," ",l.length," Location",l.length!==1&&"s"," Surveyed"]})]})]}),e.jsxs(f,{direction:"row",spacing:3,alignItems:"center",children:[e.jsxs(f,{direction:"row",spacing:1,sx:{display:{xs:"none",md:"flex"}},children:[e.jsx(A,{size:"small",label:"First Mile",sx:{bgcolor:j("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(A,{size:"small",label:"Mid Mile",sx:{bgcolor:j("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(A,{size:"small",label:"Last Mile",sx:{bgcolor:j("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}})]}),e.jsx(f,{direction:"row",spacing:1.5,alignItems:"center",children:e.jsx(k,{sx:{borderRadius:2,py:1,px:3,fontWeight:700,color:d?"#c01227":"grey.700",bgcolor:d?j("#c01227",.1):"grey.100","&:hover":{bgcolor:j("#c01227",.15),color:"#c01227"}},endIcon:d?e.jsx(ve,{}):e.jsx(Se,{}),children:d?"Hide Locations":"View Locations"})})]})]}),e.jsx(se,{in:d,timeout:"auto",unmountOnExit:!0,children:e.jsx(c,{sx:{mt:3,pt:3,borderTop:"2px dashed",borderColor:"grey.200"},children:e.jsx(f,{spacing:2,children:l.map((C,g)=>e.jsx($e,{row:C,onEdit:x,onDelete:u,users:s},C.id??g))})})})]})}function er(){const[r,l]=p.useState([]),[x,u]=p.useState(!0),[s,d]=p.useState(""),[y,C]=p.useState(0),[g,z]=p.useState(5),[v,W]=p.useState(!1),[N,$]=p.useState(null),[I,E]=p.useState(null),[R,B]=p.useState(!1),[i,t]=p.useState(null),[h,P]=p.useState([]),T=()=>{let a=!1;return u(!0),Promise.all([Re(),ue().catch(()=>[])]).then(([S,O])=>{a||(l(S.data||S||[]),P(O||[]))}).catch(S=>console.error("Error fetching data:",S)).finally(()=>{a||u(!1)}),()=>{a=!0}};p.useEffect(()=>T(),[]);const de=async()=>{B(!0),t(null);try{await pe(I.id),E(null),T()}catch(a){t(a.message||"Failed to delete. Please try again.")}finally{B(!1)}},V=r.filter(a=>(a.company||"").toLowerCase().includes(s.toLowerCase())||(a.area||"").toLowerCase().includes(s.toLowerCase())||(a.phone||"").toLowerCase().includes(s.toLowerCase())),Q=V.reduce((a,S)=>{const O=S.company||"Unknown Company";return a[O]||(a[O]=[]),a[O].push(S),a},{}),Z=Object.keys(Q).sort(),J=Z.slice(y*g,y*g+g),w={total:r.length,quoted:r.filter(a=>a.rate_per_kg&&!String(a.rate_per_kg).toLowerCase().includes("not answered")).length,missing:r.filter(a=>!a.rate_per_kg||String(a.rate_per_kg).toLowerCase().includes("not answered")).length};return e.jsxs(c,{sx:{pb:6,bgcolor:"#f8fafc",px:{xs:2,md:4},pt:3,minHeight:"100vh"},children:[e.jsx(Ie,{title:"Field Surveys",breadcrumbs:[{label:"Survey Management"}]}),e.jsxs(n,{container:!0,spacing:3,sx:{mb:5,mt:1},children:[e.jsx(n,{item:!0,xs:12,md:4,children:e.jsx(H,{title:"TOTAL ENQUIRIES",value:x?"...":w.total,icon:D,gradient:"linear-gradient(135deg, #e11d48 0%, #c01227 100%)"})}),e.jsx(n,{item:!0,xs:12,md:4,children:e.jsx(H,{title:"RATES QUOTED",value:x?"...":w.quoted,icon:G,gradient:"linear-gradient(135deg, #10b981 0%, #047857 100%)",percentage:w.total?`${Math.round(w.quoted/w.total*100)}%`:"0%"})}),e.jsx(n,{item:!0,xs:12,md:4,children:e.jsx(H,{title:"MISSING INFO",value:x?"...":w.missing,icon:te,gradient:"linear-gradient(135deg, #334155 0%, #0f172a 100%)",percentage:w.total?`${Math.round(w.missing/w.total*100)}%`:"0%"})})]}),e.jsxs(ce,{sx:{borderRadius:2,boxShadow:"0 4px 20px rgba(0,0,0,0.05)",overflow:"hidden"},children:[e.jsxs(c,{sx:{p:3,display:"flex",alignItems:"center",gap:2,borderBottom:"1px solid",borderColor:"grey.100"},children:[e.jsx(c,{sx:{width:48,height:48,borderRadius:2.5,bgcolor:j("#c01227",.1),color:"#c01227",display:"flex",alignItems:"center",justifyContent:"center"},children:e.jsx(D,{})}),e.jsxs(c,{children:[e.jsx(o,{variant:"h6",sx:{fontWeight:700,color:"grey.900"},children:"Survey Records"}),e.jsx(o,{variant:"body2",sx:{color:"grey.600",mt:.5},children:"Manage business enquiry and survey data from the field"})]})]}),e.jsxs(c,{sx:{p:2,borderBottom:"1px solid",borderColor:"grey.100",display:"flex",alignItems:"center",justifyContent:"space-between"},children:[e.jsx(m,{size:"small",placeholder:"Search by company, area or phone...",value:s,onChange:a=>{d(a.target.value),C(0)},sx:{width:{xs:"100%",md:400}},InputProps:{startAdornment:e.jsx(ke,{position:"start",children:e.jsx(ge,{fontSize:"small"})})}}),e.jsxs(f,{direction:"row",spacing:2,alignItems:"center",children:[!x&&e.jsxs(o,{variant:"body2",sx:{color:"grey.500",fontWeight:600},children:[V.length," records"]}),e.jsx(k,{variant:"contained",startIcon:e.jsx(q,{}),onClick:()=>{$(null),W(!0)},sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2,px:2,fontWeight:700,whiteSpace:"nowrap"},children:"Add New Survey"})]})]}),e.jsx(c,{sx:{p:3,bgcolor:"grey.50"},children:x?e.jsx(c,{sx:{display:"flex",justifyContent:"center",py:8},children:e.jsx(X,{})}):J.length>0?e.jsx(c,{children:J.map(a=>e.jsx(Be,{companyName:a,branches:Q[a],onEdit:S=>{$(S),W(!0)},onDelete:S=>E(S),users:h},a))}):e.jsxs(c,{sx:{py:10,textAlign:"center"},children:[e.jsx(D,{sx:{fontSize:48,color:"grey.300",mb:2}}),e.jsx(o,{variant:"h6",color:"text.secondary",children:"No survey records found."}),e.jsx(o,{variant:"body2",color:"text.disabled",sx:{mt:1},children:"Try adjusting your search filters."})]})}),e.jsx(c,{sx:{borderTop:"1px solid",borderColor:"grey.200",bgcolor:"white"},children:e.jsx(be,{component:"div",count:Z.length,page:y,onPageChange:(a,S)=>C(S),rowsPerPage:g,onRowsPerPageChange:a=>{z(parseInt(a.target.value,10)),C(0)},rowsPerPageOptions:[5,10,25],labelRowsPerPage:"Companies per page:"})})]}),e.jsx(Le,{open:v,onClose:()=>W(!1),onSave:()=>T(),initialData:N}),e.jsxs(ne,{open:!!I,onClose:R?void 0:()=>{E(null),t(null)},children:[e.jsx(oe,{children:"Delete survey record?"}),e.jsxs(ie,{children:[e.jsxs(ye,{children:["This will permanently remove the record for ",e.jsx("strong",{children:(I==null?void 0:I.area)||(I==null?void 0:I.company)}),". This cannot be undone."]}),i&&e.jsx(ae,{severity:"error",sx:{mt:2},children:i})]}),e.jsxs(le,{sx:{px:3,py:2},children:[e.jsx(k,{onClick:()=>{E(null),t(null)},disabled:R,color:"inherit",children:"Cancel"}),e.jsx(k,{color:"error",variant:"contained",onClick:de,disabled:R,startIcon:R?e.jsx(X,{size:16,color:"inherit"}):null,children:"Delete"})]})]})]})}export{er as default};