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