2 lines
14 KiB
JavaScript
2 lines
14 KiB
JavaScript
import{r as oe,j as e,i as ne,b as l,S as _,B as j,T as x,l as pe,M as D,A as me,n as xe,C as ue,o as fe,e as ye,I as je}from"./index-D7LVXeIK.js";import{d as ve,D as be,a as Se,e as we,b as Ce,c as _e}from"./Close-D3HB9j5y.js";import{d as se}from"./EditOutlined-DS7hq7Cq.js";import{c as We,d as ae,a as Ie,b as ze}from"./StorefrontOutlined-Be07_1-Y.js";import{P as ke,C as M,A as Ae,G as W}from"./PageHeader-naLAT9xr.js";import{C as K}from"./Link-BEif5YS2.js";import{T as $,F as Ee,S as Pe,O as Re}from"./TextField-Sd_pshci.js";import{I as Te}from"./InputAdornment-CMIlL2rx.js";import{B as F}from"./Button-Bqp5awT1.js";import{T as Le,a as $e,b as Fe,c as q,d as Oe}from"./TableRow-jBgitpDf.js";import{T as I}from"./TableCell-DreGbAeW.js";import{S as Be}from"./Snackbar-DOheZfWW.js";var Q={},He=ne;Object.defineProperty(Q,"__esModule",{value:!0});var le=Q.default=void 0,Me=He(oe()),qe=e;le=Q.default=(0,Me.default)((0,qe.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");var J={},De=ne;Object.defineProperty(J,"__esModule",{value:!0});var ce=J.default=void 0,Ne=De(oe()),Ve=e;ce=J.default=(0,Ne.default)((0,Ve.jsx)("path",{d:"M11 13V9c0-.55-.45-1-1-1H6V6h5V4H8.5V3h-2v1H5c-.55 0-1 .45-1 1v4c0 .55.45 1 1 1h4v2H4v2h2.5v1h2v-1H10c.55 0 1-.45 1-1m8.59-.48-5.66 5.65-2.83-2.83-1.41 1.42L13.93 21 21 13.93z"}),"PriceCheckOutlined");const N="https://api.doormile.com/api/v1",X=()=>{const s=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...s?{Authorization:`Bearer ${s}`}:{}}};async function Ge(){const s=await fetch(`${N}/admin/carrier-pricing?limit=1000`,{headers:X()});if(!s.ok)throw new Error("Failed to fetch pricing");const n=await s.json();return Array.isArray(n)?{data:n}:n}async function Ze(s){const n=!!s.id,b=n?`${N}/admin/carrier-pricing/${s.id}`:`${N}/admin/carrier-pricing`,f=await fetch(b,{method:n?"PUT":"POST",headers:X(),body:JSON.stringify({company:s.company,weight_slab:s.weight_slab,zone:s.zone||"",service_type:s.service_type||"",rate:String(s.rate)})});if(!f.ok){const C=await f.json().catch(()=>({}));throw new Error(C.error||C.message||"Failed to save pricing")}return f.status===204?{}:f.json().catch(()=>({}))}const Ue=["Local / Same","Within Tamilnadu","Interstate India"],Ke=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];function Qe({open:s,onClose:n,onSave:b,initialData:u,isCell:f=!1,pricingList:C=[],providerColumns:p=[],providerUsesServiceType:S=!1}){const[o,z]=l.useState({}),[h,k]=l.useState(!1),[O,m]=l.useState(null);l.useEffect(()=>{s&&(z(u||{company:"",weight_slab:"",zone:"",rate:""}),m(null))},[s,u]);const A=i=>y=>z(w=>({...w,[i]:y.target.value})),B=i=>(i.zone||"").trim()||(i.service_type||"").trim(),g=i=>{const y=(i.company||"").trim(),w=(i.weight_slab||"").trim(),c=(i.zone||i.service_type||"").trim();return C.find(v=>(v.company||"").trim()===y&&(v.weight_slab||"").trim()===w&&B(v)===c)||null},V=async()=>{var y,w;const i=(o.zone||o.service_type||"").trim();if(!f){if(!((y=o.company)!=null&&y.trim())){m("Company is required.");return}if(!((w=o.weight_slab)!=null&&w.trim())){m("Weight slab is required.");return}if(!i){m("Zone / Service Type is required.");return}}if(!String(o.rate??"").trim()){m("Rate is required.");return}k(!0),m(null);try{let c={...o};if(f||(c={...c,zone:S?"":i,service_type:S?i:c.service_type||""}),!c.id){const v=g(c);v&&(c={...c,id:v.id})}await Ze(c),b(),n()}catch(c){console.error(c),m(c.message||"Failed to save. Please try again.")}finally{k(!1)}};return e.jsxs(e.Fragment,{children:[e.jsx(Be,{open:!!O,autoHideDuration:6e3,onClose:()=>m(null),anchorOrigin:{vertical:"top",horizontal:"right"},sx:{zIndex:9999},children:e.jsx(Ae,{onClose:()=>m(null),severity:"error",variant:"filled",sx:{width:"100%",boxShadow:3},children:O})}),e.jsxs(be,{open:s,onClose:h?void 0:n,maxWidth:"xs",fullWidth:!0,children:[e.jsxs(Se,{sx:{fontWeight:800,display:"flex",alignItems:"center",justifyContent:"space-between"},children:[f?u!=null&&u.id?"Edit Rate":"Add Rate":u!=null&&u.id?"Edit Rate":"Add New Rate",e.jsx(je,{size:"small",onClick:n,disabled:h,children:e.jsx(we,{fontSize:"small"})})]}),e.jsx(Ce,{dividers:!0,children:e.jsxs(W,{container:!0,spacing:2,children:[f?e.jsx(W,{item:!0,xs:12,children:e.jsxs(j,{sx:{p:1.5,borderRadius:2,bgcolor:"primary.lighter",border:"1px solid",borderColor:"primary.light",mb:.5},children:[e.jsx(x,{variant:"caption",sx:{fontWeight:700,color:"grey.500",letterSpacing:.5,display:"block",mb:.5},children:"UPDATING RATE FOR"}),e.jsx(x,{variant:"subtitle2",sx:{fontWeight:700,color:"grey.900"},children:o.company}),e.jsxs(x,{variant:"body2",sx:{color:"grey.600",mt:.25},children:[o.weight_slab," · ",o.zone||o.service_type]})]})}):e.jsxs(e.Fragment,{children:[e.jsx(W,{item:!0,xs:12,children:e.jsx($,{fullWidth:!0,label:"Company *",value:o.company||"",onChange:A("company"),size:"small"})}),e.jsx(W,{item:!0,xs:12,children:e.jsx($,{select:!0,fullWidth:!0,label:"Weight Slab *",value:o.weight_slab||"",onChange:A("weight_slab"),size:"small",children:Ke.map(i=>e.jsx(D,{value:i,children:i},i))})}),e.jsx(W,{item:!0,xs:12,children:e.jsx($,{select:!0,fullWidth:!0,label:S?"Service Type *":"Zone / Service Type *",value:o.zone||o.service_type||"",onChange:i=>z(y=>({...y,zone:S?"":i.target.value,service_type:S?i.target.value:""})),size:"small",children:(p.length>0?p:Ue).map(i=>e.jsx(D,{value:i,children:i},i))})})]}),e.jsx(W,{item:!0,xs:12,children:e.jsx($,{fullWidth:!0,label:"Rate (₹) *",value:o.rate||"",onChange:A("rate"),size:"small",placeholder:"e.g. 25 or 25-35",autoFocus:!0})})]})}),e.jsxs(_e,{sx:{px:3,py:2},children:[e.jsx(F,{onClick:n,disabled:h,color:"inherit",children:"Cancel"}),e.jsx(F,{onClick:V,disabled:h,variant:"contained",children:h?"Saving...":"Save Rate"})]})]})]})}async function Je(){const s=await fetch(`${N}/admin/competitor-branches?limit=1000`,{headers:X()});if(!s.ok)throw new Error("Failed to fetch surveys");const n=await s.json(),b=Array.isArray(n)?n:n.data||[];return{data:b,total:b.length}}function dr(){const[s,n]=l.useState([]),[b,u]=l.useState([]),[f,C]=l.useState(!0),[p,S]=l.useState(""),[o,z]=l.useState(""),[h,k]=l.useState(!1),[O,m]=l.useState(!1),[A,B]=l.useState(null),[g,V]=l.useState(!1),[i,y]=l.useState(!1),[w,c]=l.useState([]),v=()=>{let r=!1;return C(!0),Promise.all([Ge().catch(()=>({data:[]})),Je().catch(()=>({data:[]})),ye().catch(()=>[])]).then(([t,a,d])=>{r||(n(Array.isArray(t)?t:t.data||[]),u(a.data||a||[]),c(Array.isArray(d)?d:d.data||[]))}).catch(t=>console.error("Failed to fetch data",t)).finally(()=>{r||C(!1)}),()=>{r=!0}};l.useEffect(()=>v(),[]);const E=[...new Set(s.map(r=>r.company).filter(Boolean))];l.useEffect(()=>{!p&&E.length>0&&S(E[0])},[E,p]);const de=s.filter(r=>r.company===p),Y=r=>(r||"").toLowerCase().trim().replace(/\s+/g," "),G=Y(p),H=b.filter(r=>{const t=Y(r.company);return t===G||t.startsWith(G)||G.startsWith(t)}),P=[...de].sort((r,t)=>(t.id||0)-(r.id||0)),ee=r=>{const t=(r||"").toLowerCase().replace(/\s+/g,""),a=t.match(/[\d.]+/g);if(!a)return 9999;const d=parseFloat(a[0]),T=t.includes("kg")?d*1e3:d;return t.startsWith("<")||t.startsWith("upto")?T-.5:t.startsWith(">")||t.includes("above")||t.endsWith("+")?T+.5:T},re=r=>(r.zone||"").trim()||(r.service_type||"").trim(),Z=P.length>0&&P.every(r=>!(r.zone||"").trim()),R=[...new Set(P.map(re).filter(Boolean))].sort(),te=[...new Set(P.map(r=>(r.weight_slab||"").trim()).filter(Boolean))].sort((r,t)=>ee(r)-ee(t)),ie=o?te.filter(r=>r.toLowerCase().includes(o.toLowerCase())):te;return e.jsxs(e.Fragment,{children:[e.jsx(ke,{title:"Logistics Pricing Board",breadcrumbs:[{label:"Pricing Overview"}]}),e.jsxs(_,{spacing:3,children:[e.jsxs(K,{sx:{overflow:"hidden"},children:[e.jsxs(j,{sx:{px:{xs:2,sm:2.5},py:2,borderBottom:1,borderColor:"divider",display:"flex",alignItems:"center",gap:1.5,background:r=>`linear-gradient(90deg, ${r.palette.primary.lighter}66 0%, ${r.palette.background.paper} 70%)`},children:[e.jsx(j,{sx:{width:40,height:40,borderRadius:2,bgcolor:"primary.lighter",color:"primary.main",display:"inline-flex",alignItems:"center",justifyContent:"center"},children:e.jsx(ce,{fontSize:"small"})}),e.jsxs(j,{children:[e.jsx(x,{variant:"subtitle1",sx:{fontWeight:700,color:"grey.800",lineHeight:1.2},children:"Pricing Matrix"}),e.jsx(x,{variant:"caption",color:"text.secondary",children:"Manage carrier rates by weight slab and zone"})]})]}),e.jsxs(_,{direction:{xs:"column",md:"row"},spacing:1.5,sx:{p:2},alignItems:{md:"center"},children:[e.jsx($,{size:"small",placeholder:"Search weight slabs…",value:o,onChange:r=>z(r.target.value),sx:{width:{xs:"100%",md:300}},InputProps:{startAdornment:e.jsx(Te,{position:"start",children:e.jsx(pe,{fontSize:"small"})})}}),e.jsx(j,{sx:{flexGrow:1}}),e.jsx(Ee,{size:"small",sx:{minWidth:220},children:e.jsx(Pe,{value:p||"",onChange:r=>S(r.target.value),displayEmpty:!0,size:"small",IconComponent:We,input:e.jsx(Re,{sx:{fontWeight:700}}),children:E.length===0?e.jsx(D,{value:"",disabled:!0,children:"Loading Providers..."}):E.map(r=>e.jsx(D,{value:r,sx:{fontWeight:600},children:r},r))})}),e.jsx(F,{variant:"contained",startIcon:e.jsx(ve,{}),onClick:()=>{B({company:p,weight_slab:"",zone:"",rate:""}),y(!1),m(!0)},sx:{whiteSpace:"nowrap"},children:"Add Rate"}),e.jsx(F,{variant:g?"contained":"outlined",color:g?"inherit":"primary",startIcon:e.jsx(se,{}),onClick:()=>V(!g),sx:{whiteSpace:"nowrap",...g&&{bgcolor:"grey.800",color:"white","&:hover":{bgcolor:"grey.900"}}},children:g?"Done":"Edit"})]})]}),p&&H.length>0&&e.jsxs(K,{elevation:0,sx:{border:"1px solid",borderColor:h?"primary.main":"grey.200",transition:"border-color 0.3s ease, box-shadow 0.3s ease","&:hover":{borderColor:"primary.main",boxShadow:"0 8px 30px rgba(192, 18, 39, 0.08)"}},children:[e.jsxs(_,{direction:{xs:"column",md:"row"},spacing:3,sx:{p:{xs:2,sm:2.5},cursor:"pointer",userSelect:"none"},alignItems:{xs:"flex-start",md:"center"},justifyContent:"space-between",onClick:()=>k(!h),children:[e.jsxs(_,{direction:"row",spacing:2,alignItems:"center",sx:{minWidth:260},children:[e.jsx(me,{sx:{width:42,height:42,bgcolor:"primary.lighter",color:"primary.main",fontWeight:800,borderRadius:2},children:(p||"A")[0].toUpperCase()}),e.jsxs(j,{children:[e.jsx(x,{variant:"overline",sx:{color:"grey.500",fontWeight:700,letterSpacing:1.2},children:"LOGISTICS PARTNER"}),e.jsx(x,{variant:"h5",sx:{fontWeight:800,color:"grey.900",lineHeight:1.1,mt:.25},children:p}),e.jsxs(x,{variant:"body2",sx:{color:"grey.600",mt:1,fontWeight:500,display:"flex",alignItems:"center"},children:[e.jsx(ae,{sx:{fontSize:16,mr:.5,color:"primary.main"}}),H.length," Registered Location",H.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(M,{size:"small",label:"First Mile",sx:{bgcolor:"primary.lighter",color:"primary.main",fontWeight:700}}),e.jsx(M,{size:"small",label:"Mid Mile",sx:{bgcolor:"primary.lighter",color:"primary.main",fontWeight:700}}),e.jsx(M,{size:"small",label:"Last Mile",sx:{bgcolor:"primary.lighter",color:"primary.main",fontWeight:700}})]}),e.jsx(F,{onClick:r=>{r.stopPropagation(),k(!h)},sx:{borderRadius:2,py:1,px:3,fontWeight:700,color:h?"primary.main":"grey.700",bgcolor:h?"primary.lighter":"grey.100","&:hover":{bgcolor:"primary.lighter",color:"primary.main"}},endIcon:h?e.jsx(Ie,{}):e.jsx(ze,{}),children:h?"Hide Coverage":"View Coverage Map"})]})]}),e.jsx(xe,{in:h,timeout:"auto",unmountOnExit:!0,children:e.jsxs(j,{sx:{p:2,borderTop:"1px dashed",borderColor:"grey.200",bgcolor:"grey.50"},children:[e.jsxs(j,{sx:{display:"flex",alignItems:"center",mb:1.5},children:[e.jsx(ae,{sx:{color:"primary.main",fontSize:20,mr:1}}),e.jsx(x,{variant:"caption",sx:{color:"grey.600",fontWeight:700,letterSpacing:1},children:"ALL SERVICED AREAS:"})]}),e.jsx(j,{sx:{display:"flex",gap:1,flexWrap:"wrap"},children:H.map((r,t)=>e.jsx(M,{label:r.area||r.address,sx:{fontWeight:600,bgcolor:"background.paper",color:"grey.800",border:"1px solid",borderColor:"grey.300","&:hover":{borderColor:"primary.main",color:"primary.main",bgcolor:"primary.lighter"},transition:"border-color 0.2s ease, color 0.2s ease, background-color 0.2s ease"}},r.id??t))})]})})]}),e.jsx(K,{sx:{overflow:"hidden"},children:f?e.jsx(j,{sx:{display:"flex",justifyContent:"center",alignItems:"center",py:10},children:e.jsx(ue,{size:40,sx:{color:"primary.light"}})}):e.jsx(Le,{children:e.jsxs($e,{sx:{minWidth:700},children:[e.jsx(Fe,{children:e.jsxs(q,{sx:{"& th":{bgcolor:"grey.50",fontWeight:700,color:"grey.700",textTransform:"uppercase",fontSize:"0.72rem",letterSpacing:.4}},children:[e.jsx(I,{children:"Weight Slab"}),R.map(r=>e.jsx(I,{align:"center",children:r},r))]})}),e.jsx(Oe,{children:R.length===0?e.jsx(q,{children:e.jsx(I,{colSpan:1,align:"center",sx:{py:8},children:e.jsx(x,{variant:"body1",color:"text.secondary",sx:{fontWeight:500},children:"No pricing data for this provider yet."})})}):ie.length===0?e.jsx(q,{children:e.jsx(I,{colSpan:R.length+1,align:"center",sx:{py:8},children:e.jsx(x,{variant:"body1",color:"text.secondary",sx:{fontWeight:500},children:"No weight slabs match your search."})})}):ie.map(r=>e.jsxs(q,{hover:!0,sx:{"&:last-child td":{borderBottom:0}},children:[e.jsx(I,{sx:{fontWeight:600,color:"grey.800"},children:r}),R.map(t=>{const a=P.find(L=>(L.weight_slab||"").trim()===r&&re(L)===t),d=!!a,T=a?`₹${a.rate}`:"—";let U="";if(a&&a.updated_by){const L=w.find(ge=>ge.id===a.updated_by);U=`Last updated by: ${L?L.first_name:"ID "+a.updated_by}`}const he=()=>{B({id:a?a.id:void 0,company:p,weight_slab:r,zone:Z?"":t,service_type:Z?t:(a==null?void 0:a.service_type)||"",rate:a?String(a.rate):""}),y(!0),m(!0)};return e.jsx(I,{align:"center",sx:{cursor:g?"pointer":"default",transition:"background 0.15s","&:hover":g?{bgcolor:"primary.lighter"}:{}},onClick:g?he:void 0,children:e.jsx(fe,{title:g?d?"Click to edit rate":"Click to add rate":U,placement:"top",arrow:!0,disableHoverListener:!g&&!U,children:e.jsxs(j,{sx:{display:"flex",alignItems:"center",justifyContent:"center",gap:1},children:[e.jsx(x,{sx:{display:"inline-block",fontWeight:d?700:400,color:d?"success.dark":g?"primary.light":"grey.300",fontSize:d?"0.95rem":"0.85rem",bgcolor:d?"success.lighter":"transparent",px:d?1.5:0,py:d?.5:0,borderRadius:2},children:T}),g&&(d?e.jsx(se,{sx:{fontSize:13,color:"grey.400"}}):e.jsx(le,{sx:{fontSize:13,color:"primary.light"}}))]})})},t)})]},r))})]})})})]}),e.jsx(Qe,{open:O,onClose:()=>m(!1),onSave:()=>v(),initialData:A,isCell:i,pricingList:s,providerColumns:R,providerUsesServiceType:Z})]})}export{dr as default};
|