2 lines
15 KiB
JavaScript
2 lines
15 KiB
JavaScript
import{r as de,j as e,i as he,b as d,S as B,l as xe,z as n,B as y,T as j,M as N,E as ge,n as pe,C as ue,o as fe,e as me,I as be}from"./index-B539dxxT.js";import{d as ye,D as je,a as ve,e as Se,b as we,c as Ce}from"./Close-DfGDV2pn.js";import{d as se}from"./EditOutlined-CEMoj7us.js";import{e as _e,c as We,d as ie,a as Re,b as Ie}from"./StorefrontOutlined-BAUlOqtW.js";import{P as ze,C as q,A as ke,G as W}from"./PageHeader-vd12s20e.js";import{T as F,F as Ae,S as Ee,O as Pe}from"./TextField-Cl3T8i0P.js";import{I as Te}from"./InputAdornment-BjFJvkmB.js";import{B as O}from"./Button-D7QX3_Jz.js";import{C as Q}from"./Link-DlAvCPCg.js";import{T as Le,a as Be,b as Fe,c as D,d as Oe}from"./TableRow-DdCvjxdw.js";import{T as R}from"./TableCell-Cw928AWh.js";import{S as $e}from"./Snackbar-CovgXSVI.js";var J={},Me=he;Object.defineProperty(J,"__esModule",{value:!0});var ae=J.default=void 0,He=Me(de()),qe=e;ae=J.default=(0,He.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");const G="https://api.doormile.com/api/v1",X=()=>{const s=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...s?{Authorization:`Bearer ${s}`}:{}}};async function De(){const s=await fetch(`${G}/admin/carrier-pricing?limit=1000`,{headers:X()});if(!s.ok)throw new Error("Failed to fetch pricing");const l=await s.json();return Array.isArray(l)?{data:l}:l}async function Ne(s){const l=!!s.id,S=l?`${G}/admin/carrier-pricing/${s.id}`:`${G}/admin/carrier-pricing`,m=await fetch(S,{method:l?"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(!m.ok){const _=await m.json().catch(()=>({}));throw new Error(_.error||_.message||"Failed to save pricing")}return m.status===204?{}:m.json().catch(()=>({}))}const Ge=["Local / Same","Within Tamilnadu","Interstate India"],Ze=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];function Ue({open:s,onClose:l,onSave:S,initialData:f,isCell:m=!1,pricingList:_=[],providerColumns:p=[],providerUsesServiceType:w=!1}){const[a,I]=d.useState({}),[g,z]=d.useState(!1),[$,u]=d.useState(null);d.useEffect(()=>{s&&(I(f||{company:"",weight_slab:"",zone:"",rate:""}),u(null))},[s,f]);const k=o=>b=>I(C=>({...C,[o]:b.target.value})),M=o=>(o.zone||"").trim()||(o.service_type||"").trim(),c=o=>{const b=(o.company||"").trim(),C=(o.weight_slab||"").trim(),h=(o.zone||o.service_type||"").trim();return _.find(v=>(v.company||"").trim()===b&&(v.weight_slab||"").trim()===C&&M(v)===h)||null},Z=async()=>{var b,C;const o=(a.zone||a.service_type||"").trim();if(!m){if(!((b=a.company)!=null&&b.trim())){u("Company is required.");return}if(!((C=a.weight_slab)!=null&&C.trim())){u("Weight slab is required.");return}if(!o){u("Zone / Service Type is required.");return}}if(!String(a.rate??"").trim()){u("Rate is required.");return}z(!0),u(null);try{let h={...a};if(m||(h={...h,zone:w?"":o,service_type:w?o:h.service_type||""}),!h.id){const v=c(h);v&&(h={...h,id:v.id})}await Ne(h),S(),l()}catch(h){console.error(h),u(h.message||"Failed to save. Please try again.")}finally{z(!1)}};return e.jsxs(e.Fragment,{children:[e.jsx($e,{open:!!$,autoHideDuration:6e3,onClose:()=>u(null),anchorOrigin:{vertical:"top",horizontal:"right"},sx:{zIndex:9999},children:e.jsx(ke,{onClose:()=>u(null),severity:"error",variant:"filled",sx:{width:"100%",boxShadow:3},children:$})}),e.jsxs(je,{open:s,onClose:g?void 0:l,maxWidth:"xs",fullWidth:!0,children:[e.jsxs(ve,{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(be,{size:"small",onClick:l,disabled:g,children:e.jsx(Se,{fontSize:"small"})})]}),e.jsx(we,{dividers:!0,children:e.jsxs(W,{container:!0,spacing:2,children:[m?e.jsx(W,{item:!0,xs:12,children:e.jsxs(y,{sx:{p:1.5,borderRadius:2,bgcolor:n("#c01227",.04),border:"1px solid",borderColor:n("#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:a.company}),e.jsxs(j,{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:Ze.map(o=>e.jsx(N,{value:o,children:o},o))})}),e.jsx(W,{item:!0,xs:12,children:e.jsx(F,{select:!0,fullWidth:!0,label:w?"Service Type *":"Zone / Service Type *",value:a.zone||a.service_type||"",onChange:o=>I(b=>({...b,zone:w?"":o.target.value,service_type:w?o.target.value:""})),size:"small",children:(p.length>0?p:Ge).map(o=>e.jsx(N,{value:o,children:o},o))})})]}),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(Ce,{sx:{p:2,bgcolor:"grey.50"},children:[e.jsx(O,{onClick:l,disabled:g,color:"inherit",children:"Cancel"}),e.jsx(O,{onClick:Z,disabled:g,variant:"contained",sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2},children:g?"Saving...":"Save Rate"})]})]})]})}async function Ve(){const s=await fetch(`${G}/admin/competitor-branches?limit=1000`,{headers:X()});if(!s.ok)throw new Error("Failed to fetch surveys");const l=await s.json(),S=Array.isArray(l)?l:l.data||[];return{data:S,total:S.length}}function nr(){const[s,l]=d.useState([]),[S,f]=d.useState([]),[m,_]=d.useState(!0),[p,w]=d.useState(""),[a,I]=d.useState(""),[g,z]=d.useState(!1),[$,u]=d.useState(!1),[k,M]=d.useState(null),[c,Z]=d.useState(!1),[o,b]=d.useState(!1),[C,h]=d.useState([]),v=()=>{let r=!1;return _(!0),Promise.all([De().catch(()=>({data:[]})),Ve().catch(()=>({data:[]})),me().catch(()=>[])]).then(([t,i,x])=>{r||(l(Array.isArray(t)?t:t.data||[]),f(i.data||i||[]),h(Array.isArray(x)?x:x.data||[]))}).catch(t=>console.error("Failed to fetch data",t)).finally(()=>{r||_(!1)}),()=>{r=!0}};d.useEffect(()=>v(),[]);const A=[...new Set(s.map(r=>r.company).filter(Boolean))];d.useEffect(()=>{!p&&A.length>0&&w(A[0])},[A,p]);const ne=s.filter(r=>r.company===p),Y=r=>(r||"").toLowerCase().trim().replace(/\s+/g," "),U=Y(p),H=S.filter(r=>{const t=Y(r.company);return t===U||t.startsWith(U)||U.startsWith(t)}),E=[...ne].sort((r,t)=>(t.id||0)-(r.id||0)),ee=r=>{const t=(r||"").toLowerCase().replace(/\s+/g,""),i=t.match(/[\d.]+/g);if(!i)return 9999;const x=parseFloat(i[0]),T=t.includes("kg")?x*1e3:x;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(),V=E.length>0&&E.every(r=>!(r.zone||"").trim()),P=[...new Set(E.map(re).filter(Boolean))].sort(),te=[...new Set(E.map(r=>(r.weight_slab||"").trim()).filter(Boolean))].sort((r,t)=>ee(r)-ee(t)),oe=a?te.filter(r=>r.toLowerCase().includes(a.toLowerCase())):te;return e.jsxs(e.Fragment,{children:[e.jsx(ze,{title:"Logistics Pricing Board",breadcrumbs:[{label:"Pricing Overview"}],action:e.jsxs(B,{direction:"row",spacing:1.5,alignItems:"center",sx:{width:{xs:"100%",sm:"auto"}},children:[e.jsx(F,{placeholder:"Search weight slabs...",size:"small",value:a,onChange:r=>I(r.target.value),InputProps:{startAdornment:e.jsx(Te,{position:"start",children:e.jsx(xe,{fontSize:"small"})}),sx:{borderRadius:2}},sx:{width:{xs:"100%",sm:200},bgcolor:"background.paper",borderRadius:1}}),e.jsx(O,{variant:"contained",startIcon:e.jsx(ye,{}),onClick:()=>{M({company:p,weight_slab:"",zone:"",rate:""}),b(!1),u(!0)},sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2,fontWeight:700,whiteSpace:"nowrap"},children:"Add Rate"}),e.jsx(O,{variant:c?"contained":"outlined",startIcon:e.jsx(se,{}),onClick:()=>Z(!c),sx:{borderRadius: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":n("#000",.05),borderColor:c?"transparent":"grey.400"}},children:c?"Done":"Edit"})]})}),e.jsxs(y,{sx:{display:"flex",flexDirection:"column",gap:3,mt:1,width:"100%"},children:[e.jsx(Q,{sx:{p:2.5,borderRadius:4,bgcolor:"#ffffff",boxShadow:"0 20px 40px -10px rgba(0,0,0,0.03)",border:"1px solid",borderColor:"grey.100",display:"flex",alignItems:"center",justifyContent:"space-between",gap:3},children:e.jsxs(y,{sx:{display:"flex",alignItems:"center",gap:2,width:"100%"},children:[e.jsx(y,{sx:{width:44,height:44,borderRadius:2.5,bgcolor:"primary.lighter",color:"primary.main",display:"flex",alignItems:"center",justifyContent:"center"},children:e.jsx(_e,{})}),e.jsxs(y,{sx:{flexGrow:1},children:[e.jsx(j,{variant:"caption",sx:{fontWeight:700,color:"text.secondary",letterSpacing:.8,textTransform:"uppercase"},children:"Active Logistics Provider"}),e.jsx(Ae,{sx:{mt:.5,minWidth:260,display:"block"},children:e.jsx(Ee,{value:p||"",onChange:r=>w(r.target.value),displayEmpty:!0,IconComponent:We,input:e.jsx(Pe,{sx:{borderRadius:3,fontWeight:700,fontSize:"1.1rem",bgcolor:n("#e2e8f0",.4),"& fieldset":{border:"none"},"&:hover":{bgcolor:n("#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(N,{value:"",disabled:!0,children:"Loading Providers..."}):A.map(r=>e.jsx(N,{value:r,sx:{fontWeight:600,py:1.5,borderRadius:1,mx:1},children:r},r))})})]})]})}),p&&H.length>0&&e.jsxs(Q,{elevation:0,sx:{borderRadius:4,border:"1px solid",borderColor:g?"#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(B,{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:()=>z(!g),children:[e.jsxs(B,{direction:"row",spacing:3,alignItems:"center",sx:{minWidth:260},children:[e.jsx(ge,{sx:{width:68,height:68,bgcolor:n("#c01227",.08),color:"#c01227",fontWeight:800,fontSize:"1.8rem",borderRadius:3},children:(p||"A")[0].toUpperCase()}),e.jsxs(y,{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(ie,{sx:{fontSize:16,mr:.5,color:"#c01227"}}),H.length," Registered Location",H.length!==1&&"s"]})]})]}),e.jsxs(B,{direction:"row",spacing:3,alignItems:"center",children:[e.jsxs(B,{direction:"row",spacing:1,sx:{display:{xs:"none",md:"flex"}},children:[e.jsx(q,{size:"small",label:"First Mile",sx:{bgcolor:n("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(q,{size:"small",label:"Mid Mile",sx:{bgcolor:n("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(q,{size:"small",label:"Last Mile",sx:{bgcolor:n("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}})]}),e.jsx(O,{onClick:r=>{r.stopPropagation(),z(!g)},sx:{borderRadius:2,py:1,px:3,fontWeight:700,color:g?"#c01227":"grey.700",bgcolor:g?n("#c01227",.1):"grey.100","&:hover":{bgcolor:n("#c01227",.15),color:"#c01227"}},endIcon:g?e.jsx(Re,{}):e.jsx(Ie,{}),children:g?"Hide Coverage":"View Coverage Map"})]})]}),e.jsx(pe,{in:g,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(ie,{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(y,{sx:{display:"flex",gap:1.5,flexWrap:"wrap"},children:H.map((r,t)=>e.jsx(q,{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:n("#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??t))})]})})]}),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:m?e.jsx(y,{sx:{display:"flex",justifyContent:"center",alignItems:"center",py:10},children:e.jsx(ue,{size:48,thickness:4,sx:{color:"primary.light"}})}):e.jsx(Le,{sx:{overflowX:"auto",width:"100%"},children:e.jsxs(Be,{sx:{minWidth:700},children:[e.jsx(Fe,{children:e.jsxs(D,{sx:{bgcolor:n("#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(Oe,{children:P.length===0?e.jsx(D,{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."})})}):oe.length===0?e.jsx(D,{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."})})}):oe.map(r=>e.jsxs(D,{sx:{transition:"background-color 0.15s","&:hover":{bgcolor:n("#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(t=>{const i=E.find(L=>(L.weight_slab||"").trim()===r&&re(L)===t),x=!!i,T=i?`₹${i.rate}`:"—";let K="";if(i&&i.updated_by){const L=C.find(ce=>ce.id===i.updated_by);K=`Last updated by: ${L?L.first_name:"ID "+i.updated_by}`}const le=()=>{M({id:i?i.id:void 0,company:p,weight_slab:r,zone:V?"":t,service_type:V?t:(i==null?void 0:i.service_type)||"",rate:i?String(i.rate):""}),b(!0),u(!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:n("#c01227",.04)}:{}},onClick:c?le:void 0,children:e.jsx(fe,{title:c?x?"Click to edit rate":"Click to add rate":K,placement:"top",arrow:!0,disableHoverListener:!c&&!K,children:e.jsxs(y,{sx:{display:"flex",alignItems:"center",justifyContent:"center",gap:1},children:[e.jsx(j,{sx:{display:"inline-block",fontWeight:x?700:400,color:x?"success.dark":c?n("#c01227",.4):"grey.300",fontSize:x?"0.95rem":"0.85rem",bgcolor:x?n("#10b981",.08):"transparent",px:x?1.5:0,py:x?.5:0,borderRadius:2},children:T}),c&&(x?e.jsx(se,{sx:{fontSize:13,color:"grey.400"}}):e.jsx(ae,{sx:{fontSize:13,color:n("#c01227",.4)}}))]})})},t)})]},r))})]})})})]}),e.jsx(Ue,{open:$,onClose:()=>u(!1),onSave:()=>v(),initialData:k,isCell:o,pricingList:s,providerColumns:P,providerUsesServiceType:V})]})}export{nr as default};
|