47 lines
16 KiB
JavaScript
47 lines
16 KiB
JavaScript
import{c as oe,r as n,j as e,T as S,S as J,B as M,C as ne,m as le,n as X,H as te,d as ce,L as de,l as pe}from"./index-TNj5auBr.js";import{B as D,P as xe}from"./PageHeader-B9Ytwo9b.js";import{E as U}from"./EmptyState-DGz1kDMS.js";import{a as ge,p as he,T as fe}from"./Table-SizHLk8D.js";import{S as ue}from"./square-pen-BItzjjNj.js";import{S as Z}from"./Selector-D-0RMSQ0.js";import{T as $}from"./TextInput-DL-5979h.js";import{P as me,D as ye,L as be,F as ve,a as je}from"./plus-CZCr3OEY.js";import{C as F}from"./Card-Cvh0wiAW.js";import{M as ee}from"./map-pin-BQjktuvD.js";import{I as we}from"./IconButton-Dlo2bZzs.js";import{C as Se}from"./chevron-up-CSuWANIv.js";import{B as Ce}from"./Banner-DVJsik2E.js";/**
|
||
* @license lucide-react v1.22.0 - ISC
|
||
*
|
||
* This source code is licensed under the ISC license.
|
||
* See the LICENSE file in the root directory of this source tree.
|
||
*/const ke=[["rect",{width:"7",height:"7",x:"3",y:"3",rx:"1",key:"1g98yp"}],["rect",{width:"7",height:"7",x:"14",y:"3",rx:"1",key:"6d4xhi"}],["rect",{width:"7",height:"7",x:"14",y:"14",rx:"1",key:"nxv5o0"}],["rect",{width:"7",height:"7",x:"3",y:"14",rx:"1",key:"1bb6yr"}]],Le=oe("layout-grid",ke),B="https://api.doormile.com/api/v1",K=()=>{const i=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...i?{Authorization:`Bearer ${i}`}:{}}};async function _e(){const i=await fetch(`${B}/admin/carrier-pricing?limit=1000`,{headers:K()});if(!i.ok)throw new Error("Failed to fetch pricing");const c=await i.json();return Array.isArray(c)?{data:c}:c}async function Ie(i){const c=!!i.id,b=c?`${B}/admin/carrier-pricing/${i.id}`:`${B}/admin/carrier-pricing`,g=await fetch(b,{method:c?"PUT":"POST",headers:K(),body:JSON.stringify({company:i.company,weight_slab:i.weight_slab,zone:i.zone||"",service_type:i.service_type||"",rate:String(i.rate)})});if(!g.ok){const C=await g.json().catch(()=>({}));throw new Error(C.error||C.message||"Failed to save pricing")}return g.status===204?{}:g.json().catch(()=>({}))}const ze=["Local / Same","Within Tamilnadu","Interstate India"],Re=["< 500g","500g - 1kg","1kg - 2kg","2kg - 5kg","> 5kg"];function We({open:i,onClose:c,onSave:b,initialData:v,isCell:g=!1,pricingList:C=[],providerColumns:d=[],providerUsesServiceType:j=!1}){const[l,k]=n.useState({}),[h,P]=n.useState(!1),[L,f]=n.useState(null);n.useEffect(()=>{i&&(k(v||{company:"",weight_slab:"",zone:"",rate:""}),f(null))},[i,v]);const N=a=>(a.zone||"").trim()||(a.service_type||"").trim(),z=a=>{const p=(a.company||"").trim(),R=(a.weight_slab||"").trim(),x=(a.zone||a.service_type||"").trim();return C.find(u=>(u.company||"").trim()===p&&(u.weight_slab||"").trim()===R&&N(u)===x)||null},H=async()=>{var p,R;const a=(l.zone||l.service_type||"").trim();if(!g){if(!((p=l.company)!=null&&p.trim())){f("Company is required.");return}if(!((R=l.weight_slab)!=null&&R.trim())){f("Weight slab is required.");return}if(!a){f("Zone / Service Type is required.");return}}if(!String(l.rate??"").trim()){f("Rate is required.");return}P(!0),f(null);try{let x={...l};if(g||(x={...x,zone:j?"":a,service_type:j?a:x.service_type||""}),!x.id){const u=z(x);u&&(x={...x,id:u.id})}await Ie(x),b(),c()}catch(x){console.error(x),f(x.message||"Failed to save. Please try again.")}finally{P(!1)}};if(!i)return null;const T=d.length>0?d:ze;return e.jsx(ye,{isOpen:i,onOpenChange:a=>{a||c()},width:440,purpose:"form",children:e.jsx(de,{header:e.jsx(je,{title:v!=null&&v.id?"Edit Rate":g?"Add Rate":"Add New Rate",onOpenChange:a=>{a||c()}}),content:e.jsx(pe,{children:e.jsxs(ve,{children:[L&&e.jsx(Ce,{status:"error",title:L}),g?e.jsxs(F,{style:{padding:"12px 16px",backgroundColor:"rgba(192, 18, 39, 0.04)",border:"1px solid rgba(192, 18, 39, 0.15)"},children:[e.jsx(S,{type:"label",color:"secondary",children:"Updating rate for"}),e.jsx("div",{style:{marginTop:"4px"},children:e.jsx(te,{level:5,children:l.company})}),e.jsx("div",{style:{marginTop:"2px"},children:e.jsxs(S,{type:"supporting",color:"secondary",children:[l.weight_slab," · ",l.zone||l.service_type]})})]}):e.jsxs(e.Fragment,{children:[e.jsx($,{label:"Company",isRequired:!0,value:l.company||"",onChange:a=>k(p=>({...p,company:a}))}),e.jsx(Z,{label:"Weight Slab",isRequired:!0,placeholder:"Select Weight Slab",options:Re,value:l.weight_slab||"",onChange:a=>k(p=>({...p,weight_slab:a||""}))}),e.jsx(Z,{label:j?"Service Type":"Zone / Service Type",isRequired:!0,placeholder:"Select Zone / Service",options:T,value:l.zone||l.service_type||"",onChange:a=>k(p=>({...p,zone:j?"":a||"",service_type:j&&a||""}))})]}),e.jsx($,{label:"Rate (₹)",isRequired:!0,hasClear:!0,hasAutoFocus:!0,placeholder:"e.g. 25 or 25-35",value:l.rate||"",onChange:a=>k(p=>({...p,rate:a}))})]})}),footer:e.jsx(be,{hasDivider:!0,children:e.jsxs("div",{style:{display:"flex",justifyContent:"flex-end",gap:"12px"},children:[e.jsx(M,{label:"Cancel",variant:"ghost",onClick:c,isDisabled:h}),e.jsx(M,{label:"Save Rate",variant:"primary",onClick:H,isDisabled:h,isLoading:h})]})})})})}async function Ee(){const i=await fetch(`${B}/admin/competitor-branches?limit=1000`,{headers:K()});if(!i.ok)throw new Error("Failed to fetch surveys");const c=await i.json(),b=Array.isArray(c)?c:c.data||[];return{data:b,total:b.length}}function Ze(){const[i,c]=n.useState([]),[b,v]=n.useState([]),[g,C]=n.useState(!0),[d,j]=n.useState(""),[l,k]=n.useState(""),[h,P]=n.useState(!1),[L,f]=n.useState(""),[N,z]=n.useState(!1),[H,T]=n.useState(null),[a,p]=n.useState(!1),[R,x]=n.useState([]),u=()=>{let t=!1;return C(!0),Promise.all([_e().catch(()=>({data:[]})),Ee().catch(()=>({data:[]})),ce().catch(()=>[])]).then(([r,s,o])=>{t||(c(Array.isArray(r)?r:r.data||[]),v(s.data||s||[]),x(Array.isArray(o)?o:o.data||[]))}).catch(r=>console.error("Failed to fetch data",r)).finally(()=>{t||C(!1)}),()=>{t=!0}};n.useEffect(()=>u(),[]);const _=[...new Set(i.map(t=>t.company).filter(Boolean))];n.useEffect(()=>{!d&&_.length>0&&j(_[0])},[_,d]);const re=i.filter(t=>t.company===d),G=t=>(t||"").toLowerCase().trim().replace(/\s+/g," "),O=G(d),w=b.filter(t=>{const r=G(t.company);return r===O||r.startsWith(O)||O.startsWith(r)}),q=n.useMemo(()=>{if(!L.trim())return w;const t=L.toLowerCase();return w.filter(r=>(r.area||"").toLowerCase().includes(t)||(r.address||"").toLowerCase().includes(t))},[w,L]),I=[...re].sort((t,r)=>(r.id||0)-(t.id||0)),Q=t=>{const r=(t||"").toLowerCase().replace(/\s+/g,""),s=r.match(/[\d.]+/g);if(!s)return 9999;const o=parseFloat(s[0]),y=r.includes("kg")?o*1e3:o;return r.startsWith("<")||r.startsWith("upto")?y-.5:r.startsWith(">")||r.includes("above")||r.endsWith("+")?y+.5:y},ae=t=>{if(!t)return"";let r=t.trim();const s=r.toLowerCase().replace(/\s+/g,"");return s==="tamilnadu"?"Tamil Nadu":s==="karanataka"||s==="karnataka"?"Karnataka":s==="local"||s==="localsame"||s==="local/same"?"Local":s==="central/northindia"||s==="central/north"?"Central/North India":r},V=t=>ae((t.zone||"").trim()||(t.service_type||"").trim()),A=I.length>0&&I.every(t=>!(t.zone||"").trim()),m=[...new Set(I.map(V).filter(Boolean))].sort(),Y=[...new Set(I.map(t=>(t.weight_slab||"").trim()).filter(Boolean))].sort((t,r)=>Q(t)-Q(r)),W=l?Y.filter(t=>t.toLowerCase().includes(l.toLowerCase())):Y,se=n.useMemo(()=>W.map(t=>({slab:t,rates:Object.fromEntries(m.map(r=>[r,I.find(s=>(s.weight_slab||"").trim()===t&&V(s)===r)||null]))})),[W,m,I]),ie=n.useMemo(()=>{const t=(r,s,o)=>{T({id:o?o.id:void 0,company:d,weight_slab:r,zone:A?"":s,service_type:A?s:(o==null?void 0:o.service_type)||"",rate:o?String(o.rate):""}),p(!0),z(!0)};return[{key:"slab",header:e.jsx("div",{style:{paddingLeft:"12px"},children:"Weight Slab"}),width:ge(180),renderCell:r=>e.jsx("div",{style:{paddingLeft:"12px"},children:e.jsx(S,{type:"body",weight:"semibold",children:r.slab})})},...m.map(r=>({key:r,header:r,width:he(1),align:"center",renderCell:s=>{const o=s.rates[r],y=!!o;return e.jsxs("div",{className:"matrix-cell",style:{position:"relative",display:"flex",alignItems:"center",justifyContent:"center",padding:"4px",borderRadius:"6px",width:"100%",boxSizing:"border-box"},children:[y?e.jsx(D,{variant:"red",label:`₹${o.rate}`}):e.jsx(S,{type:"supporting",color:"disabled",children:"—"}),e.jsx("button",{type:"button",className:"matrix-cell-edit",onClick:()=>t(s.slab,r,o),"aria-label":`Edit rate for ${s.slab} in ${r}`,title:y?"Edit rate":"Add rate",children:e.jsx(ue,{size:12})})]})}}))]},[m,d,A]);return e.jsxs("div",{style:{fontFamily:"system-ui, sans-serif"},children:[e.jsx(xe,{title:"Logistics Pricing Board",action:e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"12px",flexWrap:"nowrap"},children:[e.jsx("div",{style:{flex:"0 1 280px",minWidth:"200px"},children:e.jsx(Z,{label:"Active Logistics Provider",isLabelHidden:!0,placeholder:_.length===0?"Loading providers…":"Select provider",hasSearch:!0,hasClear:!1,isDisabled:_.length===0,options:_,value:d||"",onChange:t=>j(t||"")})}),e.jsx("div",{style:{flex:"0 1 260px",minWidth:"180px"},children:e.jsx($,{label:"Search weight slabs",isLabelHidden:!0,placeholder:"Search weight slabs…",startIcon:e.jsx(J,{size:16}),hasClear:!0,value:l,onChange:t=>k(t)})}),e.jsx(M,{variant:"primary",icon:e.jsx(me,{size:14}),onClick:()=>{T({company:d,weight_slab:"",zone:"",rate:""}),p(!1),z(!0)},label:"Add Rate",style:{flexShrink:0}})]})}),e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:"32px",marginTop:"24px"},children:[d&&w.length>0&&e.jsxs(F,{style:{borderRadius:"16px",border:h?"1px solid rgba(192, 18, 39, 0.25)":"1px solid rgba(5, 54, 89, 0.08)",backgroundColor:"#ffffff",padding:"0",overflow:"hidden",boxShadow:"0 10px 30px -5px rgba(0,0,0,0.02), 0 4px 12px -3px rgba(0,0,0,0.02)"},children:[e.jsxs("div",{style:{display:"flex",flexDirection:"row",alignItems:"center",padding:"12px 16px",cursor:"pointer",justifyContent:"space-between",flexWrap:"wrap",gap:"12px"},onClick:()=>P(!h),children:[e.jsxs("div",{style:{display:"flex",gap:"10px",alignItems:"center"},children:[e.jsx("div",{style:{width:"32px",height:"32px",borderRadius:"8px",backgroundColor:"rgba(192, 18, 39, 0.08)",color:"#c01227",fontWeight:800,fontSize:"0.875rem",display:"flex",alignItems:"center",justifyContent:"center",flexShrink:0},children:(d||"A")[0].toUpperCase()}),e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px",flexWrap:"wrap"},children:[e.jsx(S,{type:"body",weight:"semibold",children:d}),e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"4px"},children:[e.jsx(ee,{size:12,style:{color:"#94a3b8"}}),e.jsxs(S,{type:"supporting",color:"secondary",children:[w.length," location",w.length!==1&&"s"]})]})]})]}),e.jsx(we,{label:h?"Hide coverage map":"View coverage map",tooltip:h?"Hide coverage map":"View coverage map",variant:"secondary",size:"sm",icon:h?e.jsx(Se,{size:14}):e.jsx(ne,{size:14})})]}),h&&e.jsxs("div",{style:{padding:"16px 24px 24px 24px",backgroundColor:"#f8fafc",borderTop:"1px dashed rgba(5, 54, 89, 0.1)"},children:[e.jsxs("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",gap:"16px",marginBottom:"14px",flexWrap:"wrap"},children:[e.jsx("div",{style:{maxWidth:"400px",flexGrow:1,minWidth:"240px"},children:e.jsx($,{label:"Search locations",isLabelHidden:!0,placeholder:"Search areas or addresses…",startIcon:e.jsx(J,{size:16}),hasClear:!0,value:L,onChange:t=>f(t)})}),e.jsxs(S,{type:"supporting",color:"secondary",children:[q.length," of ",w.length," serviced location",w.length!==1?"s":""]})]}),q.length===0?e.jsx(F,{style:{borderRadius:"12px",border:"1px solid rgba(5, 54, 89, 0.08)",backgroundColor:"#ffffff"},children:e.jsx(U,{title:"No matching locations",caption:"Try a different search term."})}):e.jsx("div",{style:{display:"grid",gridTemplateColumns:"repeat(auto-fill, minmax(280px, 1fr))",gap:"12px",maxHeight:"420px",overflowY:"auto",paddingRight:"4px"},children:q.map((t,r)=>{const s=t.area||t.address||"Unknown area",o=t.area&&t.address?t.address:null,y=t.plus_code?`${t.plus_code} ${t.address||""}`.trim():t.address||t.area,E=y?`https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(y)}`:void 0;return e.jsx("div",{style:{borderRadius:"10px",border:"1px solid rgba(5, 54, 89, 0.08)",backgroundColor:"#ffffff",overflow:"hidden"},className:E?"location-card":"",children:e.jsx(le,{label:s,description:o,labelLines:1,descriptionLines:2,align:"start",startContent:e.jsx(X,{icon:ee,size:"sm",color:"secondary"}),endContent:E?e.jsx(X,{icon:"externalLink",size:"xsm",color:"secondary"}):void 0,href:E,target:E?"_blank":void 0,rel:E?"noopener noreferrer":void 0})},t.id??r)})})]})]}),e.jsxs(F,{style:{padding:"0",borderRadius:"16px",border:"1px solid rgba(5, 54, 89, 0.08)",backgroundColor:"#ffffff",overflow:"hidden",boxShadow:"0 10px 30px -5px rgba(0,0,0,0.02), 0 4px 12px -3px rgba(0,0,0,0.02)"},children:[e.jsxs("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",gap:"12px",padding:"20px 24px",borderBottom:"1px solid rgba(5, 54, 89, 0.06)",background:"linear-gradient(90deg, rgba(192, 18, 39, 0.03) 0%, #ffffff 70%)",flexWrap:"wrap"},children:[e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"12px"},children:[e.jsx("div",{style:{width:"40px",height:"40px",borderRadius:"8px",backgroundColor:"rgba(192, 18, 39, 0.08)",color:"#C01227",display:"flex",alignItems:"center",justifyContent:"center"},children:e.jsx(Le,{size:18})}),e.jsxs("div",{children:[e.jsx(te,{level:4,children:"Rate Matrix"}),e.jsxs(S,{type:"supporting",color:"secondary",children:["Weight slab × zone pricing grid",d?` for ${d}`:""]})]})]}),!g&&m.length>0&&e.jsxs("div",{style:{display:"flex",gap:"8px",alignItems:"center",flexWrap:"wrap"},children:[e.jsx(D,{variant:"info",label:`${W.length} slab${W.length!==1?"s":""}`}),e.jsx(D,{variant:"neutral",label:`${m.length} zone${m.length!==1?"s":""}`})]})]}),g?e.jsx("div",{style:{display:"flex",justifyContent:"center",padding:"48px 0"},children:e.jsx("div",{className:"spinner",style:{width:"32px",height:"32px",border:"3px solid #f1f5f9",borderTop:"3px solid #C01227",borderRadius:"50%",animation:"spin 1s linear infinite"}})}):m.length===0?e.jsx(U,{title:"No pricing data for this provider yet",caption:"Add a rate to start building the pricing matrix."}):W.length===0?e.jsx(U,{title:"No weight slabs match your search",caption:"Try a different search term."}):e.jsx(fe,{data:se,columns:ie,idKey:"slab",density:"spacious",dividers:"grid",hasHover:!0,isStriped:!0})]})]}),e.jsx(We,{open:N,onClose:()=>z(!1),onSave:()=>u(),initialData:H,isCell:a,pricingList:i,providerColumns:m,providerUsesServiceType:A}),e.jsx("style",{children:`
|
||
/* Rate matrix cells: no global "edit mode" toggle anymore — hovering
|
||
a specific slab/zone cell reveals a small edit button just for
|
||
that cell, so editing is always inline and per-cell. No background
|
||
tint on hover, just the icon. */
|
||
.matrix-cell-edit {
|
||
position: absolute;
|
||
right: 4px;
|
||
top: 50%;
|
||
transform: translateY(-50%);
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
width: 24px;
|
||
height: 24px;
|
||
border-radius: 6px;
|
||
border: 1px solid rgba(5, 54, 89, 0.1);
|
||
background-color: #ffffff;
|
||
box-shadow: 0 2px 6px rgba(15, 23, 42, 0.12);
|
||
color: #C01227;
|
||
cursor: pointer;
|
||
opacity: 0;
|
||
pointer-events: none;
|
||
transition: opacity 0.15s ease, background-color 0.15s ease;
|
||
}
|
||
.matrix-cell:hover .matrix-cell-edit {
|
||
opacity: 1;
|
||
pointer-events: auto;
|
||
}
|
||
.matrix-cell-edit:hover {
|
||
background-color: rgba(192, 18, 39, 0.08) !important;
|
||
}
|
||
.location-card {
|
||
transition: border-color 0.15s ease, box-shadow 0.15s ease;
|
||
}
|
||
.location-card:hover {
|
||
border-color: rgba(192, 18, 39, 0.3) !important;
|
||
box-shadow: 0 4px 12px rgba(192, 18, 39, 0.08);
|
||
}
|
||
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
|
||
`})]})}export{Ze as default};
|