Files
doormile_crm_build/dist/assets/Pricing-DRDC22ok.js
2026-06-20 15:58:44 +05:30

2 lines
15 KiB
JavaScript

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