Files
doormile_crm_build/dist/assets/Pricing-BbUkW6tl.js
2026-06-20 15:47:22 +05:30

2 lines
15 KiB
JavaScript

import{r as ae,j as e,i as ne,a as d,B as h,T as f,M,N as a,H as le,S as F,Q as ce,L as de,C as xe,K as he,c as ge,I as pe}from"./index-Dg-wT72v.js";import{d as ue,e as X,D as fe,a as me,b as be,c as ye}from"./EditOutlined-COfONJBH.js";import{d as je}from"./Close-DKUHif5O.js";import{e as ve,c as Se,b as Y,d as we,a as Ce}from"./StorefrontOutlined-BR_dFc0C.js";import{P as We,a as $,A as Re,G as W}from"./PageHeader-DgkErsA7.js";import{C as U}from"./Link-CndAQvyC.js";import{F as Ie,S as _e,O as ke,T as E}from"./TextField-COVOKpzm.js";import{I as Ae}from"./InputAdornment-HqPFSHfS.js";import{B as P}from"./Button-COL1_fTB.js";import{T as ze,a as Ee,b as Pe,c as O,d as Te}from"./TableRow-Du7Jvlvh.js";import{T as R}from"./TableCell-ClQIqE-T.js";var G={},Le=ne;Object.defineProperty(G,"__esModule",{value:!0});var ee=G.default=void 0,Be=Le(ae()),Fe=e;ee=G.default=(0,Be.default)((0,Fe.jsx)("path",{d:"M13 7h-2v4H7v2h4v4h2v-4h4v-2h-4zm-1-5C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2m0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8"}),"AddCircleOutline");const H="https://api.doormile.com/api/v1",Z=()=>{const t=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...t?{Authorization:`Bearer ${t}`}:{}}};async function $e(){const t=await fetch(`${H}/admin/carrier-pricing?limit=1000`,{headers:Z()});if(!t.ok)throw new Error("Failed to fetch pricing");const l=await t.json();return Array.isArray(l)?{data:l}:l}async function Oe(t){const l=!!t.id,v=l?`${H}/admin/carrier-pricing/${t.id}`:`${H}/admin/carrier-pricing`,m=await fetch(v,{method:l?"PUT":"POST",headers:Z(),body:JSON.stringify({company:t.company,weight_slab:t.weight_slab,zone:t.zone,rate:String(t.rate)})});if(!m.ok){const w=await m.json().catch(()=>({}));throw new Error(w.error||w.message||"Failed to save pricing")}return m.status===204?{}:m.json().catch(()=>({}))}const Me=["Local / Same","Within Tamilnadu","Interstate India"],He=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];function Ne({open:t,onClose:l,onSave:v,initialData:p,isCell:m=!1,pricingList:w=[]}){const[o,I]=d.useState({}),[y,T]=d.useState(!1),[u,b]=d.useState(null);d.useEffect(()=>{t&&(I(p||{company:"",weight_slab:"",zone:"",rate:""}),b(null))},[t,p]);const C=n=>i=>I(S=>({...S,[n]:i.target.value})),_=n=>{const i=(n.company||"").trim(),S=(n.weight_slab||"").trim(),g=(n.zone||"").trim();return w.find(j=>(j.company||"").trim()===i&&(j.weight_slab||"").trim()===S&&(j.zone||"").trim()===g)||null},N=async()=>{var n,i,S;if(!m){if(!((n=o.company)!=null&&n.trim())){b("Company is required.");return}if(!((i=o.weight_slab)!=null&&i.trim())){b("Weight slab is required.");return}if(!((S=o.zone)!=null&&S.trim())){b("Zone is required.");return}}if(!String(o.rate??"").trim()){b("Rate is required.");return}T(!0),b(null);try{let g={...o};if(!g.id){const j=_(g);j&&(g={...g,id:j.id})}await Oe(g),v(),l()}catch(g){console.error(g),b(g.message||"Failed to save. Please try again.")}finally{T(!1)}};return e.jsxs(fe,{open:t,onClose:y?void 0:l,maxWidth:"xs",fullWidth:!0,children:[e.jsxs(me,{sx:{fontWeight:800,display:"flex",alignItems:"center",justifyContent:"space-between"},children:[m?p!=null&&p.id?"Edit Rate":"Add Rate":p!=null&&p.id?"Edit Rate":"Add New Rate",e.jsx(pe,{size:"small",onClick:l,disabled:y,children:e.jsx(je,{fontSize:"small"})})]}),e.jsxs(be,{dividers:!0,children:[u&&e.jsx(Re,{severity:"error",sx:{mb:2},children:u}),e.jsxs(W,{container:!0,spacing:2,children:[m?e.jsx(W,{item:!0,xs:12,children:e.jsxs(h,{sx:{p:1.5,borderRadius:2,bgcolor:a("#c01227",.04),border:"1px solid",borderColor:a("#c01227",.15),mb:.5},children:[e.jsx(f,{variant:"caption",sx:{fontWeight:700,color:"grey.500",letterSpacing:.5,display:"block",mb:.5},children:"UPDATING RATE FOR"}),e.jsx(f,{variant:"subtitle2",sx:{fontWeight:700,color:"grey.900"},children:o.company}),e.jsxs(f,{variant:"body2",sx:{color:"grey.600",mt:.25},children:[o.weight_slab," · ",o.zone]})]})}):e.jsxs(e.Fragment,{children:[e.jsx(W,{item:!0,xs:12,children:e.jsx(E,{fullWidth:!0,label:"Company *",value:o.company||"",onChange:C("company"),size:"small"})}),e.jsx(W,{item:!0,xs:12,children:e.jsx(E,{select:!0,fullWidth:!0,label:"Weight Slab *",value:o.weight_slab||"",onChange:C("weight_slab"),size:"small",children:He.map(n=>e.jsx(M,{value:n,children:n},n))})}),e.jsx(W,{item:!0,xs:12,children:e.jsx(E,{select:!0,fullWidth:!0,label:"Zone *",value:o.zone||"",onChange:C("zone"),size:"small",children:Me.map(n=>e.jsx(M,{value:n,children:n},n))})})]}),e.jsx(W,{item:!0,xs:12,children:e.jsx(E,{fullWidth:!0,label:"Rate (₹) *",value:o.rate||"",onChange:C("rate"),size:"small",placeholder:"e.g. 25 or 25-35",autoFocus:!0})})]})]}),e.jsxs(ye,{sx:{p:2,bgcolor:"grey.50"},children:[e.jsx(P,{onClick:l,disabled:y,color:"inherit",children:"Cancel"}),e.jsx(P,{onClick:N,disabled:y,variant:"contained",sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2},children:y?"Saving...":"Save Rate"})]})]})}async function qe(){const t=await fetch(`${H}/admin/competitor-branches?limit=1000`,{headers:Z()});if(!t.ok)throw new Error("Failed to fetch surveys");const l=await t.json(),v=Array.isArray(l)?l:l.data||[];return{data:v,total:v.length}}function rr(){const[t,l]=d.useState([]),[v,p]=d.useState([]),[m,w]=d.useState(!0),[o,I]=d.useState(""),[y,T]=d.useState(""),[u,b]=d.useState(!1),[C,_]=d.useState(!1),[N,n]=d.useState(null),[i,S]=d.useState(!1),[g,j]=d.useState(!1),[re,te]=d.useState([]),Q=()=>{let r=!1;return w(!0),Promise.all([$e().catch(()=>({data:[]})),qe().catch(()=>({data:[]})),ge().catch(()=>[])]).then(([s,c,x])=>{r||(l(Array.isArray(s)?s:s.data||[]),p(c.data||c||[]),te(Array.isArray(x)?x:x.data||[]))}).catch(s=>console.error("Failed to fetch data",s)).finally(()=>{r||w(!1)}),()=>{r=!0}};d.useEffect(()=>Q(),[]);const k=[...new Set(t.map(r=>r.company).filter(Boolean))];d.useEffect(()=>{!o&&k.length>0&&I(k[0])},[k,o]);const oe=t.filter(r=>r.company===o),L=v.filter(r=>(r.company||"").toLowerCase()===(o||"").toLowerCase()),q=[...oe].sort((r,s)=>(s.id||0)-(r.id||0)),V=r=>{const s=(r||"").toLowerCase().replace(/\s+/g,""),c=s.match(/[\d.]+/g);if(!c)return 9999;const x=parseFloat(c[0]),A=s.includes("kg")?x*1e3:x;return s.startsWith("<")||s.startsWith("upto")?A-.5:s.startsWith(">")||s.includes("above")||s.endsWith("+")?A+.5:A},B=[...new Set(q.map(r=>(r.zone||"").trim()).filter(Boolean))].sort(),J=[...new Set(q.map(r=>(r.weight_slab||"").trim()).filter(Boolean))].sort((r,s)=>V(r)-V(s)),K=y?J.filter(r=>r.toLowerCase().includes(y.toLowerCase())):J;return e.jsxs(h,{sx:{pb:6,minHeight:"80vh",bgcolor:"#f8fafc",px:{xs:2,md:4},pt:3},children:[e.jsx(We,{title:"Logistics Pricing Board",breadcrumbs:[{label:"Pricing Overview"}]}),e.jsxs(h,{sx:{display:"flex",flexDirection:"column",gap:4,mt:3,maxWidth:1200,mx:"auto"},children:[e.jsxs(U,{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(h,{sx:{display:"flex",alignItems:"center",gap:2,width:{xs:"100%",md:"auto"}},children:[e.jsx(h,{sx:{width:48,height:48,borderRadius:3,bgcolor:"primary.lighter",color:"primary.main",display:"flex",alignItems:"center",justifyContent:"center"},children:e.jsx(ve,{})}),e.jsxs(h,{children:[e.jsx(f,{variant:"caption",sx:{fontWeight:700,color:"text.secondary",letterSpacing:.8,textTransform:"uppercase"},children:"Active Logistics Provider"}),e.jsx(Ie,{sx:{mt:.5,minWidth:260,display:"block"},children:e.jsx(_e,{value:o||"",onChange:r=>I(r.target.value),displayEmpty:!0,IconComponent:Se,input:e.jsx(ke,{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:k.length===0?e.jsx(M,{value:"",disabled:!0,children:"Loading Providers..."}):k.map(r=>e.jsx(M,{value:r,sx:{fontWeight:600,py:1.5,borderRadius:1,mx:1},children:r},r))})})]})]}),e.jsxs(h,{sx:{display:"flex",gap:2,alignItems:"center"},children:[e.jsx(E,{placeholder:"Search weight slabs...",value:y,onChange:r=>T(r.target.value),InputProps:{startAdornment:e.jsx(Ae,{position:"start",children:e.jsx(le,{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(P,{variant:"contained",startIcon:e.jsx(ue,{}),onClick:()=>{n({company:o,weight_slab:"",zone:"",rate:""}),j(!1),_(!0)},sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2,py:1.2,fontWeight:700,whiteSpace:"nowrap"},children:"Add Rate"}),e.jsx(P,{variant:i?"contained":"outlined",startIcon:e.jsx(X,{}),onClick:()=>S(!i),sx:{borderRadius:2,py:1.2,fontWeight:700,whiteSpace:"nowrap",color:i?"white":"grey.700",borderColor:i?"transparent":"grey.300",bgcolor:i?"grey.800":"transparent","&:hover":{bgcolor:i?"grey.900":a("#000",.05),borderColor:i?"transparent":"grey.400"}},children:i?"Done":"Edit"})]})]}),o&&L.length>0&&e.jsxs(U,{elevation:0,sx:{borderRadius:4,border:"1px solid",borderColor:u?"#c01227":"grey.200",bgcolor:"white",transition:"border-color 0.3s ease, box-shadow 0.3s ease","&:hover":{borderColor:"#c01227",boxShadow:"0 8px 30px rgba(192, 18, 39, 0.08)"}},children:[e.jsxs(F,{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:()=>b(!u),children:[e.jsxs(F,{direction:"row",spacing:3,alignItems:"center",sx:{minWidth:260},children:[e.jsx(ce,{sx:{width:68,height:68,bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:800,fontSize:"1.8rem",borderRadius:3},children:(o||"A")[0].toUpperCase()}),e.jsxs(h,{children:[e.jsx(f,{variant:"overline",sx:{color:"grey.500",fontWeight:700,letterSpacing:1.2},children:"LOGISTICS PARTNER"}),e.jsx(f,{variant:"h5",sx:{fontWeight:800,color:"grey.900",lineHeight:1.1,mt:.25},children:o}),e.jsxs(f,{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"}}),L.length," Registered Location",L.length!==1&&"s"]})]})]}),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($,{size:"small",label:"First Mile",sx:{bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx($,{size:"small",label:"Mid Mile",sx:{bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx($,{size:"small",label:"Last Mile",sx:{bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}})]}),e.jsx(P,{onClick:r=>{r.stopPropagation(),b(!u)},sx:{borderRadius:2,py:1,px:3,fontWeight:700,color:u?"#c01227":"grey.700",bgcolor:u?a("#c01227",.1):"grey.100","&:hover":{bgcolor:a("#c01227",.15),color:"#c01227"}},endIcon:u?e.jsx(we,{}):e.jsx(Ce,{}),children:u?"Hide Coverage":"View Coverage Map"})]})]}),e.jsx(de,{in:u,timeout:"auto",unmountOnExit:!0,children:e.jsxs(h,{sx:{p:3,pt:0,borderTop:"1px dashed",borderColor:"grey.200",bgcolor:"grey.50",borderBottomLeftRadius:16,borderBottomRightRadius:16},children:[e.jsxs(h,{sx:{display:"flex",alignItems:"center",mb:2,mt:3},children:[e.jsx(Y,{sx:{color:"#c01227",fontSize:20,mr:1}}),e.jsx(f,{variant:"caption",sx:{color:"grey.600",fontWeight:700,letterSpacing:1},children:"ALL SERVICED AREAS:"})]}),e.jsx(h,{sx:{display:"flex",gap:1.5,flexWrap:"wrap"},children:L.map((r,s)=>e.jsx($,{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),boxShadow:"0 4px 12px rgba(192,18,39,0.08)"},transition:"border-color 0.2s ease, color 0.2s ease, background-color 0.2s ease, box-shadow 0.2s ease"}},r.id??s))})]})})]}),e.jsx(U,{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:m?e.jsx(h,{sx:{display:"flex",justifyContent:"center",alignItems:"center",py:10},children:e.jsx(xe,{size:48,thickness:4,sx:{color:"primary.light"}})}):e.jsx(ze,{children:e.jsxs(Ee,{sx:{minWidth:700},children:[e.jsx(Pe,{children:e.jsxs(O,{sx:{bgcolor:a("#f8fafc",.8)},children:[e.jsx(R,{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"}),B.map(r=>e.jsx(R,{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(Te,{children:B.length===0?e.jsx(O,{children:e.jsx(R,{colSpan:2,align:"center",sx:{py:8},children:e.jsx(f,{variant:"body1",color:"text.secondary",sx:{fontWeight:500},children:"No pricing data for this provider yet."})})}):K.length===0?e.jsx(O,{children:e.jsx(R,{colSpan:B.length+1,align:"center",sx:{py:8},children:e.jsx(f,{variant:"body1",color:"text.secondary",sx:{fontWeight:500},children:"No weight slabs match your search."})})}):K.map(r=>e.jsxs(O,{sx:{transition:"background-color 0.15s","&:hover":{bgcolor:a("#f1f5f9",.5)},"&:last-child td":{borderBottom:0}},children:[e.jsx(R,{sx:{px:4,py:2,fontWeight:600,color:"grey.800",fontSize:"0.9rem",borderBottom:"1px solid",borderColor:"grey.100"},children:r}),B.map(s=>{const c=q.find(z=>(z.weight_slab||"").trim()===r&&(z.zone||"").trim()===s),x=!!c,A=c?`${c.rate}`:"—";let D="";if(c&&c.updated_by){const z=re.find(ie=>ie.id===c.updated_by);D=`Last updated by: ${z?z.first_name:"ID "+c.updated_by}`}const se=()=>{n({id:c?c.id:void 0,company:o,weight_slab:r,zone:s,rate:c?String(c.rate):""}),j(!0),_(!0)};return e.jsx(R,{align:"center",sx:{py:1.5,borderBottom:"1px solid",borderColor:"grey.100",cursor:i?"pointer":"default",transition:"background 0.15s","&:hover":i?{bgcolor:a("#c01227",.04)}:{}},onClick:i?se:void 0,children:e.jsx(he,{title:i?x?"Click to edit rate":"Click to add rate":D,placement:"top",arrow:!0,disableHoverListener:!i&&!D,children:e.jsxs(h,{sx:{display:"flex",alignItems:"center",justifyContent:"center",gap:1},children:[e.jsx(f,{sx:{display:"inline-block",fontWeight:x?700:400,color:x?"success.dark":i?a("#c01227",.4):"grey.300",fontSize:x?"0.95rem":"0.85rem",bgcolor:x?a("#10b981",.08):"transparent",px:x?1.5:0,py:x?.5:0,borderRadius:2},children:A}),i&&(x?e.jsx(X,{sx:{fontSize:13,color:"grey.400"}}):e.jsx(ee,{sx:{fontSize:13,color:a("#c01227",.4)}}))]})})},s)})]},r))})]})})})]}),e.jsx(Ne,{open:C,onClose:()=>_(!1),onSave:()=>Q(),initialData:N,isCell:g,pricingList:t})]})}export{rr as default};