Files
doormile_crm_build/dist/assets/Pricing-BuyNgb7k.js
2026-06-16 16:28:00 +05:30

2 lines
8.8 KiB
JavaScript

import{r as H,j as e,i as M,a as b,S as v,N,B as x,C as Z,T as d,K as z,I as T}from"./index-DdJWTZFy.js";import{d as D,D as q,a as G,b as U,c as J,e as K,f as Q}from"./DeleteOutline-DetaO5yn.js";import{d as W}from"./LocalShippingOutlined-DRozEKne.js";import{P as V,G as p,C as S}from"./PageHeader-CdP5B6Qy.js";import{B as y}from"./Button-Bcwcjqrt.js";import{C as P}from"./Link-Dp34e72e.js";import{T as j}from"./TextField-Btd7UbKs.js";import{T as Y,a as X,b as ee,c as k,d as n,e as re}from"./TableRow-CEoQ4SIZ.js";var R={},te=M;Object.defineProperty(R,"__esModule",{value:!0});var $=R.default=void 0,ie=te(H()),ae=e;$=R.default=(0,ie.default)((0,ae.jsx)("path",{d:"M2.5 19h19v2h-19zm19.57-9.36c-.21-.8-1.04-1.28-1.84-1.06L14.92 10l-6.9-6.43-1.93.51 4.14 7.17-4.97 1.33-1.97-1.54-1.45.39 2.59 4.49L21 11.49c.81-.23 1.28-1.05 1.07-1.85"}),"FlightTakeoffOutlined");const C="https://api.doormile.com/api/v1",_=()=>{const l=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...l?{Authorization:`Bearer ${l}`}:{}}};async function le(){const l=await fetch(`${C}/admin/carrier-pricing?limit=500`,{headers:_()});if(!l.ok)throw new Error("Failed to fetch pricing");return l.json()}async function se(l){const{id:a,created_at:c,updated_at:g,...f}=l,o=a?`${C}/admin/carrier-pricing/${a}`:`${C}/admin/carrier-pricing`,r=await fetch(o,{method:a?"PUT":"POST",headers:_(),body:JSON.stringify(f)});if(!r.ok)throw new Error("Failed to save pricing");return r.json()}async function ne(l){const a=await fetch(`${C}/admin/carrier-pricing/${l}`,{method:"DELETE",headers:_()});if(!a.ok)throw new Error("Failed to delete pricing");return a.json()}const E={id:null,company:"",weight_slab:"",service_type:"",zone:"",rate:"",delivery_time:""};function w({label:l,value:a,icon:c,color:g="primary"}){return e.jsx(x,{sx:{flex:1,p:2.5,borderRadius:2,border:1,borderColor:"divider",bgcolor:"background.paper",boxShadow:"0 4px 20px rgba(0,0,0,0.03)"},children:e.jsxs(v,{direction:"row",spacing:2,alignItems:"center",children:[e.jsx(x,{sx:{width:48,height:48,borderRadius:2,bgcolor:`${g}.lighter`,color:`${g}.main`,display:"inline-flex",alignItems:"center",justifyContent:"center"},children:e.jsx(c,{sx:{fontSize:24}})}),e.jsxs(x,{children:[e.jsx(d,{variant:"h4",sx:{fontWeight:800,color:"grey.800",lineHeight:1.1},children:a}),e.jsx(d,{variant:"body2",sx:{fontWeight:600,mt:.5},color:"text.secondary",children:l})]})]})})}function oe({company:l,rows:a,onEditRow:c,onAddRow:g,onDeleteRow:f}){const o=[...new Set(a.map(t=>t.service_type).filter(Boolean))],r=a.some(t=>t.zone&&t.zone.trim()!==""),u=a.some(t=>t.delivery_time&&t.delivery_time.trim()!=="");return e.jsxs(P,{sx:{mb:4,borderRadius:2.5,boxShadow:"0 8px 24px rgba(0,0,0,0.04)",border:1,borderColor:"grey.200",overflow:"hidden"},children:[e.jsxs(x,{sx:{px:2.5,py:2,borderBottom:1,borderColor:"divider",display:"flex",alignItems:"center",justifyContent:"space-between",background:t=>`linear-gradient(90deg, ${t.palette.primary.lighter}44 0%, ${t.palette.background.paper} 70%)`},children:[e.jsxs(v,{direction:"row",spacing:2,alignItems:"center",children:[e.jsx(x,{sx:{width:48,height:48,borderRadius:"50%",bgcolor:"primary.main",color:"primary.contrastText",display:"flex",alignItems:"center",justifyContent:"center",fontWeight:800,fontSize:"1.4rem"},children:l[0].toUpperCase()}),e.jsxs(x,{children:[e.jsx(d,{variant:"h6",sx:{fontWeight:800,color:"grey.800",lineHeight:1.2},children:l}),e.jsxs(d,{variant:"caption",color:"text.secondary",children:[a.length," rate",a.length!==1?"s":""]})]})]}),e.jsx(v,{direction:"row",spacing:1,alignItems:"center",flexWrap:"wrap",children:o.map(t=>e.jsx(S,{label:t,size:"small",sx:{fontWeight:600,bgcolor:"primary.lighter",color:"primary.dark"}},t))})]}),e.jsx(Y,{children:e.jsxs(X,{sx:{minWidth:500},children:[e.jsx(ee,{children:e.jsxs(k,{sx:{"& th":{bgcolor:"grey.50",fontWeight:700,color:"grey.700",textTransform:"uppercase",fontSize:"0.72rem",letterSpacing:.4}},children:[e.jsx(n,{children:"Weight Slab"}),e.jsx(n,{children:"Service Type"}),r&&e.jsx(n,{children:"Zone"}),e.jsx(n,{children:"Rate (₹)"}),u&&e.jsx(n,{children:"Delivery"}),e.jsx(n,{align:"right",children:"Actions"})]})}),e.jsx(re,{children:a.map(t=>e.jsxs(k,{hover:!0,sx:{"& > *":{borderBottom:"1px solid",borderColor:"divider"}},children:[e.jsx(n,{children:e.jsx(d,{variant:"body2",sx:{fontWeight:700,color:"grey.800"},children:t.weight_slab||"—"})}),e.jsx(n,{children:e.jsx(d,{variant:"body2",color:"text.secondary",children:t.service_type||"—"})}),r&&e.jsx(n,{children:e.jsx(S,{size:"small",label:t.zone||"—",sx:{fontWeight:600,bgcolor:"grey.100",color:"grey.700",borderRadius:1}})}),e.jsx(n,{children:e.jsx(S,{size:"small",label:t.rate?`${t.rate}`:"—",sx:{fontWeight:600,bgcolor:"success.lighter",color:"success.dark",borderRadius:1}})}),u&&e.jsx(n,{children:e.jsx(d,{variant:"body2",color:"text.secondary",children:t.delivery_time||"—"})}),e.jsxs(n,{align:"right",children:[e.jsx(z,{title:"Edit Row",children:e.jsx(T,{size:"small",onClick:()=>c(t),children:e.jsx(K,{fontSize:"small"})})}),e.jsx(z,{title:"Delete Row",children:e.jsx(T,{size:"small",onClick:()=>f(t),children:e.jsx(Q,{fontSize:"small"})})})]})]},t.id))})]})}),e.jsx(x,{sx:{p:1,borderTop:1,borderColor:"divider",bgcolor:"grey.50"},children:e.jsx(y,{fullWidth:!0,size:"small",startIcon:e.jsx(D,{}),sx:{color:"text.secondary",fontWeight:600,"&:hover":{bgcolor:"grey.200",color:"primary.main"}},onClick:()=>g(l),children:"Add Rate Slab"})})]})}function je(){const[l,a]=b.useState([]),[c,g]=b.useState(!0),[f,o]=b.useState(!1),[r,u]=b.useState(null);b.useEffect(()=>{let i=!1;return le().then(s=>{i||a(s.data||[])}).catch(()=>{i||a([])}).finally(()=>{i||g(!1)}),()=>{i=!0}},[]);const t=[...new Set(l.map(i=>i.company).filter(Boolean))].sort(),I=t.reduce((i,s)=>(i[s]=l.filter(h=>h.company===s),i),{}),B=new Set(l.map(i=>i.weight_slab).filter(Boolean)).size,A=i=>{u({...i}),o(!0)},F=i=>{u({...E,company:i}),o(!0)},L=async i=>{if(window.confirm("Delete this pricing entry?"))try{await ne(i.id),a(s=>s.filter(h=>h.id!==i.id))}catch{alert("Failed to delete. Please try again.")}},O=async()=>{try{const i=await se(r);r.id?a(s=>s.map(h=>h.id===r.id?i:h)):a(s=>[...s,i]),o(!1)}catch{alert("Failed to save. Please try again.")}},m=i=>s=>u(h=>({...h,[i]:s.target.value}));return e.jsxs(e.Fragment,{children:[e.jsx(V,{title:"Logistics Pricing Matrix",breadcrumbs:[{label:"Pricing Rate Cards"}],action:e.jsx(y,{variant:"contained",startIcon:e.jsx(D,{}),onClick:()=>{u({...E}),o(!0)},sx:{borderRadius:2,px:3,boxShadow:"0 4px 12px rgba(0,0,0,0.1)"},children:"Add Rate Entry"})}),e.jsxs(v,{direction:{xs:"column",md:"row"},spacing:2,sx:{mt:3,mb:4},children:[e.jsx(w,{label:"Total Carriers",value:c?"…":t.length,icon:W,color:"primary"}),e.jsx(w,{label:"Rate Entries",value:c?"…":l.length,icon:N,color:"success"}),e.jsx(w,{label:"Weight Slabs",value:c?"…":B,icon:$,color:"warning"})]}),c?e.jsx(x,{sx:{display:"flex",justifyContent:"center",py:8},children:e.jsx(Z,{size:40})}):t.length===0?e.jsxs(P,{sx:{p:6,textAlign:"center",borderRadius:2.5,boxShadow:"0 8px 24px rgba(0,0,0,0.04)",border:1,borderColor:"grey.200"},children:[e.jsx(W,{sx:{fontSize:48,color:"grey.300",mb:2}}),e.jsx(d,{variant:"h6",sx:{color:"grey.700",fontWeight:600},children:"No pricing data found"}),e.jsx(d,{variant:"body2",color:"text.secondary",sx:{mt:1},children:"Run the SQL import or add entries manually."})]}):t.map(i=>e.jsx(oe,{company:i,rows:I[i],onEditRow:A,onAddRow:F,onDeleteRow:L},i)),e.jsxs(q,{open:f,onClose:()=>o(!1),maxWidth:"sm",fullWidth:!0,children:[e.jsx(G,{children:r!=null&&r.id?"Edit Rate Entry":"Add Rate Entry"}),e.jsx(U,{dividers:!0,children:e.jsxs(p,{container:!0,spacing:2,children:[e.jsx(p,{item:!0,xs:12,children:e.jsx(j,{fullWidth:!0,size:"small",label:"Carrier / Company",value:(r==null?void 0:r.company)||"",onChange:m("company")})}),e.jsx(p,{item:!0,xs:12,sm:6,children:e.jsx(j,{fullWidth:!0,size:"small",label:"Weight Slab",value:(r==null?void 0:r.weight_slab)||"",onChange:m("weight_slab"),placeholder:"e.g. 1kg - 2kg"})}),e.jsx(p,{item:!0,xs:12,sm:6,children:e.jsx(j,{fullWidth:!0,size:"small",label:"Service Type",value:(r==null?void 0:r.service_type)||"",onChange:m("service_type"),placeholder:"e.g. Standard Surface"})}),e.jsx(p,{item:!0,xs:12,sm:6,children:e.jsx(j,{fullWidth:!0,size:"small",label:"Zone",value:(r==null?void 0:r.zone)||"",onChange:m("zone"),placeholder:"e.g. Local / Zonal / National"})}),e.jsx(p,{item:!0,xs:12,sm:6,children:e.jsx(j,{fullWidth:!0,size:"small",label:"Rate (₹)",value:(r==null?void 0:r.rate)||"",onChange:m("rate"),placeholder:"e.g. 100 - 200"})}),e.jsx(p,{item:!0,xs:12,children:e.jsx(j,{fullWidth:!0,size:"small",label:"Delivery Time",value:(r==null?void 0:r.delivery_time)||"",onChange:m("delivery_time"),placeholder:"e.g. 3 - 5 days"})})]})}),e.jsxs(J,{sx:{p:2},children:[e.jsx(y,{onClick:()=>o(!1),children:"Cancel"}),e.jsx(y,{variant:"contained",onClick:O,children:"Save"})]})]})]})}export{je as default};