Files
doormile_crm_build/dist/assets/Pricing-Bu0-9cE3.js

2 lines
13 KiB
JavaScript

import{a,j as e,B as u,T as v,M as G,N as n,H as K,S as T,O as X,L as Z,C as ee,I as re}from"./index-DgiSycAe.js";import{d as te,e as U,D as oe,a as se,b as ie,c as ne}from"./EditOutlined-Cybcxtqy.js";import{e as ae,c as le,b as Q,d as ce,a as de}from"./StorefrontOutlined-CchPzH-F.js";import{P as ge,C as E,G as w}from"./PageHeader-BZc3dw3P.js";import{C as F}from"./Link-ksPAHyrc.js";import{F as xe,S as ue,O as he,T as R}from"./TextField-X_DfLpXg.js";import{I as pe}from"./InputAdornment-BpAXe-Rj.js";import{B as W}from"./Button-CfeUJ628.js";import{T as fe,a as me,b as be,c as M,d as ye}from"./TableRow-DhJAYJSx.js";import{T as k}from"./TableCell-DU4FFYK_.js";const B="https://api.doormile.com/api/v1",O=()=>{const o=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...o?{Authorization:`Bearer ${o}`}:{}}};async function je(){const o=await fetch(`${B}/admin/carrier-pricing?limit=500`,{headers:O()});if(!o.ok)throw new Error("Failed to fetch pricing");return o.json()}async function Se(o){const d=!!o.id,b=d?`${B}/admin/carrier-pricing/${o.id}`:`${B}/admin/carrier-pricing`,l=await fetch(b,{method:d?"PUT":"POST",headers:O(),body:JSON.stringify({company:o.company,weight_slab:o.weight_slab,zone:o.zone,rate:Number(o.rate)})});if(!l.ok)throw new Error("Failed to save pricing");return l.json()}function ve({open:o,onClose:d,onSave:b,initialData:h}){const[l,p]=a.useState({}),[i,S]=a.useState(!1);a.useEffect(()=>{o&&p(h||{company:"",weight_slab:"",zone:"",rate:""})},[o,h]);const f=g=>I=>{p($=>({...$,[g]:I.target.value}))},_=async()=>{S(!0);try{await Se(l),b(),d()}catch(g){console.error(g),alert("Failed to save. Please try again.")}finally{S(!1)}};return e.jsxs(oe,{open:o,onClose:d,maxWidth:"xs",fullWidth:!0,children:[e.jsx(se,{sx:{fontWeight:800},children:h!=null&&h.id?"Edit Pricing Rate":"Add New Pricing"}),e.jsx(ie,{dividers:!0,children:e.jsxs(w,{container:!0,spacing:2,children:[e.jsx(w,{item:!0,xs:12,children:e.jsx(R,{fullWidth:!0,label:"Company",value:l.company||"",onChange:f("company"),size:"small"})}),e.jsx(w,{item:!0,xs:12,children:e.jsx(R,{fullWidth:!0,label:"Weight Slab (e.g. 1kg - 2kg)",value:l.weight_slab||"",onChange:f("weight_slab"),size:"small"})}),e.jsx(w,{item:!0,xs:12,children:e.jsx(R,{fullWidth:!0,label:"Zone (e.g. Local / Same)",value:l.zone||"",onChange:f("zone"),size:"small"})}),e.jsx(w,{item:!0,xs:12,children:e.jsx(R,{fullWidth:!0,label:"Rate (₹)",type:"number",value:l.rate||"",onChange:f("rate"),size:"small"})})]})}),e.jsxs(ne,{sx:{p:2,bgcolor:"grey.50"},children:[e.jsx(W,{onClick:d,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 Ce(){let o=[],d=0;const b=50;let h=1;for(;d<h;){const l=await fetch(`${B}/admin/competitor-branches?limit=${b}&offset=${d}`,{headers:O()});if(!l.ok)throw new Error("Failed to fetch surveys");const p=await l.json();if(!p.data||p.data.length===0)break;const i=p.data.filter(S=>!o.some(f=>f.id===S.id));if(i.length===0)break;o=[...o,...i],h=p.total||0,d+=b}return{data:o,total:o.length}}function _e(){const[o,d]=a.useState([]),[b,h]=a.useState([]),[l,p]=a.useState(!0),[i,S]=a.useState(""),[f,_]=a.useState(""),[g,I]=a.useState(!1),[$,A]=a.useState(!1),[V,D]=a.useState(null),[m,Y]=a.useState(!1),H=()=>{let r=!1;return p(!0),Promise.all([je(),Ce().catch(()=>({data:[]}))]).then(([y,z])=>{r||(d(y.data||[]),h(z.data||z||[]))}).catch(y=>console.error("Failed to fetch data",y)).finally(()=>{r||p(!1)}),()=>{r=!0}};a.useEffect(()=>H(),[]);const C=[...new Set(o.map(r=>r.company).filter(Boolean))];a.useEffect(()=>{!i&&C.length>0&&S(C[0])},[C,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 q=o.filter(r=>r.company===i),P=b.filter(r=>(r.company||"").toLowerCase()===(i||"").toLowerCase()),N=["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(ge,{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(F,{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(ae,{})}),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(xe,{sx:{mt:.5,minWidth:260,display:"block"},children:e.jsx(ue,{value:i||"",onChange:r=>S(r.target.value),displayEmpty:!0,IconComponent:le,input:e.jsx(he,{sx:{borderRadius:3,fontWeight:700,fontSize:"1.1rem",bgcolor:n("#e2e8f0",.4),"& fieldset":{border:"none"},"&:hover":{bgcolor:n("#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:C.length===0?e.jsx(G,{value:"",disabled:!0,children:"Loading Providers..."}):C.map(r=>e.jsx(G,{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(pe,{position:"start",children:e.jsx(K,{color:"primary"})}),sx:{borderRadius:50,bgcolor:n("#e2e8f0",.3),"& fieldset":{border:"none"},"&:hover":{bgcolor:n("#e2e8f0",.5)},"&.Mui-focused":{bgcolor:"#fff","& fieldset":{border:"2px solid",borderColor:"primary.main"}}}}}),e.jsx(W,{variant:"contained",startIcon:e.jsx(te,{}),onClick:()=>{D(null),A(!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(U,{}),onClick:()=>Y(!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":n("#000",.05),borderColor:m?"transparent":"grey.400"}},children:m?"Done":"Edit"})]})]}),i&&P.length>0&&e.jsxs(F,{elevation:0,sx:{borderRadius:4,border:"1px solid",borderColor:g?"#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(T,{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(!g),children:[e.jsxs(T,{direction:"row",spacing:3,alignItems:"center",sx:{minWidth:260},children:[e.jsx(X,{sx:{width:68,height:68,bgcolor:n("#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(Q,{sx:{fontSize:16,mr:.5,color:"#c01227"}}),P.length," Registered Location",P.length!==1&&"s"]})]})]}),e.jsxs(T,{direction:"row",spacing:3,alignItems:"center",children:[e.jsxs(T,{direction:"row",spacing:1,sx:{display:{xs:"none",md:"flex"}},children:[e.jsx(E,{size:"small",label:"First Mile",sx:{bgcolor:n("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(E,{size:"small",label:"Mid Mile",sx:{bgcolor:n("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(E,{size:"small",label:"Last Mile",sx:{bgcolor:n("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}})]}),e.jsx(W,{onClick:r=>{r.stopPropagation(),I(!g)},sx:{borderRadius:2,py:1,px:3,fontWeight:700,color:g?"#c01227":"grey.700",bgcolor:g?n("#c01227",.1):"grey.100","&:hover":{bgcolor:n("#c01227",.15),color:"#c01227"}},endIcon:g?e.jsx(ce,{}):e.jsx(de,{}),children:g?"Hide Coverage":"View Coverage Map"})]})]}),e.jsx(Z,{in:g,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(Q,{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:n("#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(F,{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:l?e.jsx(u,{sx:{display:"flex",justifyContent:"center",alignItems:"center",py:10},children:e.jsx(ee,{size:48,thickness:4,sx:{color:"primary.light"}})}):e.jsx(fe,{children:e.jsxs(me,{sx:{minWidth:700},children:[e.jsx(be,{children:e.jsxs(M,{sx:{bgcolor:n("#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"}),N.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(ye,{children:L.length===0?e.jsx(M,{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=q.filter(x=>{const t=(x.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,"")}),z=x=>{const t=y.find(j=>{const s=(j.zone||"").toLowerCase();return x==="Local / Same"?s.includes("local")||s.includes("same")||s.includes("city")||s==="l":x==="Within Tamilnadu"?s.includes("tamil")||s.includes("tn")||s.includes("state")||s==="s":x==="Interstate India"?s.includes("inter")||s.includes("india")||s.includes("national")||s.includes("rest")||s==="n":!1});return t?`${t.rate}`:"—"};return e.jsxs(M,{sx:{transition:"all 0.2s","&:hover":{bgcolor:n("#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}),N.map(x=>{const t=z(x),j=t!=="—",s=y.find(J=>{const c=(J.zone||"").toLowerCase();return x==="Local / Same"?c.includes("local")||c.includes("same")||c.includes("city")||c==="l":x==="Within Tamilnadu"?c.includes("tamil")||c.includes("tn")||c.includes("state")||c==="s":x==="Interstate India"?c.includes("inter")||c.includes("india")||c.includes("national")||c.includes("rest")||c==="n":!1});return e.jsx(k,{align:"center",sx:{py:2,borderBottom:"1px solid",borderColor:"grey.100"},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?n("#10b981",.08):"transparent",px:j?1.5:0,py:j?.5:0,borderRadius:2},children:t}),m&&e.jsx(re,{size:"small",sx:{color:"grey.400","&:hover":{color:"#c01227",bgcolor:n("#c01227",.1)}},onClick:()=>{D({id:s?s.id:null,company:i,weight_slab:r,zone:s?s.zone:x,rate:s?s.rate:""}),A(!0)},children:e.jsx(U,{sx:{fontSize:14}})})]})},x)})]},r)})})]})})})]}),e.jsx(ve,{open:$,onClose:()=>A(!1),onSave:()=>H(),initialData:V})]})}export{_e as default};