2 lines
16 KiB
JavaScript
2 lines
16 KiB
JavaScript
import{r as de,j as e,i as xe,a as d,B as f,T as j,M as N,x as a,h as he,S as H,A as ge,m as pe,C as ue,l as fe,c as me,I as be}from"./index-q3Tisdnr.js";import{d as ye,D as je,a as ve,e as Se,b as Ce,c as we}from"./Close-DL7iP88x.js";import{d as se}from"./EditOutlined-CZAx7RBQ.js";import{e as _e,c as We,b as ie,d as Re,a as Ie}from"./StorefrontOutlined-CL5n-1RH.js";import{P as ke,C as D,A as ze,G as W}from"./PageHeader-BdVI3_ZB.js";import{C as Q}from"./Link-CshtP915.js";import{F as Ae,S as Ee,O as Pe,T as B}from"./TextField-CVNMv7Sa.js";import{I as Te}from"./InputAdornment-NDKbvoTZ.js";import{B as F}from"./Button-BmCu8mk5.js";import{T as Le,a as Be,b as Fe,c as q,d as Oe}from"./TableRow-C5wbv4Kn.js";import{T as R}from"./TableCell-DV7KoMLs.js";import{S as $e}from"./Snackbar-Cz2M_lq2.js";var J={},Me=xe;Object.defineProperty(J,"__esModule",{value:!0});var ae=J.default=void 0,He=Me(de()),De=e;ae=J.default=(0,He.default)((0,De.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 qe(){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`,b=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(!b.ok){const _=await b.json().catch(()=>({}));throw new Error(_.error||_.message||"Failed to save pricing")}return b.status===204?{}:b.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:m,isCell:b=!1,pricingList:_=[],providerColumns:p=[],providerUsesServiceType:C=!1}){const[n,I]=d.useState({}),[g,k]=d.useState(!1),[O,u]=d.useState(null);d.useEffect(()=>{s&&(I(m||{company:"",weight_slab:"",zone:"",rate:""}),u(null))},[s,m]);const z=o=>y=>I(w=>({...w,[o]:y.target.value})),$=o=>(o.zone||"").trim()||(o.service_type||"").trim(),c=o=>{const y=(o.company||"").trim(),w=(o.weight_slab||"").trim(),x=(o.zone||o.service_type||"").trim();return _.find(v=>(v.company||"").trim()===y&&(v.weight_slab||"").trim()===w&&$(v)===x)||null},Z=async()=>{var y,w;const o=(n.zone||n.service_type||"").trim();if(!b){if(!((y=n.company)!=null&&y.trim())){u("Company is required.");return}if(!((w=n.weight_slab)!=null&&w.trim())){u("Weight slab is required.");return}if(!o){u("Zone / Service Type is required.");return}}if(!String(n.rate??"").trim()){u("Rate is required.");return}k(!0),u(null);try{let x={...n};if(b||(x={...x,zone:C?"":o,service_type:C?o:x.service_type||""}),!x.id){const v=c(x);v&&(x={...x,id:v.id})}await Ne(x),S(),l()}catch(x){console.error(x),u(x.message||"Failed to save. Please try again.")}finally{k(!1)}};return e.jsxs(e.Fragment,{children:[e.jsx($e,{open:!!O,autoHideDuration:6e3,onClose:()=>u(null),anchorOrigin:{vertical:"top",horizontal:"right"},sx:{zIndex:9999},children:e.jsx(ze,{onClose:()=>u(null),severity:"error",variant:"filled",sx:{width:"100%",boxShadow:3},children:O})}),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:[b?m!=null&&m.id?"Edit Rate":"Add Rate":m!=null&&m.id?"Edit Rate":"Add New Rate",e.jsx(be,{size:"small",onClick:l,disabled:g,children:e.jsx(Se,{fontSize:"small"})})]}),e.jsx(Ce,{dividers:!0,children:e.jsxs(W,{container:!0,spacing:2,children:[b?e.jsx(W,{item:!0,xs:12,children:e.jsxs(f,{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:z("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:z("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(B,{select:!0,fullWidth:!0,label:C?"Service Type *":"Zone / Service Type *",value:n.zone||n.service_type||"",onChange:o=>I(y=>({...y,zone:C?"":o.target.value,service_type:C?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(B,{fullWidth:!0,label:"Rate (₹) *",value:n.rate||"",onChange:z("rate"),size:"small",placeholder:"e.g. 25 or 25-35",autoFocus:!0})})]})}),e.jsxs(we,{sx:{p:2,bgcolor:"grey.50"},children:[e.jsx(F,{onClick:l,disabled:g,color:"inherit",children:"Cancel"}),e.jsx(F,{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,m]=d.useState([]),[b,_]=d.useState(!0),[p,C]=d.useState(""),[n,I]=d.useState(""),[g,k]=d.useState(!1),[O,u]=d.useState(!1),[z,$]=d.useState(null),[c,Z]=d.useState(!1),[o,y]=d.useState(!1),[w,x]=d.useState([]),v=()=>{let r=!1;return _(!0),Promise.all([qe().catch(()=>({data:[]})),Ve().catch(()=>({data:[]})),me().catch(()=>[])]).then(([t,i,h])=>{r||(l(Array.isArray(t)?t:t.data||[]),m(i.data||i||[]),x(Array.isArray(h)?h:h.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&&C(A[0])},[A,p]);const ne=s.filter(r=>r.company===p),Y=r=>(r||"").toLowerCase().trim().replace(/\s+/g," "),U=Y(p),M=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 h=parseFloat(i[0]),T=t.includes("kg")?h*1e3:h;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=n?te.filter(r=>r.toLowerCase().includes(n.toLowerCase())):te;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(_e,{})}),e.jsxs(f,{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=>C(r.target.value),displayEmpty:!0,IconComponent:We,input:e.jsx(Pe,{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: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))})})]})]}),e.jsxs(f,{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(Te,{position:"start",children:e.jsx(he,{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(ye,{}),onClick:()=>{$({company:p,weight_slab:"",zone:"",rate:""}),y(!1),u(!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(se,{}),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(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(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(!g),children:[e.jsxs(H,{direction:"row",spacing:3,alignItems:"center",sx:{minWidth:260},children:[e.jsx(ge,{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(f,{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"}}),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(D,{size:"small",label:"First Mile",sx:{bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(D,{size:"small",label:"Mid Mile",sx:{bgcolor:a("#c01227",.08),color:"#c01227",fontWeight:700,borderRadius:1.5}}),e.jsx(D,{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(!g)},sx:{borderRadius:2,py:1,px:3,fontWeight:700,color:g?"#c01227":"grey.700",bgcolor:g?a("#c01227",.1):"grey.100","&:hover":{bgcolor:a("#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(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(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(f,{sx:{display:"flex",gap:1.5,flexWrap:"wrap"},children:M.map((r,t)=>e.jsx(D,{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??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:b?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(Le,{children:e.jsxs(Be,{sx:{minWidth:700},children:[e.jsx(Fe,{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(Oe,{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."})})}):oe.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."})})}):oe.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(t=>{const i=E.find(L=>(L.weight_slab||"").trim()===r&&re(L)===t),h=!!i,T=i?`₹${i.rate}`:"—";let K="";if(i&&i.updated_by){const L=w.find(ce=>ce.id===i.updated_by);K=`Last updated by: ${L?L.first_name:"ID "+i.updated_by}`}const le=()=>{$({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):""}),y(!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:a("#c01227",.04)}:{}},onClick:c?le:void 0,children:e.jsx(fe,{title:c?h?"Click to edit rate":"Click to add rate":K,placement:"top",arrow:!0,disableHoverListener:!c&&!K,children:e.jsxs(f,{sx:{display:"flex",alignItems:"center",justifyContent:"center",gap:1},children:[e.jsx(j,{sx:{display:"inline-block",fontWeight:h?700:400,color:h?"success.dark":c?a("#c01227",.4):"grey.300",fontSize:h?"0.95rem":"0.85rem",bgcolor:h?a("#10b981",.08):"transparent",px:h?1.5:0,py:h?.5:0,borderRadius:2},children:T}),c&&(h?e.jsx(se,{sx:{fontSize:13,color:"grey.400"}}):e.jsx(ae,{sx:{fontSize:13,color:a("#c01227",.4)}}))]})})},t)})]},r))})]})})})]}),e.jsx(Ue,{open:O,onClose:()=>u(!1),onSave:()=>v(),initialData:z,isCell:o,pricingList:s,providerColumns:P,providerUsesServiceType:V})]})}export{nr as default};
|