Files
doormile_crm_build/dist/assets/Pricing-CIHP-hAm.js
2026-06-20 15:13:25 +05:30

2 lines
17 KiB
JavaScript

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