import{r as M,j as e,i as N,a as h,S as p,B as d,T as o,H as ge,C as me,I as B,K as Z,L as je,N as fe}from"./index-DZwdj7Pl.js";import{d as ye,D as be,a as ve,b as _e,c as Ce,e as J,f as Se}from"./DeleteOutline-Bm5lfgKP.js";import{d as ke,a as ze,b as F,c as $e}from"./StorefrontOutlined-DMIj4EXQ.js";import{T as Pe,d as We}from"./PhoneOutlined-Bh6Y12dw.js";import{d as Re}from"./LocalShippingOutlined-DOPA6ldB.js";import{P as Te,G as c,C as _}from"./PageHeader-B7PKwqPE.js";import{E as we}from"./EmptyState-BRMKqYw7.js";import{B as R}from"./Button-BiqOMFHI.js";import{C as Ie}from"./Link-COsQmKmg.js";import{T as u}from"./TextField-sSppbuZW.js";import{I as De}from"./InputAdornment-BzECKCIc.js";import{T as X,a as Y,b as Q,c as C,d as i,e as ee}from"./TableRow-Bbdtig1r.js";var q={},Ee=N;Object.defineProperty(q,"__esModule",{value:!0});var H=q.default=void 0,Le=Ee(M()),Be=e;H=q.default=(0,Le.default)((0,Be.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");var U={},Fe=N;Object.defineProperty(U,"__esModule",{value:!0});var re=U.default=void 0,Oe=Fe(M()),Ae=e;re=U.default=(0,Oe.default)((0,Ae.jsx)("path",{d:"M23 8c0 1.1-.9 2-2 2-.18 0-.35-.02-.51-.07l-3.56 3.55c.05.16.07.34.07.52 0 1.1-.9 2-2 2s-2-.9-2-2c0-.18.02-.36.07-.52l-2.55-2.55c-.16.05-.34.07-.52.07s-.36-.02-.52-.07l-4.55 4.56c.05.16.07.33.07.51 0 1.1-.9 2-2 2s-2-.9-2-2 .9-2 2-2c.18 0 .35.02.51.07l4.56-4.55C8.02 9.36 8 9.18 8 9c0-1.1.9-2 2-2s2 .9 2 2c0 .18-.02.36-.07.52l2.55 2.55c.16-.05.34-.07.52-.07s.36.02.52.07l3.55-3.56C19.02 8.35 19 8.18 19 8c0-1.1.9-2 2-2s2 .9 2 2"}),"TimelineOutlined");var V={},He=N;Object.defineProperty(V,"__esModule",{value:!0});var te=V.default=void 0,Me=He(M()),Ne=e;te=V.default=(0,Me.default)((0,Ne.jsx)("path",{d:"M22 8.98V18c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2l.01-12c0-1.1.89-2 1.99-2h10.1c-.06.32-.1.66-.1 1s.04.68.1 1H4l8 5 3.67-2.29c.47.43 1.02.76 1.63.98L12 13 4 8v10h16V9.9c.74-.15 1.42-.48 2-.92M16 5c0 1.66 1.34 3 3 3s3-1.34 3-3-1.34-3-3-3-3 1.34-3 3"}),"MarkEmailUnreadOutlined");const z="https://api.doormile.com/api/v1",T=()=>{const r=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...r?{Authorization:`Bearer ${r}`}:{}}};async function K(r=1){const s=await fetch(`${z}/admin/competitor-branches?limit=200&page=${r}`,{headers:T()});if(!s.ok)throw new Error("Failed to fetch branches");return s.json()}async function qe(r){const{id:s,created_at:j,updated_at:x,...y}=r,v=s?`${z}/admin/competitor-branches/${s}`:`${z}/admin/competitor-branches`,f=await fetch(v,{method:s?"PUT":"POST",headers:T(),body:JSON.stringify(y)});if(!f.ok)throw new Error("Failed to save branch");return f.json()}async function Ue(r){const s=await fetch(`${z}/admin/competitor-branches/${r}`,{method:"DELETE",headers:T()});if(!s.ok)throw new Error("Failed to delete branch");return s.json()}async function Ve(){const r=await fetch(`${z}/admin/carrier-pricing?limit=500`,{headers:T()});if(!r.ok)throw new Error("Failed to fetch pricing");return r.json()}const Ge={id:null,company:"",area:"",phone:"",rate_per_kg:"",offers_pickup:"",offers_drop:"",time_in_days:"",packing_charge:"",plus_code:"",address:""},Ze={textTransform:"uppercase",letterSpacing:.4,fontSize:"0.68rem",fontWeight:700,color:"grey.700"};function k({label:r,color:s="default"}){return r==null||r===""?e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.500"},children:"—"}):e.jsx(_,{size:"small",label:r,sx:{fontWeight:600,...s==="default"?{bgcolor:"grey.100",color:"grey.700"}:{bgcolor:`${s}.lighter`,color:`${s}.dark`}}})}function b({label:r,children:s}){return e.jsxs(d,{children:[e.jsx(o,{variant:"caption",color:"text.secondary",sx:Ze,children:r}),e.jsx(d,{sx:{mt:.5},children:s})]})}function O({label:r,value:s}){return e.jsx(b,{label:r,children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:s?"grey.800":"grey.500",wordBreak:"break-word"},children:s||"—"})})}function W({icon:r,title:s,accent:j="primary",children:x}){return e.jsxs(d,{sx:{height:"100%",borderRadius:2,border:1,borderColor:"divider",bgcolor:"background.paper",overflow:"hidden",boxShadow:"0 2px 8px rgba(0,0,0,0.03)"},children:[e.jsxs(p,{direction:"row",spacing:1,alignItems:"center",sx:{px:2,py:1.5,borderBottom:1,borderColor:"divider",bgcolor:`${j}.lighter`,opacity:.999},children:[e.jsx(r,{sx:{fontSize:18,color:`${j}.main`}}),e.jsx(o,{variant:"overline",sx:{fontWeight:700,color:"grey.800",letterSpacing:.6,lineHeight:1},children:s})]}),e.jsx(p,{spacing:2,sx:{p:2},children:x})]})}function A({label:r,value:s,icon:j,color:x="primary"}){return e.jsx(d,{sx:{flex:1,p:2.5,borderRadius:2,border:1,borderColor:"divider",bgcolor:"background.paper",boxShadow:"0 4px 20px rgba(0,0,0,0.03)"},children:e.jsxs(p,{direction:"row",spacing:2,alignItems:"center",children:[e.jsx(d,{sx:{width:48,height:48,borderRadius:2,bgcolor:`${x}.lighter`,color:`${x}.main`,display:"inline-flex",alignItems:"center",justifyContent:"center"},children:e.jsx(j,{sx:{fontSize:24}})}),e.jsxs(d,{children:[e.jsx(o,{variant:"h4",sx:{fontWeight:800,color:"grey.800",lineHeight:1.1},children:s}),e.jsx(o,{variant:"body2",sx:{fontWeight:600,mt:.5},color:"text.secondary",children:r})]})]})})}function Je({row:r,open:s,onToggle:j,onEdit:x,onDelete:y,pricingData:v}){const f=(v||[]).filter(l=>l.company&&l.company.trim()&&l.company.toLowerCase()===(r.company||"").toLowerCase());return e.jsxs(e.Fragment,{children:[e.jsxs(C,{hover:!0,sx:{"& > *":{borderBottom:"unset"}},children:[e.jsx(i,{sx:{pl:1,width:48},children:e.jsx(B,{size:"small",onClick:j,sx:{color:"grey.500",bgcolor:s?"grey.100":"transparent","&:hover":{bgcolor:"grey.200"}},children:s?e.jsx(ke,{}):e.jsx(ze,{})})}),e.jsxs(i,{children:[e.jsx(o,{variant:"body2",sx:{fontWeight:600,color:"grey.800"},children:r.area||"—"}),r.company&&e.jsx(o,{variant:"caption",color:"text.secondary",children:r.company})]}),e.jsx(i,{children:e.jsxs(p,{direction:"row",spacing:.75,alignItems:"center",children:[e.jsx(We,{sx:{fontSize:15,color:"grey.400"}}),e.jsx(o,{variant:"body2",children:r.phone||"—"})]})}),e.jsx(i,{children:e.jsxs(p,{direction:"row",spacing:.75,alignItems:"flex-start",children:[e.jsx(F,{sx:{fontSize:16,color:"grey.400",mt:.2}}),e.jsx(o,{variant:"body2",sx:{maxWidth:250,display:"-webkit-box",WebkitLineClamp:1,WebkitBoxOrient:"vertical",overflow:"hidden"},children:r.address||"—"})]})}),e.jsx(i,{children:e.jsx(o,{variant:"body2",sx:{fontWeight:600},children:r.rate_per_kg||"—"})}),e.jsx(i,{children:e.jsxs(p,{direction:"row",spacing:1,flexWrap:"wrap",children:[r.offers_pickup&&e.jsx(_,{size:"small",label:`Pickup: ${r.offers_pickup}`}),r.offers_drop&&e.jsx(_,{size:"small",label:`Drop: ${r.offers_drop}`}),r.time_in_days&&e.jsx(_,{size:"small",label:`Days: ${r.time_in_days}`}),r.packing_charge&&e.jsx(_,{size:"small",label:`Packing: ${r.packing_charge}`}),!r.offers_pickup&&!r.offers_drop&&!r.time_in_days&&!r.packing_charge&&e.jsx(o,{variant:"body2",color:"text.secondary",children:"—"})]})}),e.jsxs(i,{align:"right",children:[e.jsx(Z,{title:"Edit",children:e.jsx(B,{size:"small",onClick:l=>{l.stopPropagation(),x(r)},children:e.jsx(J,{fontSize:"small"})})}),e.jsx(Z,{title:"Delete",children:e.jsx(B,{size:"small",onClick:l=>{l.stopPropagation(),y(r)},children:e.jsx(Se,{fontSize:"small"})})})]})]}),e.jsx(C,{children:e.jsx(i,{colSpan:7,sx:{py:0,borderBottom:s?void 0:"none"},children:e.jsx(je,{in:s,timeout:"auto",unmountOnExit:!0,children:e.jsxs(d,{sx:{m:2,borderRadius:2.5,border:1,borderColor:"divider",overflow:"hidden",boxShadow:"0 4px 16px rgba(0,0,0,0.06)"},children:[e.jsxs(p,{direction:{xs:"column",sm:"row"},justifyContent:"space-between",alignItems:{xs:"flex-start",sm:"center"},spacing:1.5,sx:{px:2.5,py:1.75,borderBottom:1,borderColor:"divider",background:l=>`linear-gradient(90deg, ${l.palette.primary.lighter}88 0%, ${l.palette.background.paper} 75%)`},children:[e.jsxs(p,{direction:"row",spacing:1.5,alignItems:"center",children:[e.jsx(d,{sx:{width:40,height:40,borderRadius:"50%",bgcolor:"primary.main",color:"primary.contrastText",display:"flex",alignItems:"center",justifyContent:"center",fontWeight:700,fontSize:"1.2rem"},children:(r.company||r.area||"S")[0].toUpperCase()}),e.jsxs(d,{children:[e.jsx(o,{variant:"subtitle1",sx:{fontWeight:700,lineHeight:1.2},children:r.company||"Survey Record"}),e.jsx(o,{variant:"caption",color:"text.secondary",children:r.area||"Unknown Area"})]})]}),e.jsx(R,{size:"small",variant:"contained",startIcon:e.jsx(J,{}),onClick:()=>x(r),children:"Edit Survey"})]}),e.jsx(d,{sx:{p:2.5,bgcolor:"grey.50"},children:e.jsxs(c,{container:!0,spacing:2.5,alignItems:"stretch",children:[e.jsx(c,{item:!0,xs:12,md:4,children:e.jsxs(W,{icon:$e,title:"Enquiry Details",accent:"primary",children:[e.jsx(b,{label:"Rate per kg",children:e.jsx(k,{label:r.rate_per_kg,color:r.rate_per_kg&&r.rate_per_kg!=="Not answered"?"success":"default"})}),e.jsx(b,{label:"Pickup",children:e.jsx(k,{label:r.offers_pickup,color:"info"})}),e.jsx(b,{label:"Drop",children:e.jsx(k,{label:r.offers_drop,color:"info"})}),e.jsx(b,{label:"Packing",children:e.jsx(k,{label:r.packing_charge,color:"info"})})]})}),e.jsx(c,{item:!0,xs:12,md:4,children:e.jsxs(W,{icon:Re,title:"Logistics & Ops",accent:"primary",children:[e.jsx(b,{label:"Time in Days",children:e.jsx(k,{label:r.time_in_days,color:"primary"})}),e.jsx(O,{label:"Company",value:r.company}),e.jsx(O,{label:"Contact Number",value:r.phone})]})}),e.jsx(c,{item:!0,xs:12,md:4,children:e.jsxs(W,{icon:F,title:"Location & Pincode",accent:"primary",children:[e.jsx(b,{label:"Area / Zone",children:e.jsx(o,{variant:"body2",sx:{fontWeight:500,color:"grey.800"},children:r.area||"—"})}),e.jsx(O,{label:"Plus Code",value:r.plus_code}),e.jsx(b,{label:"Full Address",children:e.jsxs(p,{direction:"row",spacing:1,sx:{p:1.25,borderRadius:1.5,bgcolor:"grey.50",border:1,borderColor:"divider"},children:[e.jsx(F,{sx:{fontSize:16,color:"grey.400",mt:"2px"}}),e.jsx(o,{variant:"body2",sx:{color:"grey.800",lineHeight:1.5},children:r.address||"—"})]})})]})}),f.length>0&&e.jsx(c,{item:!0,xs:12,children:e.jsx(W,{icon:fe,title:"Logistics Rate Card",accent:"success",children:e.jsx(X,{sx:{border:1,borderColor:"divider",borderRadius:1.5,overflow:"hidden",mt:1},children:e.jsxs(Y,{size:"small",children:[e.jsx(Q,{sx:{bgcolor:"grey.100"},children:e.jsxs(C,{sx:{"& th":{fontWeight:600,color:"grey.700",fontSize:"0.75rem"}},children:[e.jsx(i,{children:"Weight Slab"}),e.jsx(i,{children:"Service"}),e.jsx(i,{children:"Zone"}),e.jsx(i,{children:"Rate (₹)"}),e.jsx(i,{children:"Delivery Time"})]})}),e.jsx(ee,{children:f.map(l=>e.jsxs(C,{hover:!0,children:[e.jsx(i,{sx:{fontWeight:500},children:l.weight_slab}),e.jsx(i,{children:e.jsx(_,{size:"small",label:l.service_type||"Standard",sx:{bgcolor:"primary.lighter",color:"primary.dark"}})}),e.jsx(i,{children:l.zone}),e.jsx(i,{sx:{fontWeight:600,color:"success.dark"},children:l.rate}),e.jsx(i,{sx:{color:"text.secondary"},children:l.delivery_time||"—"})]},l.id))})]})})})})]})})]})})})})]})}function or(){const[r,s]=h.useState([]),[j,x]=h.useState(0),[y,v]=h.useState(!0),[f,l]=h.useState(""),[w,I]=h.useState(0),[$,ae]=h.useState(5),[se,S]=h.useState(!1),[a,D]=h.useState(null),[ie,ne]=h.useState([]),[le,oe]=h.useState(null);h.useEffect(()=>{let t=!1;async function n(){v(!0);try{const m=await K(1),E=[...m.data||[]];if((m.total||0)>200){const L=await K(2);E.push(...L.data||[])}t||(s(E),x(m.total||E.length));try{const L=await Ve();t||ne(L.data||[])}catch{}}catch{t||s([])}finally{t||v(!1)}}return n(),()=>{t=!0}},[]);const P=r.filter(t=>{if(!f)return!0;const n=f.toLowerCase();return t.company&&t.company.toLowerCase().includes(n)||t.area&&t.area.toLowerCase().includes(n)||t.phone&&t.phone.includes(n)||t.address&&t.address.toLowerCase().includes(n)}),G=P.slice(w*$,w*$+$),ce=t=>{D({...t}),S(!0)},de=()=>{D({...Ge}),S(!0)},xe=async()=>{try{const t=await qe(a);a.id?s(n=>n.map(m=>m.id===a.id?t:m)):(s(n=>[t,...n]),x(n=>n+1)),S(!1)}catch{alert("Failed to save. Please try again.")}},he=async t=>{if(window.confirm("Delete this branch record?"))try{await Ue(t.id),s(n=>n.filter(m=>m.id!==t.id)),x(n=>n-1)}catch{alert("Failed to delete. Please try again.")}},g=t=>n=>D(m=>({...m,[t]:n.target.value})),ue=new Set(r.map(t=>t.company).filter(Boolean)).size,pe=r.filter(t=>t.rate_per_kg&&t.rate_per_kg!=="Not answered"&&t.rate_per_kg!=="").length;return e.jsxs(e.Fragment,{children:[e.jsx(Te,{title:"Field Surveys",breadcrumbs:[{label:"Surveys"}],action:e.jsx(R,{variant:"contained",startIcon:e.jsx(ye,{}),onClick:de,sx:{borderRadius:2,px:3,boxShadow:"0 4px 12px rgba(0,0,0,0.1)"},children:"New Survey"})}),e.jsxs(p,{direction:{xs:"column",md:"row"},spacing:2,sx:{mt:3,mb:1},children:[e.jsx(A,{label:"Total Surveys",value:y?"…":j,icon:H,color:"primary"}),e.jsx(A,{label:"Companies Surveyed",value:y?"…":ue,icon:re,color:"success"}),e.jsx(A,{label:"With Rate Info",value:y?"…":pe,icon:te,color:"warning"})]}),e.jsxs(Ie,{sx:{overflow:"hidden",mt:3,borderRadius:2.5,boxShadow:"0 8px 24px rgba(0,0,0,0.04)",border:1,borderColor:"grey.200"},children:[e.jsxs(d,{sx:{px:2.5,py:2,borderBottom:1,borderColor:"divider",display:"flex",alignItems:"center",gap:1.5,background:t=>`linear-gradient(90deg, ${t.palette.primary.lighter}66 0%, ${t.palette.background.paper} 70%)`},children:[e.jsx(d,{sx:{width:40,height:40,borderRadius:2,bgcolor:"primary.lighter",color:"primary.main",display:"inline-flex",alignItems:"center",justifyContent:"center"},children:e.jsx(H,{fontSize:"small"})}),e.jsxs(d,{children:[e.jsx(o,{variant:"subtitle1",sx:{fontWeight:700,color:"grey.800",lineHeight:1.2},children:"Survey Records"}),e.jsx(o,{variant:"caption",color:"text.secondary",children:"Manage business enquiry and survey data from the field"})]})]}),e.jsxs(p,{direction:{xs:"column",md:"row"},spacing:1.5,sx:{p:2},alignItems:{md:"center"},children:[e.jsx(u,{size:"small",placeholder:"Search by company, area, phone, or address…",value:f,onChange:t=>{l(t.target.value),I(0)},sx:{minWidth:300},InputProps:{startAdornment:e.jsx(De,{position:"start",children:e.jsx(ge,{fontSize:"small"})})}}),e.jsx(d,{sx:{flexGrow:1}}),e.jsxs(o,{variant:"body2",color:"text.secondary",children:[P.length," ",P.length===1?"record":"records"]})]}),y?e.jsx(d,{sx:{display:"flex",justifyContent:"center",py:6},children:e.jsx(me,{size:36})}):e.jsxs(e.Fragment,{children:[e.jsx(X,{children:e.jsxs(Y,{sx:{minWidth:1e3},children:[e.jsx(Q,{children:e.jsxs(C,{sx:{"& th":{bgcolor:"grey.50",fontWeight:700,color:"grey.700",textTransform:"uppercase",fontSize:"0.72rem",letterSpacing:.4}},children:[e.jsx(i,{sx:{width:48}}),e.jsx(i,{children:"Area / Company"}),e.jsx(i,{children:"Contact"}),e.jsx(i,{children:"Location"}),e.jsx(i,{children:"Rate / Pricing"}),e.jsx(i,{children:"Logistics"}),e.jsx(i,{align:"right",children:"Actions"})]})}),e.jsx(ee,{children:G.length===0?e.jsx(C,{children:e.jsx(i,{colSpan:7,sx:{border:"none"},children:e.jsx(we,{title:"No records found",caption:"Try a different search term or add a new survey."})})}):G.map(t=>e.jsx(Je,{row:t,open:le===t.id,onToggle:()=>oe(n=>n===t.id?null:t.id),onEdit:ce,onDelete:he,pricingData:ie},t.id))})]})}),e.jsx(Pe,{component:"div",count:P.length,page:w,onPageChange:(t,n)=>I(n),rowsPerPage:$,onRowsPerPageChange:t=>{ae(+t.target.value),I(0)},rowsPerPageOptions:[5,10,25]})]})]}),e.jsxs(be,{open:se,onClose:()=>S(!1),maxWidth:"md",fullWidth:!0,children:[e.jsx(ve,{children:a!=null&&a.id?"Edit Survey":"New Survey"}),e.jsx(_e,{dividers:!0,children:e.jsxs(c,{container:!0,spacing:2,children:[e.jsx(c,{item:!0,xs:12,sm:6,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Company",value:(a==null?void 0:a.company)||"",onChange:g("company")})}),e.jsx(c,{item:!0,xs:12,sm:6,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Area",value:(a==null?void 0:a.area)||"",onChange:g("area")})}),e.jsx(c,{item:!0,xs:12,sm:6,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Phone Number",value:(a==null?void 0:a.phone)||"",onChange:g("phone")})}),e.jsx(c,{item:!0,xs:12,sm:6,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Rate per kg",value:(a==null?void 0:a.rate_per_kg)||"",onChange:g("rate_per_kg")})}),e.jsx(c,{item:!0,xs:12,sm:3,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Pickup",value:(a==null?void 0:a.offers_pickup)||"",onChange:g("offers_pickup")})}),e.jsx(c,{item:!0,xs:12,sm:3,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Drop",value:(a==null?void 0:a.offers_drop)||"",onChange:g("offers_drop")})}),e.jsx(c,{item:!0,xs:12,sm:3,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Time in days",value:(a==null?void 0:a.time_in_days)||"",onChange:g("time_in_days")})}),e.jsx(c,{item:!0,xs:12,sm:3,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Packing charge",value:(a==null?void 0:a.packing_charge)||"",onChange:g("packing_charge")})}),e.jsx(c,{item:!0,xs:12,sm:6,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Plus code",value:(a==null?void 0:a.plus_code)||"",onChange:g("plus_code")})}),e.jsx(c,{item:!0,xs:12,children:e.jsx(u,{fullWidth:!0,size:"small",label:"Address",value:(a==null?void 0:a.address)||"",onChange:g("address"),multiline:!0,minRows:2})})]})}),e.jsxs(Ce,{sx:{p:2},children:[e.jsx(R,{onClick:()=>S(!1),children:"Cancel"}),e.jsx(R,{variant:"contained",onClick:xe,children:"Save Survey"})]})]})]})}export{or as default};