2 lines
17 KiB
JavaScript
2 lines
17 KiB
JavaScript
import{r as ie,j as e,i as ne,a as h,B as y,T as I,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-CZzGbxkv.js";import{d as pe,e as ee,D as he,a as fe,b as me,c as be}from"./EditOutlined-i6C2jrx8.js";import{d as ye}from"./Close-BkWpEqme.js";import{e as je,c as ve,b as re,d as Se,a as we}from"./StorefrontOutlined-4mMhizPl.js";import{P as Ce,a as N,A as ke,G as z}from"./PageHeader-BzLCnEhm.js";import{C as Y}from"./Link-CxKAnTsZ.js";import{F as Ie,S as Re,O as We,T as $}from"./TextField-BEFRiuHv.js";import{I as Le}from"./InputAdornment-DmcxLfyx.js";import{B}from"./Button-GeMuqY03.js";import{T as _e,a as Te,b as ze,c as J,d as Ee}from"./TableRow-TC6HPWUy.js";import{T as P}from"./TableCell-DjMuei2k.js";var K={},Ae=ne;Object.defineProperty(K,"__esModule",{value:!0});var te=K.default=void 0,Pe=Ae(ie()),$e=e;te=K.default=(0,Pe.default)((0,$e.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",Z=()=>{const o=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...o?{Authorization:`Bearer ${o}`}:{}}};async function Be(){const o=await fetch(`${G}/admin/carrier-pricing?limit=500`,{headers:Z()});if(!o.ok)throw new Error("Failed to fetch pricing");const x=await o.json();return Array.isArray(x)?{data:x}:x}async function Fe(o){const x=!!o.id,L=x?`${G}/admin/carrier-pricing/${o.id}`:`${G}/admin/carrier-pricing`,b=await fetch(L,{method:x?"PUT":"POST",headers:Z(),body:JSON.stringify({company:o.company,weight_slab:o.weight_slab,zone:o.zone,rate:String(o.rate)})});if(!b.ok){const j=await b.json().catch(()=>({}));throw new Error(j.error||j.message||"Failed to save pricing")}return b.status===204?{}:b.json().catch(()=>({}))}const Oe=["Local / Same","Within Tamilnadu","Interstate India"],Me=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];function He({open:o,onClose:x,onSave:L,initialData:m,isCell:b=!1,pricingList:j=[]}){const[i,_]=h.useState({}),[S,F]=h.useState(!1),[w,C]=h.useState(null);h.useEffect(()=>{o&&(_(m||{company:"",weight_slab:"",zone:"",rate:""}),C(null))},[o,m]);const T=u=>l=>_(n=>({...n,[u]:l.target.value})),E=u=>{const l=(u.company||"").toLowerCase().trim(),n=(u.weight_slab||"").toLowerCase().replace(/\s+/g,""),a=(u.zone||"").toLowerCase();return j.find(R=>{if((R.company||"").toLowerCase().trim()!==l)return!1;const s=(R.weight_slab||"").toLowerCase().replace(/\s+/g,"");let W=!1;if(n.includes("<500")?W=s.includes("<500")||s==="500g"||s==="0.5kg"||s==="0-500g"||s.includes("500gm")||s.includes("upto500"):n.includes("500g-1")||n==="1kg"||n==="upto1kg"||n.includes("0.5-1")||n.includes("500-1")?W=s.includes("500g-1")||s.includes("500-1")||s.includes("0.5-1")||s==="1kg"||s==="upto1kg":n.includes("1kg-2")||n==="2kg"||n==="upto2kg"||n.includes("1-2")||n.includes("1to2")?W=s.includes("1kg-2")||s.includes("1-2")||s.includes("1to2")||s==="2kg"||s==="upto2kg":n.includes("2kg-5")||n==="5kg"||n==="upto5kg"||n.includes("2-5")||n.includes("2to5")?W=s.includes("2kg-5")||s.includes("2-5")||s.includes("2to5")||s==="5kg"||s==="upto5kg":n.includes(">5")?W=s.includes(">5")||s.includes("above5")||s.includes("5+")||s.includes("10kg"):W=s===n,!W)return!1;const p=(R.zone||"").toLowerCase();return a.includes("local")||a.includes("same")||a.includes("city")||a==="l"?p.includes("local")||p.includes("same")||p.includes("city")||p==="l":a.includes("tamil")||a.includes("tn")||a.includes("state")||a==="s"?p.includes("tamil")||p.includes("tn")||p.includes("state")||p==="s":a.includes("inter")||a.includes("india")||a.includes("national")||a.includes("rest")||a==="n"?p.includes("inter")||p.includes("india")||p.includes("national")||p.includes("rest")||p==="n":p===a})||null},Q=async()=>{var u,l,n;if(!b){if(!((u=i.company)!=null&&u.trim())){C("Company is required.");return}if(!((l=i.weight_slab)!=null&&l.trim())){C("Weight slab is required.");return}if(!((n=i.zone)!=null&&n.trim())){C("Zone is required.");return}}if(!String(i.rate??"").trim()){C("Rate is required.");return}F(!0),C(null);try{let a={...i};if(!a.id){const R=E(a);R&&(a={...a,id:R.id})}await Fe(a),L(),x()}catch(a){console.error(a),C(a.message||"Failed to save. Please try again.")}finally{F(!1)}};return e.jsxs(he,{open:o,onClose:S?void 0:x,maxWidth:"xs",fullWidth:!0,children:[e.jsxs(fe,{sx:{fontWeight:800,display:"flex",alignItems:"center",justifyContent:"space-between"},children:[b?m!=null&&m.id?"Edit Rate":"Add Rate":m!=null&&m.id?"Edit Rate":"Add New Rate",e.jsx(xe,{size:"small",onClick:x,disabled:S,children:e.jsx(ye,{fontSize:"small"})})]}),e.jsxs(me,{dividers:!0,children:[w&&e.jsx(ke,{severity:"error",sx:{mb:2},children:w}),e.jsxs(z,{container:!0,spacing:2,children:[b?e.jsx(z,{item:!0,xs:12,children:e.jsxs(y,{sx:{p:1.5,borderRadius:2,bgcolor:d("#c01227",.04),border:"1px solid",borderColor:d("#c01227",.15),mb:.5},children:[e.jsx(I,{variant:"caption",sx:{fontWeight:700,color:"grey.500",letterSpacing:.5,display:"block",mb:.5},children:"UPDATING RATE FOR"}),e.jsx(I,{variant:"subtitle2",sx:{fontWeight:700,color:"grey.900"},children:i.company}),e.jsxs(I,{variant:"body2",sx:{color:"grey.600",mt:.25},children:[i.weight_slab," · ",i.zone]})]})}):e.jsxs(e.Fragment,{children:[e.jsx(z,{item:!0,xs:12,children:e.jsx($,{fullWidth:!0,label:"Company *",value:i.company||"",onChange:T("company"),size:"small"})}),e.jsx(z,{item:!0,xs:12,children:e.jsx($,{select:!0,fullWidth:!0,label:"Weight Slab *",value:i.weight_slab||"",onChange:T("weight_slab"),size:"small",children:Me.map(u=>e.jsx(U,{value:u,children:u},u))})}),e.jsx(z,{item:!0,xs:12,children:e.jsx($,{select:!0,fullWidth:!0,label:"Zone *",value:i.zone||"",onChange:T("zone"),size:"small",children:Oe.map(u=>e.jsx(U,{value:u,children:u},u))})})]}),e.jsx(z,{item:!0,xs:12,children:e.jsx($,{fullWidth:!0,label:"Rate (₹) *",value:i.rate||"",onChange:T("rate"),size:"small",placeholder:"e.g. 25 or 25-35",autoFocus:!0})})]})]}),e.jsxs(be,{sx:{p:2,bgcolor:"grey.50"},children:[e.jsx(B,{onClick:x,disabled:S,color:"inherit",children:"Cancel"}),e.jsx(B,{onClick:Q,disabled:S,variant:"contained",sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2},children:S?"Saving...":"Save Rate"})]})]})}async function qe(){let o=[],x=0;const L=50;let m=1;for(;x<m;){const b=await fetch(`${G}/admin/competitor-branches?limit=${L}&offset=${x}`,{headers:Z()});if(!b.ok)throw new Error("Failed to fetch surveys");const j=await b.json();if(!j.data||j.data.length===0)break;const i=j.data.filter(_=>!o.some(S=>S.id===_.id));if(i.length===0)break;o=[...o,...i],m=j.total||0,x+=L}return{data:o,total:o.length}}function rr(){const[o,x]=h.useState([]),[L,m]=h.useState([]),[b,j]=h.useState(!0),[i,_]=h.useState(""),[S,F]=h.useState(""),[w,C]=h.useState(!1),[T,E]=h.useState(!1),[Q,u]=h.useState(null),[l,n]=h.useState(!1),[a,R]=h.useState(!1),[s,W]=h.useState([]),p=()=>{let r=!1;return j(!0),Promise.all([Be().catch(()=>({data:[]})),qe().catch(()=>({data:[]})),ge().catch(()=>[])]).then(([v,H,g])=>{r||(x(Array.isArray(v)?v:v.data||[]),m(H.data||H||[]),W(Array.isArray(g)?g:g.data||[]))}).catch(v=>console.error("Failed to fetch data",v)).finally(()=>{r||j(!1)}),()=>{r=!0}};h.useEffect(()=>p(),[]);const A=[...new Set(o.map(r=>r.company).filter(Boolean))];h.useEffect(()=>{!i&&A.length>0&&_(A[0])},[A,i]);let O=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];if(S){const r=S.toLowerCase();O=O.filter(v=>v.toLowerCase().includes(r))}const se=o.filter(r=>r.company===i),M=L.filter(r=>(r.company||"").toLowerCase()===(i||"").toLowerCase()),X=["Local / Same","Within Tamilnadu","Interstate India"];return e.jsxs(y,{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(y,{sx:{display:"flex",flexDirection:"column",gap:4,mt:3,maxWidth:1200,mx:"auto"},children:[e.jsxs(Y,{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(y,{sx:{display:"flex",alignItems:"center",gap:2,width:{xs:"100%",md:"auto"}},children:[e.jsx(y,{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(y,{children:[e.jsx(I,{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(Re,{value:i||"",onChange:r=>_(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:A.length===0?e.jsx(U,{value:"",disabled:!0,children:"Loading Providers..."}):A.map(r=>e.jsx(U,{value:r,sx:{fontWeight:600,py:1.5,borderRadius:1,mx:1},children:r},r))})})]})]}),e.jsxs(y,{sx:{display:"flex",gap:2,alignItems:"center"},children:[e.jsx($,{placeholder:"Search weight slabs...",value:S,onChange:r=>F(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(B,{variant:"contained",startIcon:e.jsx(pe,{}),onClick:()=>{u({company:i,weight_slab:"",zone:"",rate:""}),R(!1),E(!0)},sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2,py:1.2,fontWeight:700,whiteSpace:"nowrap"},children:"Add Rate"}),e.jsx(B,{variant:l?"contained":"outlined",startIcon:e.jsx(ee,{}),onClick:()=>n(!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"})]})]}),i&&M.length>0&&e.jsxs(Y,{elevation:0,sx:{borderRadius:4,border:"1px solid",borderColor:w?"#c01227":"grey.200",bgcolor:"white",transition:"all 0.3s ease","&:hover":{borderColor:"#c01227",boxShadow:"0 8px 30px rgba(192, 18, 39, 0.08)",transform:"translateY(-2px)"}},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:()=>C(!w),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:(i||"A")[0].toUpperCase()}),e.jsxs(y,{children:[e.jsx(I,{variant:"overline",sx:{color:"grey.500",fontWeight:700,letterSpacing:1.2},children:"LOGISTICS PARTNER"}),e.jsx(I,{variant:"h5",sx:{fontWeight:800,color:"grey.900",lineHeight:1.1,mt:.25},children:i}),e.jsxs(I,{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(B,{onClick:r=>{r.stopPropagation(),C(!w)},sx:{borderRadius:2,py:1,px:3,fontWeight:700,color:w?"#c01227":"grey.700",bgcolor:w?d("#c01227",.1):"grey.100","&:hover":{bgcolor:d("#c01227",.15),color:"#c01227"}},endIcon:w?e.jsx(Se,{}):e.jsx(we,{}),children:w?"Hide Coverage":"View Coverage Map"})]})]}),e.jsx(ce,{in:w,timeout:"auto",unmountOnExit:!0,children:e.jsxs(y,{sx:{p:3,pt:0,borderTop:"1px dashed",borderColor:"grey.200",bgcolor:"grey.50",borderBottomLeftRadius:16,borderBottomRightRadius:16},children:[e.jsxs(y,{sx:{display:"flex",alignItems:"center",mb:2,mt:3},children:[e.jsx(re,{sx:{color:"#c01227",fontSize:20,mr:1}}),e.jsx(I,{variant:"caption",sx:{color:"grey.600",fontWeight:700,letterSpacing:1},children:"ALL SERVICED AREAS:"})]}),e.jsx(y,{sx:{display:"flex",gap:1.5,flexWrap:"wrap"},children:M.map((r,v)=>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),transform:"translateY(-1px)",boxShadow:"0 4px 12px rgba(192,18,39,0.08)"},transition:"all 0.2s ease"}},r.id??v))})]})})]}),e.jsx(Y,{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:b?e.jsx(y,{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(J,{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"}),X.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(Ee,{children:O.length===0?e.jsx(J,{children:e.jsx(P,{colSpan:4,align:"center",sx:{py:8},children:e.jsx(I,{variant:"body1",color:"text.secondary",sx:{fontWeight:500},children:"No pricing data matches your search."})})}):O.map(r=>{const v=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=v.find(k=>{const c=(k.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(J,{sx:{transition:"all 0.2s","&:hover":{bgcolor:d("#f1f5f9",.5),transform:"scale(1.001)"},"&: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}),X.map(g=>{const t=H(g),k=t!=="—",c=v.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:i,weight_slab:r,zone:g,rate:c?String(c.rate):""}),R(!0),E(!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?k?"Click to edit rate":"Click to add rate":V,placement:"top",arrow:!0,disableHoverListener:!l&&!V,children:e.jsxs(y,{sx:{display:"flex",alignItems:"center",justifyContent:"center",gap:1},children:[e.jsx(I,{sx:{display:"inline-block",fontWeight:k?700:400,color:k?"success.dark":l?d("#c01227",.4):"grey.300",fontSize:k?"0.95rem":"0.85rem",bgcolor:k?d("#10b981",.08):"transparent",px:k?1.5:0,py:k?.5:0,borderRadius:2},children:t}),l&&(k?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:T,onClose:()=>E(!1),onSave:()=>p(),initialData:Q,isCell:a,pricingList:o})]})}export{rr as default};
|