import{a as l,j as e,B as u,T as v,M as Q,N as a,H as ee,S as _,O as re,L as te,C as oe,K as se,I as ie,c as ae}from"./index-BtnGSOUW.js";import{d as ne,e as V,D as le,a as ce,b as de,c as ge}from"./EditOutlined-DygVX55C.js";import{e as xe,c as ue,b as Y,d as he,a as pe}from"./StorefrontOutlined-_9b_sI4N.js";import{P as fe,C as E,G as C}from"./PageHeader-BbVGIYxA.js";import{C as M}from"./Link-BYhfZC01.js";import{F as me,S as be,O as ye,T as R}from"./TextField-BBSES6Bn.js";import{I as je}from"./InputAdornment-Cc3k2D3z.js";import{B as W}from"./Button-CbWIqGJy.js";import{T as Se,a as ve,b as we,c as O,d as Ce}from"./TableRow-CmSgfUTb.js";import{T as k}from"./TableCell-D9JXvsTJ.js";const B="https://api.doormile.com/api/v1",D=()=>{const o=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...o?{Authorization:`Bearer ${o}`}:{}}};async function ke(){const o=await fetch(`${B}/admin/carrier-pricing?limit=500`,{headers:D()});if(!o.ok)throw new Error("Failed to fetch pricing");return o.json()}async function Re(o){const g=!!o.id,b=g?`${B}/admin/carrier-pricing/${o.id}`:`${B}/admin/carrier-pricing`,d=await fetch(b,{method:g?"PUT":"POST",headers:D(),body:JSON.stringify({company:o.company,weight_slab:o.weight_slab,zone:o.zone,rate:String(o.rate)})});if(!d.ok)throw new Error("Failed to save pricing");return d.json()}function We({open:o,onClose:g,onSave:b,initialData:h}){const[d,p]=l.useState({}),[i,S]=l.useState(!1);l.useEffect(()=>{o&&p(h||{company:"",weight_slab:"",zone:"",rate:""})},[o,h]);const f=x=>I=>{p(A=>({...A,[x]:I.target.value}))},$=async()=>{S(!0);try{await Re(d),b(),g()}catch(x){console.error(x),alert("Failed to save. Please try again.")}finally{S(!1)}};return e.jsxs(le,{open:o,onClose:g,maxWidth:"xs",fullWidth:!0,children:[e.jsx(ce,{sx:{fontWeight:800},children:h!=null&&h.id?"Edit Pricing Rate":"Add New Pricing"}),e.jsx(de,{dividers:!0,children:e.jsxs(C,{container:!0,spacing:2,children:[e.jsx(C,{item:!0,xs:12,children:e.jsx(R,{fullWidth:!0,label:"Company",value:d.company||"",onChange:f("company"),size:"small"})}),e.jsx(C,{item:!0,xs:12,children:e.jsx(R,{fullWidth:!0,label:"Weight Slab (e.g. 1kg - 2kg)",value:d.weight_slab||"",onChange:f("weight_slab"),size:"small"})}),e.jsx(C,{item:!0,xs:12,children:e.jsx(R,{fullWidth:!0,label:"Zone (e.g. Local / Same)",value:d.zone||"",onChange:f("zone"),size:"small"})}),e.jsx(C,{item:!0,xs:12,children:e.jsx(R,{fullWidth:!0,label:"Rate (₹)",type:"number",value:d.rate||"",onChange:f("rate"),size:"small"})})]})}),e.jsxs(ge,{sx:{p:2,bgcolor:"grey.50"},children:[e.jsx(W,{onClick:g,disabled:i,color:"inherit",children:"Cancel"}),e.jsx(W,{onClick:$,disabled:i,variant:"contained",sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2},children:i?"Saving...":"Save Rate"})]})]})}async function Ie(){let o=[],g=0;const b=50;let h=1;for(;g!o.some(f=>f.id===S.id));if(i.length===0)break;o=[...o,...i],h=p.total||0,g+=b}return{data:o,total:o.length}}function Oe(){const[o,g]=l.useState([]),[b,h]=l.useState([]),[d,p]=l.useState(!0),[i,S]=l.useState(""),[f,$]=l.useState(""),[x,I]=l.useState(!1),[A,F]=l.useState(!1),[q,H]=l.useState(null),[m,J]=l.useState(!1),[K,X]=l.useState([]),N=()=>{let r=!1;return p(!0),Promise.all([ke(),Ie().catch(()=>({data:[]})),ae().catch(()=>[])]).then(([y,T,c])=>{r||(g(y.data||[]),h(T.data||T||[]),X(c||[]))}).catch(y=>console.error("Failed to fetch data",y)).finally(()=>{r||p(!1)}),()=>{r=!0}};l.useEffect(()=>N(),[]);const w=[...new Set(o.map(r=>r.company).filter(Boolean))];l.useEffect(()=>{!i&&w.length>0&&S(w[0])},[w,i]);let L=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];if(f){const r=f.toLowerCase();L=L.filter(y=>y.toLowerCase().includes(r))}const Z=o.filter(r=>r.company===i),P=b.filter(r=>(r.company||"").toLowerCase()===(i||"").toLowerCase()),U=["Local / Same","Within Tamilnadu","Interstate India"];return e.jsxs(u,{sx:{pb:6,minHeight:"80vh",bgcolor:"#f8fafc",px:{xs:2,md:4},pt:3},children:[e.jsx(fe,{title:"Logistics Pricing Board",breadcrumbs:[{label:"Pricing Overview"}]}),e.jsxs(u,{sx:{display:"flex",flexDirection:"column",gap:4,mt:3,maxWidth:1200,mx:"auto"},children:[e.jsxs(M,{sx:{p:3,borderRadius:4,bgcolor:"#ffffff",boxShadow:"0 20px 40px -10px rgba(0,0,0,0.03)",border:"1px solid",borderColor:"grey.100",display:"flex",flexDirection:{xs:"column",md:"row"},alignItems:"center",justifyContent:"space-between",gap:3},children:[e.jsxs(u,{sx:{display:"flex",alignItems:"center",gap:2,width:{xs:"100%",md:"auto"}},children:[e.jsx(u,{sx:{width:48,height:48,borderRadius:3,bgcolor:"primary.lighter",color:"primary.main",display:"flex",alignItems:"center",justifyContent:"center"},children:e.jsx(xe,{})}),e.jsxs(u,{children:[e.jsx(v,{variant:"caption",sx:{fontWeight:700,color:"text.secondary",letterSpacing:.8,textTransform:"uppercase"},children:"Active Logistics Provider"}),e.jsx(me,{sx:{mt:.5,minWidth:260,display:"block"},children:e.jsx(be,{value:i||"",onChange:r=>S(r.target.value),displayEmpty:!0,IconComponent:ue,input:e.jsx(ye,{sx:{borderRadius:3,fontWeight:700,fontSize:"1.1rem",bgcolor:a("#e2e8f0",.4),"& fieldset":{border:"none"},"&:hover":{bgcolor:a("#e2e8f0",.6)},"&.Mui-focused":{bgcolor:"#fff","& fieldset":{border:"2px solid",borderColor:"primary.main"}}}}),MenuProps:{PaperProps:{sx:{borderRadius:3,mt:1,boxShadow:"0 12px 24px rgba(0,0,0,0.1)"}}},children:w.length===0?e.jsx(Q,{value:"",disabled:!0,children:"Loading Providers..."}):w.map(r=>e.jsx(Q,{value:r,sx:{fontWeight:600,py:1.5,borderRadius:1,mx:1},children:r},r))})})]})]}),e.jsxs(u,{sx:{display:"flex",gap:2,alignItems:"center"},children:[e.jsx(R,{placeholder:"Search weight slabs...",value:f,onChange:r=>$(r.target.value),InputProps:{startAdornment:e.jsx(je,{position:"start",children:e.jsx(ee,{color:"primary"})}),sx:{borderRadius:50,bgcolor:a("#e2e8f0",.3),"& fieldset":{border:"none"},"&:hover":{bgcolor:a("#e2e8f0",.5)},"&.Mui-focused":{bgcolor:"#fff","& fieldset":{border:"2px solid",borderColor:"primary.main"}}}}}),e.jsx(W,{variant:"contained",startIcon:e.jsx(ne,{}),onClick:()=>{H(null),F(!0)},sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2,py:1.2,fontWeight:700,whiteSpace:"nowrap"},children:"Add Rate"}),e.jsx(W,{variant:m?"contained":"outlined",startIcon:e.jsx(V,{}),onClick:()=>J(!m),sx:{borderRadius:2,py:1.2,fontWeight:700,whiteSpace:"nowrap",color:m?"white":"grey.700",borderColor:m?"transparent":"grey.300",bgcolor:m?"grey.800":"transparent","&:hover":{bgcolor:m?"grey.900":a("#000",.05),borderColor:m?"transparent":"grey.400"}},children:m?"Done":"Edit"})]})]}),i&&P.length>0&&e.jsxs(M,{elevation:0,sx:{borderRadius:4,border:"1px solid",borderColor:x?"#c01227":"grey.200",bgcolor:"white",transition:"all 0.3s ease","&:hover":{borderColor:"#c01227",boxShadow:"0 8px 30px rgba(192, 18, 39, 0.08)",transform:"translateY(-2px)"}},children:[e.jsxs(_,{direction:{xs:"column",md:"row"},spacing:4,sx:{p:{xs:3,md:3.5},cursor:"pointer",userSelect:"none"},alignItems:{xs:"flex-start",md:"center"},justifyContent:"space-between",onClick:()=>I(!x),children:[e.jsxs(_,{direction:"row",spacing:3,alignItems:"center",sx:{minWidth:260},children:[e.jsx(re,{sx:{width:68,height:68,bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:800,fontSize:"1.8rem",borderRadius:3},children:(i||"A")[0].toUpperCase()}),e.jsxs(u,{children:[e.jsx(v,{variant:"overline",sx:{color:"grey.500",fontWeight:700,letterSpacing:1.2},children:"LOGISTICS PARTNER"}),e.jsx(v,{variant:"h5",sx:{fontWeight:800,color:"grey.900",lineHeight:1.1,mt:.25},children:i}),e.jsxs(v,{variant:"body2",sx:{color:"grey.600",mt:1,fontWeight:500,display:"flex",alignItems:"center"},children:[e.jsx(Y,{sx:{fontSize:16,mr:.5,color:"#c01227"}}),P.length," Registered Location",P.length!==1&&"s"]})]})]}),e.jsxs(_,{direction:"row",spacing:3,alignItems:"center",children:[e.jsxs(_,{direction:"row",spacing:1,sx:{display:{xs:"none",md:"flex"}},children:[e.jsx(E,{size:"small",label:"First Mile",sx:{bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(E,{size:"small",label:"Mid Mile",sx:{bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(E,{size:"small",label:"Last Mile",sx:{bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}})]}),e.jsx(W,{onClick:r=>{r.stopPropagation(),I(!x)},sx:{borderRadius:2,py:1,px:3,fontWeight:700,color:x?"#c01227":"grey.700",bgcolor:x?a("#c01227",.1):"grey.100","&:hover":{bgcolor:a("#c01227",.15),color:"#c01227"}},endIcon:x?e.jsx(he,{}):e.jsx(pe,{}),children:x?"Hide Coverage":"View Coverage Map"})]})]}),e.jsx(te,{in:x,timeout:"auto",unmountOnExit:!0,children:e.jsxs(u,{sx:{p:3,pt:0,borderTop:"1px dashed",borderColor:"grey.200",bgcolor:"grey.50",borderBottomLeftRadius:16,borderBottomRightRadius:16},children:[e.jsxs(u,{sx:{display:"flex",alignItems:"center",mb:2,mt:3},children:[e.jsx(Y,{sx:{color:"#c01227",fontSize:20,mr:1}}),e.jsx(v,{variant:"caption",sx:{color:"grey.600",fontWeight:700,letterSpacing:1},children:"ALL SERVICED AREAS:"})]}),e.jsx(u,{sx:{display:"flex",gap:1.5,flexWrap:"wrap"},children:P.map(r=>e.jsx(E,{label:r.area||r.address,sx:{borderRadius:2,fontWeight:600,px:1,py:2.5,bgcolor:"white",color:"grey.800",border:"1px solid",borderColor:"grey.300",boxShadow:"0 2px 8px rgba(0,0,0,0.02)","&:hover":{borderColor:"#c01227",color:"#c01227",bgcolor:a("#c01227",.05),transform:"translateY(-1px)",boxShadow:"0 4px 12px rgba(192,18,39,0.08)"},transition:"all 0.2s ease"}},r.id||Math.random()))})]})})]}),e.jsx(M,{sx:{borderRadius:4,boxShadow:"0 24px 48px -12px rgba(0,0,0,0.05)",border:"1px solid",borderColor:"grey.200",overflow:"hidden",bgcolor:"#fff"},children:d?e.jsx(u,{sx:{display:"flex",justifyContent:"center",alignItems:"center",py:10},children:e.jsx(oe,{size:48,thickness:4,sx:{color:"primary.light"}})}):e.jsx(Se,{children:e.jsxs(ve,{sx:{minWidth:700},children:[e.jsx(we,{children:e.jsxs(O,{sx:{bgcolor:a("#f8fafc",.8)},children:[e.jsx(k,{sx:{py:2,px:4,fontWeight:700,color:"grey.500",fontSize:"0.75rem",letterSpacing:1.2,textTransform:"uppercase",borderBottom:"2px solid",borderColor:"grey.100"},children:"Weight Slab"}),U.map(r=>e.jsx(k,{align:"center",sx:{py:2,fontWeight:700,color:"grey.500",fontSize:"0.75rem",letterSpacing:1.2,textTransform:"uppercase",borderBottom:"2px solid",borderColor:"grey.100"},children:r},r))]})}),e.jsx(Ce,{children:L.length===0?e.jsx(O,{children:e.jsx(k,{colSpan:4,align:"center",sx:{py:8},children:e.jsx(v,{variant:"body1",color:"text.secondary",sx:{fontWeight:500},children:"No pricing data matches your search."})})}):L.map(r=>{const y=Z.filter(c=>{const t=(c.weight_slab||"").toLowerCase().replace(/\s+/g,"");return r==="< 500g"?t.includes("<500")||t.includes("upto500")||t==="500g"||t==="0.5kg"||t==="0-500g"||t.includes("500gm"):r==="500g - 1kg"?t.includes("500-1")||t.includes("0.5-1")||t==="1kg"||t==="upto1kg":r==="1kg - 2kg"?t.includes("1-2")||t.includes("1to2")||t==="2kg"||t==="upto2kg":r==="2kg - 5kg"?t.includes("2-5")||t.includes("2to5")||t==="5kg"||t==="upto5kg":r==="> 5kg"?t.includes(">5")||t.includes("above5")||t.includes("5+")||t.includes("10kg"):t===r.toLowerCase().replace(/\s+/g,"")}),T=c=>{const t=y.find(j=>{const s=(j.zone||"").toLowerCase();return c==="Local / Same"?s.includes("local")||s.includes("same")||s.includes("city")||s==="l":c==="Within Tamilnadu"?s.includes("tamil")||s.includes("tn")||s.includes("state")||s==="s":c==="Interstate India"?s.includes("inter")||s.includes("india")||s.includes("national")||s.includes("rest")||s==="n":!1});return t?`₹${t.rate}`:"—"};return e.jsxs(O,{sx:{transition:"all 0.2s","&:hover":{bgcolor:a("#f1f5f9",.5),transform:"scale(1.001)"},"&:last-child td":{borderBottom:0}},children:[e.jsx(k,{sx:{px:4,py:2,fontWeight:600,color:"grey.800",fontSize:"0.9rem",borderBottom:"1px solid",borderColor:"grey.100"},children:r}),U.map(c=>{const t=T(c),j=t!=="—",s=y.find(z=>{const n=(z.zone||"").toLowerCase();return c==="Local / Same"?n.includes("local")||n.includes("same")||n.includes("city")||n==="l":c==="Within Tamilnadu"?n.includes("tamil")||n.includes("tn")||n.includes("state")||n==="s":c==="Interstate India"?n.includes("inter")||n.includes("india")||n.includes("national")||n.includes("rest")||n==="n":!1});let G="";if(s&&s.updated_by){const z=K.find(n=>n.id===s.updated_by);G=`Last updated by: ${z?z.first_name:"ID "+s.updated_by}`}return e.jsx(k,{align:"center",sx:{py:2,borderBottom:"1px solid",borderColor:"grey.100"},children:e.jsx(se,{title:G,placement:"top",arrow:!0,children:e.jsxs(u,{sx:{display:"flex",alignItems:"center",justifyContent:"center",gap:1},children:[e.jsx(v,{sx:{display:"inline-block",fontWeight:j?700:500,color:j?"success.dark":"grey.400",fontSize:j?"0.95rem":"0.9rem",bgcolor:j?a("#10b981",.08):"transparent",px:j?1.5:0,py:j?.5:0,borderRadius:2},children:t}),m&&e.jsx(ie,{size:"small",sx:{color:"grey.400","&:hover":{color:"#c01227",bgcolor:a("#c01227",.1)}},onClick:()=>{H({id:s?s.id:null,company:i,weight_slab:r,zone:s?s.zone:c,rate:s?s.rate:""}),F(!0)},children:e.jsx(V,{sx:{fontSize:14}})})]})})},c)})]},r)})})]})})})]}),e.jsx(We,{open:A,onClose:()=>F(!1),onSave:()=>N(),initialData:q})]})}export{Oe as default};