Files
doormile_crm_build/dist/assets/Survey-B2OopkeF.js
2026-07-08 18:53:29 +05:30

23 lines
24 KiB
JavaScript

import{c as J,r as h,j as e,S as Q,B as C,d as te,k as oe,T as Y,C as re,L as ie,l as ae,X as V}from"./index-TNj5auBr.js";import{P as le,B as de}from"./PageHeader-B9Ytwo9b.js";import{S as $}from"./StatCard-CeBP5QBY.js";import{E as K}from"./EmptyState-DGz1kDMS.js";import{T as m}from"./TextInput-DL-5979h.js";import{P as U,D as ce,L as pe,F as w,a as xe}from"./plus-CZCr3OEY.js";import{S as q,T as fe,M as he}from"./truck-DmPHp74_.js";import{P as X,A as ge}from"./phone-BqZPAT1U.js";import{C as ye}from"./Card-Cvh0wiAW.js";import{C as ee}from"./circle-check-DCMZ9tv4.js";import{I as ue}from"./IconButton-Dlo2bZzs.js";import{C as me}from"./chevron-up-CSuWANIv.js";import{B as be}from"./Banner-DVJsik2E.js";import{S as P}from"./Selector-D-0RMSQ0.js";import{T as je}from"./TextArea-B4wLAlyG.js";import{M}from"./map-pin-BQjktuvD.js";import{S as ve}from"./square-pen-BItzjjNj.js";import{T as Se}from"./trash-2-Mcf_1vVz.js";import"./HStack-tSPMb9QU.js";/**
* @license lucide-react v1.22.0 - ISC
*
* This source code is licensed under the ISC license.
* See the LICENSE file in the root directory of this source tree.
*/const Ce=[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["line",{x1:"12",x2:"12",y1:"8",y2:"12",key:"1pkeuh"}],["line",{x1:"12",x2:"12.01",y1:"16",y2:"16",key:"4dfq90"}]],se=J("circle-alert",Ce);/**
* @license lucide-react v1.22.0 - ISC
*
* This source code is licensed under the ISC license.
* See the LICENSE file in the root directory of this source tree.
*/const ke=[["path",{d:"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z",key:"1oefj6"}],["path",{d:"M14 2v5a1 1 0 0 0 1 1h5",key:"wfsgrz"}],["path",{d:"M10 9H8",key:"b1mrlr"}],["path",{d:"M16 13H8",key:"t4e002"}],["path",{d:"M16 17H8",key:"z1uh3a"}]],H=J("file-text",ke);function ne(s){return!!(s.rate_per_kg&&!String(s.rate_per_kg).toLowerCase().includes("not answered"))}const L="https://api.doormile.com/api/v1",G=()=>{const s=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...s?{Authorization:`Bearer ${s}`}:{}}};function Z({label:s,color:i="neutral",truncate:a=!1}){if(!s)return e.jsx("span",{style:{fontWeight:500,color:"#94a3b8"},children:"—"});const l={success:{bg:"#E3F6EC",fg:"#00773B"},warning:{bg:"#FFF7E0",fg:"#8A6500"},info:{bg:"#E0F7F8",fg:"#00727B"},error:{bg:"#FEEAE9",fg:"#A82216"},primary:{bg:"#F8E0E3",fg:"#9E0E20"},neutral:{bg:"#f1f5f9",fg:"#475569"}},n=l[i]||l.neutral;return e.jsx("span",{title:a?s:void 0,style:{display:"inline-block",maxWidth:a?"100%":void 0,overflow:a?"hidden":void 0,textOverflow:a?"ellipsis":void 0,whiteSpace:a?"nowrap":void 0,verticalAlign:a?"bottom":void 0,boxSizing:"border-box",padding:"2px 8px",borderRadius:"4px",fontSize:"0.75rem",fontWeight:600,backgroundColor:n.bg,color:n.fg},children:s})}function y({label:s,children:i}){return e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:"2px"},children:[e.jsx("span",{style:{textTransform:"uppercase",letterSpacing:"0.05em",fontSize:"0.62rem",fontWeight:700,color:"#475569"},children:s}),e.jsx("div",{children:i})]})}function O({icon:s,title:i,children:a}){return e.jsxs("div",{style:{height:"100%",borderRadius:"8px",border:"1px solid #e2e8f0",backgroundColor:"#ffffff",overflow:"hidden",display:"flex",flexDirection:"column"},children:[e.jsxs("div",{style:{display:"flex",flexDirection:"row",alignItems:"center",gap:"6px",padding:"6px 10px",borderBottom:"1px solid #e2e8f0",backgroundColor:"rgba(192, 18, 39, 0.08)"},children:[e.jsx(s,{size:12,style:{color:"#c01227"}}),e.jsx("span",{style:{fontWeight:700,color:"#1e293b",fontSize:"0.64rem",letterSpacing:"0.6px",textTransform:"uppercase"},children:i})]}),e.jsx("div",{style:{display:"flex",flexDirection:"column",gap:"8px",padding:"8px 10px"},children:a})]})}async function _e(){const s=await fetch(`${L}/admin/competitor-branches?limit=1000`,{headers:G()});if(!s.ok)throw new Error("Failed to fetch surveys");const i=await s.json(),a=Array.isArray(i)?i:i.data||[];return{data:a,total:a.length}}async function ze(s){const i=!!s.id,a=i?`${L}/admin/competitor-branches/${s.id}`:`${L}/admin/competitor-branches`,n=await fetch(a,{method:i?"PUT":"POST",headers:G(),body:JSON.stringify(s)});if(!n.ok){const d=await n.json().catch(()=>({}));throw new Error(d.error||d.message||"Failed to save survey")}return n.status===204?{}:n.json().catch(()=>({}))}async function we(s){const i=`${L}/admin/carrier-pricing`,l=await fetch(i,{method:"POST",headers:G(),body:JSON.stringify({company:s.company,weight_slab:s.weight_slab,zone:s.zone||"",service_type:s.service_type||"",rate:String(s.rate)})});if(!l.ok){const n=await l.json().catch(()=>({}));throw new Error(n.error||n.message||"Failed to save pricing")}return l.status===204?{}:l.json().catch(()=>({}))}const Te=["Local / Same","Within Tamilnadu","Interstate India"],Ee=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];function Ie({open:s,onClose:i,onSave:a,initialData:l}){const[n,d]=h.useState({}),[c,u]=h.useState(!1),[p,g]=h.useState(null);h.useEffect(()=>{s&&(d(l?{...l,pincodes:l.pincodes?l.pincodes.split(",").map(t=>t.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:[]}),g(null))},[s,l]);const x=t=>r=>{d(f=>({...f,[t]:r}))},b=(t,r)=>f=>{const k=[...n.slabs||[]];k[t]={...k[t],[r]:f},d(_=>({..._,slabs:k}))},T=()=>{d(t=>({...t,slabs:[...t.slabs||[],{weight_slab:"",zone:"",service_type:"",rate:""}]}))},z=t=>{const r=[...n.slabs||[]];r.splice(t,1),d(f=>({...f,slabs:r}))},j=t=>r=>{const f=[...n.pincodes||[]];f[t]=r,d(k=>({...k,pincodes:f}))},E=()=>{d(t=>({...t,pincodes:[...t.pincodes||[],""]}))},R=t=>{const r=[...n.pincodes||[]];r.splice(t,1),d(f=>({...f,pincodes:r}))},D=async()=>{u(!0),g(null);try{if(n.pincodes&&n.pincodes.length>0){const r=n.pincodes.filter(f=>f.trim()&&!/^\d{6}$/.test(f.trim()));if(r.length>0)throw new Error(`Invalid pincodes: ${r.join(", ")}. Must be 6 digits.`)}const t={...n.id?{id:n.id}:{},company:n.company||"",area:n.area||"",phone:n.phone||"",rate_per_kg:n.rate_per_kg||"",offers_pickup:n.offers_pickup||"no",offers_drop:n.offers_drop||"no",packing_charge:n.packing_charge||"",time_in_days:n.time_in_days||"",plus_code:n.plus_code||"",address:n.address||"",frequency:n.frequency||"",pincodes:n.pincodes?n.pincodes.join(","):""};if(await ze(t),n.slabs&&n.slabs.length>0&&n.company)try{await Promise.all(n.slabs.map(r=>r.weight_slab&&r.rate?we({company:n.company,weight_slab:r.weight_slab,zone:r.zone||"",service_type:r.service_type||"",rate:r.rate}):Promise.resolve()))}catch(r){console.error("Failed to save pricing slabs",r)}a(),i()}catch(t){console.error(t),g(t.message||"Failed to save. Please try again.")}finally{u(!1)}},A=[{value:"yes",label:"Yes"},{value:"no",label:"No"}],I=["Daily","Weekly","Bi-weekly","Monthly","On-demand"],W=Ee.map(t=>({value:t,label:t})),N=Te.map(t=>({value:t,label:t}));return e.jsx(ce,{isOpen:s,onOpenChange:t=>{t||i()},width:600,maxHeight:"90dvh",purpose:"form",children:e.jsx(ie,{header:e.jsx(xe,{title:l?"Edit Survey Record":"Add New Survey",onOpenChange:t=>{t||i()}}),content:e.jsx(ae,{children:e.jsxs(w,{children:[p&&e.jsx(be,{status:"error",title:p}),e.jsxs(w,{direction:"horizontal",children:[e.jsx(m,{label:"Company",value:n.company||"",onChange:x("company")}),e.jsx(m,{label:"Area / Zone",value:n.area||"",onChange:x("area")})]}),e.jsxs(w,{direction:"horizontal",children:[e.jsx(m,{label:"Phone",value:n.phone||"",onChange:x("phone")}),e.jsx(m,{label:"Rate Per KG",value:n.rate_per_kg||"",onChange:x("rate_per_kg")})]}),e.jsxs(w,{direction:"horizontal",children:[e.jsx(P,{label:"Offers Pickup",options:A,value:n.offers_pickup||"no",onChange:x("offers_pickup")}),e.jsx(P,{label:"Offers Drop",options:A,value:n.offers_drop||"no",onChange:x("offers_drop")})]}),e.jsxs(w,{direction:"horizontal",children:[e.jsx(m,{label:"Time in Days",value:n.time_in_days||"",onChange:x("time_in_days")}),e.jsx(m,{label:"Packing Charge",value:n.packing_charge||"",onChange:x("packing_charge")})]}),e.jsxs(w,{direction:"horizontal",children:[e.jsx(P,{label:"Frequency",placeholder:"Select Frequency",hasClear:!0,options:I,value:n.frequency||"",onChange:t=>x("frequency")(t??"")}),e.jsx(m,{label:"Plus Code",value:n.plus_code||"",onChange:x("plus_code")})]}),e.jsxs("div",{style:{borderTop:"1px dashed #e2e8f0",paddingTop:"16px"},children:[e.jsxs("div",{style:{display:"flex",justifyContent:"space-between",alignItems:"center",marginBottom:"12px"},children:[e.jsx("span",{style:{fontSize:"0.875rem",fontWeight:700,color:"#C01227"},children:"SERVICEABLE PINCODES"}),e.jsx(C,{size:"sm",variant:"secondary",icon:e.jsx(U,{size:12}),onClick:E,label:"Add Pincode"})]}),(n.pincodes||[]).map((t,r)=>e.jsxs("div",{style:{display:"flex",gap:"8px",alignItems:"flex-end",marginBottom:"8px"},children:[e.jsx("div",{style:{flexGrow:1},children:e.jsx(m,{label:`Pincode ${r+1}`,isLabelHidden:!0,placeholder:`Pincode ${r+1}`,value:t,onChange:j(r),maxLength:6})}),e.jsx("button",{onClick:()=>R(r),style:{background:"none",border:"none",cursor:"pointer",color:"#dc2626",padding:"8px"},children:e.jsx(V,{size:16})})]},r))]}),e.jsx(je,{label:"Address",rows:2,value:n.address||"",onChange:x("address")}),e.jsxs("div",{style:{borderTop:"1px dashed #e2e8f0",paddingTop:"16px"},children:[e.jsxs("div",{style:{display:"flex",justifyContent:"space-between",alignItems:"center",marginBottom:"12px"},children:[e.jsx("span",{style:{fontSize:"0.875rem",fontWeight:700,color:"#C01227"},children:"PRICING SLABS (OPTIONAL)"}),e.jsx(C,{size:"sm",variant:"secondary",icon:e.jsx(U,{size:12}),onClick:T,label:"Add Slab"})]}),(n.slabs||[]).map((t,r)=>e.jsxs("div",{style:{display:"flex",gap:"8px",alignItems:"flex-end",marginBottom:"8px",flexWrap:"wrap"},children:[e.jsx("div",{style:{width:"140px"},children:e.jsx(P,{label:"Weight Slab",isLabelHidden:!0,placeholder:"Weight Slab",hasClear:!0,options:W,value:t.weight_slab||"",onChange:f=>b(r,"weight_slab")(f??"")})}),e.jsx("div",{style:{width:"140px"},children:e.jsx(P,{label:"Zone",isLabelHidden:!0,placeholder:"Zone",hasClear:!0,options:N,value:t.zone||"",onChange:f=>b(r,"zone")(f??"")})}),e.jsx("div",{style:{flexGrow:1,minWidth:"140px"},children:e.jsx(m,{label:"Service Type",isLabelHidden:!0,placeholder:"Service Type",value:t.service_type||"",onChange:b(r,"service_type")})}),e.jsx("div",{style:{width:"90px"},children:e.jsx(m,{label:"Rate",isLabelHidden:!0,placeholder:"Rate",value:t.rate||"",onChange:b(r,"rate")})}),e.jsx("button",{onClick:()=>z(r),style:{background:"none",border:"none",cursor:"pointer",color:"#dc2626",padding:"8px"},children:e.jsx(V,{size:16})})]},r))]})]})}),footer:e.jsx(pe,{hasDivider:!0,children:e.jsxs("div",{style:{display:"flex",justifyContent:"flex-end",gap:"12px"},children:[e.jsx(C,{label:"Cancel",variant:"ghost",onClick:i,isDisabled:c}),e.jsx(C,{label:"Save Record",variant:"primary",onClick:D,isDisabled:c,isLoading:c})]})})})})}function Pe({row:s,onEdit:i,onDelete:a,users:l}){const[n,d]=h.useState(!1),c=l.find(g=>g.id===s.created_by),u=l.find(g=>g.id===s.updated_by),p=ne(s);return e.jsxs("div",{style:{padding:"8px 10px",borderRadius:"8px",border:"1px solid #e2e8f0",borderLeft:`3px solid ${p?"#10b981":"#f59e0b"}`,backgroundColor:"#ffffff",transition:"border-color 0.2s, box-shadow 0.2s",marginBottom:"6px"},className:"branch-card",children:[e.jsxs("div",{style:{display:"flex",flexDirection:"row",alignItems:"center",justifyContent:"space-between",flexWrap:"wrap",gap:"10px"},onClick:()=>d(!n),children:[e.jsxs("div",{style:{display:"flex",gap:"8px",alignItems:"center",minWidth:"200px"},children:[e.jsx("div",{style:{padding:"6px",borderRadius:"6px",backgroundColor:"rgba(192, 18, 39, 0.05)",color:"#c01227",display:"flex"},children:e.jsx(M,{size:14})}),e.jsxs("div",{children:[e.jsxs("div",{style:{fontWeight:700,color:"#1e293b",fontSize:"0.8rem",display:"flex",alignItems:"center",gap:"4px"},children:[s.area||"Unknown Area",p?e.jsx(ee,{size:11,style:{color:"#10b981",flexShrink:0}}):e.jsx(se,{size:11,style:{color:"#f59e0b",flexShrink:0}})]}),e.jsxs("div",{style:{color:"#64748b",fontSize:"0.72rem",display:"flex",alignItems:"center",gap:"4px",marginTop:"2px"},children:[e.jsx(X,{size:10})," ",s.phone||"—"]})]})]}),e.jsxs("div",{style:{display:"flex",gap:"16px",flexGrow:1},children:[e.jsxs("div",{style:{width:"130px",flexShrink:0,overflow:"hidden"},children:[e.jsx("span",{style:{fontSize:"0.6rem",color:"#64748b",fontWeight:700,letterSpacing:"0.05em",display:"block",marginBottom:"2px",textTransform:"uppercase"},children:"Rate/kg"}),e.jsx(Z,{label:s.rate_per_kg,color:p?"success":"warning",truncate:!0})]}),e.jsxs("div",{style:{width:"90px",flexShrink:0,overflow:"hidden"},children:[e.jsx("span",{style:{fontSize:"0.6rem",color:"#64748b",fontWeight:700,letterSpacing:"0.05em",display:"block",marginBottom:"2px",textTransform:"uppercase"},children:"Logistics"}),e.jsx("span",{style:{fontWeight:600,color:"#1e293b",fontSize:"0.8rem",display:"block",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:s.offers_pickup==="yes"||s.offers_drop==="yes"?`${s.offers_pickup==="yes"?"Pickup":""} ${s.offers_drop==="yes"?"Drop":""}`:"—"})]})]}),e.jsxs("div",{style:{display:"flex",gap:"4px",alignItems:"center"},onClick:g=>g.stopPropagation(),children:[e.jsx(C,{size:"sm",variant:"secondary",label:n?"Hide Info":"More Info",onClick:()=>d(!n)}),e.jsx("button",{onClick:()=>i(s),style:{background:"none",border:"none",cursor:"pointer",padding:"5px",borderRadius:"4px",color:"#475569"},className:"action-btn",children:e.jsx(ve,{size:14})}),e.jsx("button",{onClick:()=>a(s),style:{background:"none",border:"none",cursor:"pointer",padding:"5px",borderRadius:"4px",color:"#dc2626"},className:"action-btn",children:e.jsx(Se,{size:14})})]})]}),n&&e.jsxs("div",{style:{marginTop:"8px",paddingTop:"8px",borderTop:"1px dashed #e2e8f0"},children:[e.jsxs("div",{style:{display:"grid",gridTemplateColumns:"repeat(auto-fit, minmax(200px, 1fr))",gap:"8px"},children:[e.jsxs(O,{icon:q,title:"ENQUIRY DETAILS",children:[e.jsx(y,{label:"RATE PER KG",children:e.jsx(Z,{label:s.rate_per_kg,color:"success"})}),e.jsx(y,{label:"PICKUP",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:s.offers_pickup||"—"})}),e.jsx(y,{label:"DROP",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:s.offers_drop||"—"})}),e.jsx(y,{label:"PACKING",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:s.packing_charge||"—"})})]}),e.jsxs(O,{icon:fe,title:"LOGISTICS & OPS",children:[e.jsx(y,{label:"TIME IN DAYS",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:s.time_in_days||"—"})}),e.jsx(y,{label:"COMPANY",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:s.company||"—"})}),e.jsx(y,{label:"FREQUENCY",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:s.frequency||"—"})}),e.jsx(y,{label:"CONTACT NUMBER",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:s.phone||"—"})})]}),e.jsxs(O,{icon:M,title:"LOCATION & PINCODE",children:[e.jsx(y,{label:"AREA / ZONE",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:s.area||"—"})}),e.jsx(y,{label:"SERVICEABLE PINCODES",children:s.pincodes?e.jsx("div",{style:{display:"flex",gap:"4px",flexWrap:"wrap",marginTop:"4px"},children:s.pincodes.split(",").map((g,x)=>e.jsx("span",{style:{fontSize:"0.7rem",padding:"2px 6px",backgroundColor:"#f1f5f9",color:"#475569",borderRadius:"4px"},children:g.trim()},x))}):e.jsx("span",{style:{fontSize:"0.78rem",color:"#94a3b8"},children:"—"})}),e.jsx(y,{label:"PLUS CODE",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:s.plus_code||"—"})}),e.jsx(y,{label:"FULL ADDRESS",children:e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:"6px"},children:[e.jsxs("div",{style:{display:"flex",gap:"6px",padding:"8px",borderRadius:"6px",backgroundColor:"#f8fafc",border:"1px solid #e2e8f0"},children:[e.jsx(M,{size:12,style:{color:"#94a3b8",marginTop:"2px"}}),e.jsx("span",{style:{color:"#1e293b",fontSize:"0.78rem",lineHeight:1.4},children:s.address||"—"})]}),(s.address||s.plus_code)&&e.jsxs("a",{href:`https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(s.plus_code?s.plus_code+" "+(s.address||""):s.address)}`,target:"_blank",rel:"noopener noreferrer",style:{alignSelf:"flex-start",padding:"4px 10px",fontSize:"0.7rem",fontWeight:600,borderRadius:"4px",color:"#c01227",border:"1px solid rgba(192, 18, 39, 0.2)",backgroundColor:"rgba(192, 18, 39, 0.05)",textDecoration:"none",display:"inline-flex",alignItems:"center",gap:"4px"},children:[e.jsx(he,{size:12}),"View on map"]})]})})]})]}),e.jsx("div",{style:{marginTop:"8px"},children:e.jsx(O,{icon:H,title:"RECORD METADATA",children:e.jsxs("div",{style:{display:"grid",gridTemplateColumns:"repeat(auto-fit, minmax(160px, 1fr))",gap:"8px"},children:[e.jsx(y,{label:"CREATED BY",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:c?c.first_name:s.created_by?`User ID: ${s.created_by}`:"System"})}),e.jsx(y,{label:"LAST EDITED BY",children:e.jsx("span",{style:{fontSize:"0.78rem",fontWeight:500,color:"#1e293b"},children:u?u.first_name:s.updated_by?`User ID: ${s.updated_by}`:"—"})})]})})})]})]})}function De({companyName:s,branches:i,onEdit:a,onDelete:l,users:n,isLast:d}){const[c,u]=h.useState(!1),p=i.filter(ne).length,g=i.length,x=p===g,T=x?"success":p===0?"error":"warning";return e.jsxs("div",{style:{borderBottom:d?"none":"1px solid rgba(5, 54, 89, 0.06)"},children:[e.jsxs("div",{style:{display:"flex",flexDirection:"row",alignItems:"center",justifyContent:"space-between",flexWrap:"wrap",gap:"12px",padding:"10px 16px",cursor:"pointer"},onClick:()=>u(!c),children:[e.jsxs("div",{style:{display:"flex",gap:"10px",alignItems:"center"},children:[e.jsx("div",{style:{width:"32px",height:"32px",borderRadius:"8px",backgroundColor:"rgba(192, 18, 39, 0.08)",color:"#C01227",fontWeight:700,fontSize:"0.8rem",display:"flex",alignItems:"center",justifyContent:"center",flexShrink:0},children:(s||"A")[0].toUpperCase()}),e.jsxs("div",{children:[e.jsx(Y,{type:"body",weight:"semibold",children:s||"Unknown Company"}),e.jsxs("div",{style:{display:"flex",gap:"6px",alignItems:"center",marginTop:"2px",flexWrap:"wrap"},children:[e.jsxs("div",{style:{display:"inline-flex",alignItems:"center",gap:"4px"},children:[e.jsx(q,{size:11,style:{color:"#94a3b8"}}),e.jsxs(Y,{type:"supporting",color:"secondary",children:[i.length," location",i.length!==1&&"s"]})]}),e.jsx(de,{variant:T,icon:x?e.jsx(ee,{size:11}):e.jsx(se,{size:11}),label:`${p}/${g} quoted`})]})]})]}),e.jsx(ue,{label:c?"Hide locations":"View locations",tooltip:c?"Hide locations":"View locations",variant:"secondary",size:"sm",icon:c?e.jsx(me,{size:14}):e.jsx(re,{size:14})})]}),c&&e.jsx("div",{style:{padding:"0 16px 10px 16px",backgroundColor:"#f8fafc"},children:e.jsx("div",{style:{display:"flex",flexDirection:"column",gap:"6px",paddingTop:"4px"},children:i.map((z,j)=>e.jsx(Pe,{row:z,onEdit:a,onDelete:l,users:n},z.id??j))})})]})}function Je(){const[s,i]=h.useState([]),[a,l]=h.useState(!0),[n,d]=h.useState(""),[c,u]=h.useState(0),[p,g]=h.useState(5),[x,b]=h.useState(!1),[T,z]=h.useState(null),[j,E]=h.useState(null),[R,D]=h.useState(!1),[A,I]=h.useState(null),[W,N]=h.useState([]),t=()=>{let o=!1;return l(!0),Promise.all([_e(),te().catch(()=>[])]).then(([v,S])=>{o||(i(v.data||v||[]),N(S||[]))}).catch(v=>console.error("Error fetching data:",v)).finally(()=>{o||l(!1)}),()=>{o=!0}};h.useEffect(()=>t(),[]);const r=async()=>{D(!0),I(null);try{await oe(j.id),E(null),t()}catch(o){I(o.message||"Failed to delete. Please try again.")}finally{D(!1)}},k=s.filter(o=>(o.company||"").toLowerCase().includes(n.toLowerCase())||(o.area||"").toLowerCase().includes(n.toLowerCase())||(o.phone||"").toLowerCase().includes(n.toLowerCase())).reduce((o,v)=>{const S=v.company||"Unknown Company";return o[S]||(o[S]=[]),o[S].push(v),o},{}),_=Object.keys(k).sort(),B=_.slice(c*p,c*p+p),F={total:s.length,quoted:s.filter(o=>o.rate_per_kg&&!String(o.rate_per_kg).toLowerCase().includes("not answered")).length,missing:s.filter(o=>!o.rate_per_kg||String(o.rate_per_kg).toLowerCase().includes("not answered")).length};return e.jsxs("div",{style:{fontFamily:"system-ui, sans-serif"},children:[e.jsx(le,{title:"Field Surveys",action:e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"12px",flexWrap:"nowrap"},children:[e.jsx("div",{style:{flex:"0 1 320px",minWidth:"200px"},children:e.jsx(m,{label:"Search survey records",isLabelHidden:!0,placeholder:"Search surveys…",startIcon:e.jsx(Q,{size:16}),hasClear:!0,value:n,onChange:o=>{d(o),u(0)}})}),e.jsx(C,{variant:"primary",icon:e.jsx(U,{size:14}),onClick:()=>{z(null),b(!0)},label:"Add New Survey",style:{flexShrink:0}})]})}),e.jsxs("div",{style:{display:"grid",gridTemplateColumns:"repeat(auto-fit, minmax(200px, 1fr))",gap:"16px",marginBottom:"24px",marginTop:"16px"},children:[e.jsx($,{size:"sm",title:"TOTAL ENQUIRIES",value:a?"...":F.total,icon:H,color:"primary"}),e.jsx($,{size:"sm",title:"RATES QUOTED",value:a?"...":F.quoted,icon:q,color:"success"}),e.jsx($,{size:"sm",title:"MISSING INFO",value:a?"...":F.missing,icon:X,color:"warning"})]}),e.jsxs(ye,{style:{padding:"0",borderRadius:"16px",border:"1px solid rgba(5, 54, 89, 0.08)",backgroundColor:"#ffffff",overflow:"hidden",boxShadow:"0 10px 30px -5px rgba(0,0,0,0.02), 0 4px 12px -3px rgba(0,0,0,0.02)"},children:[a?e.jsx("div",{style:{padding:"20px 24px"},children:[0,1,2].map(o=>e.jsx("div",{className:"skeleton-pulse",style:{height:"64px",borderRadius:"10px",backgroundColor:"#f1f5f9",marginBottom:"10px"}},o))}):B.length>0?e.jsx("div",{children:B.map((o,v)=>e.jsx(De,{companyName:o,branches:k[o],onEdit:S=>{z(S),b(!0)},onDelete:S=>E(S),users:W,isLast:v===B.length-1},o))}):n?e.jsxs("div",{style:{padding:"32px 24px"},children:[e.jsx(K,{icon:Q,title:"No matching records",caption:`Nothing matches "${n}". Try a different company, area or phone number.`}),e.jsx("div",{style:{marginTop:"16px",display:"flex",justifyContent:"center"},children:e.jsx(C,{label:"Clear search",variant:"secondary",onClick:()=>{d(""),u(0)}})})]}):e.jsx("div",{style:{padding:"32px 24px"},children:e.jsx(K,{icon:H,title:"No survey records yet",caption:"Field surveys you add will show up here, grouped by company."})}),e.jsxs("div",{style:{display:"flex",justifyContent:"flex-end",alignItems:"center",padding:"12px 24px",borderTop:"1px solid #e2e8f0",gap:"20px",flexWrap:"wrap",backgroundColor:"#ffffff"},children:[e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px",fontSize:"0.875rem",color:"#475569"},children:[e.jsx("span",{children:"Companies per page:"}),e.jsx("select",{value:p,onChange:o=>{g(Number(o.target.value)),u(0)},className:"rpp-select",children:[5,10,25].map(o=>e.jsx("option",{value:o,children:o},o))})]}),e.jsxs("span",{style:{fontSize:"0.875rem",color:"#475569"},children:[_.length===0?"0-0":`${c*p+1}-${Math.min((c+1)*p,_.length)}`," of ",_.length]}),e.jsxs("div",{style:{display:"flex",gap:"8px"},children:[e.jsx(C,{label:"Prev",variant:"secondary",size:"sm",onClick:()=>u(o=>Math.max(0,o-1)),isDisabled:c===0}),e.jsx(C,{label:"Next",variant:"secondary",size:"sm",onClick:()=>u(o=>Math.min(Math.ceil(_.length/p)-1,o+1)),isDisabled:c>=Math.ceil(_.length/p)-1})]})]})]}),e.jsx(Ie,{open:x,onClose:()=>b(!1),onSave:()=>t(),initialData:T}),e.jsx(ge,{isOpen:!!j,onOpenChange:o=>{o||(E(null),I(null))},title:"Delete survey record?",description:j?A||`This will permanently remove the record for ${j.area||j.company}. This cannot be undone.`:"",actionLabel:"Delete",onAction:r,isActionLoading:R}),e.jsx("style",{children:`
.action-btn:hover {
background-color: #f1f5f9;
}
.branch-card:hover {
border-color: #cbd5e1;
box-shadow: 0 4px 12px rgba(15, 23, 42, 0.06);
}
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
@keyframes skeletonPulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.55; } }
.skeleton-pulse { animation: skeletonPulse 1.4s ease-in-out infinite; }
`})]})}export{Je as default};