Files
doormile_crm_build/dist/assets/Pricing-CttgMHSv.js

2 lines
13 KiB
JavaScript

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