2 lines
15 KiB
JavaScript
2 lines
15 KiB
JavaScript
import{b as u,j as e,S as G,l as J,B as w,C as q,z as V,f as Q,T as x,I as U,M as E}from"./index-B539dxxT.js";import{d as L,D as X,a as Y,b as Z,c as ee}from"./Close-DfGDV2pn.js";import{d as ae}from"./DeleteOutline-BrmtQL6A.js";import{P as re,C as T,A as se,G as n}from"./PageHeader-vd12s20e.js";import{A as W,C as ie}from"./ClientFormDialog-CWqJvzya.js";import{T as l}from"./TextField-Cl3T8i0P.js";import{I as te}from"./InputAdornment-BjFJvkmB.js";import{B as k}from"./Button-D7QX3_Jz.js";import{C as ne}from"./Link-DlAvCPCg.js";import{T as oe,a as _}from"./Tabs-B1a1ukmT.js";import{T as le,a as ce,b as de,c as N,d as ue}from"./TableRow-DdCvjxdw.js";import{T as h}from"./TableCell-Cw928AWh.js";import{S as he}from"./Snackbar-CovgXSVI.js";import{F as me,C as pe}from"./FormControlLabel-CIhtgm4u.js";import"./SwitchBase-BSl_qXN1.js";const I="https://api.doormile.com/api/v1",xe=["Chennai","Coimbatore","Madurai","Tiruchirappalli","Salem","Tuticorin","Tirupur","Erode","Vellore","Tirunelveli","Thanjavur","Dindigul","Hosur","Nagercoil","Karur","Namakkal","Kanchipuram","Cuddalore","Thoothukudi","Bengaluru","Mysuru","Mangaluru","Hubli","Belagavi","Kalaburagi","Davangere","Ballari","Kochi","Thiruvananthapuram","Kozhikode","Kannur","Thrissur","Kollam","Hyderabad","Warangal","Visakhapatnam","Vijayawada","Tirupati","Guntur","Rajahmundry","Nellore","Mumbai","Pune","Nagpur","Nashik","Aurangabad","Kolhapur","Solapur","New Delhi","Gurugram","Noida","Faridabad","Chandigarh","Amritsar","Ludhiana","Jalandhar","Ahmedabad","Surat","Vadodara","Rajkot","Bhavnagar","Jamnagar","Kolkata","Howrah","Durgapur","Asansol","Siliguri","Lucknow","Kanpur","Agra","Varanasi","Allahabad","Meerut","Bhopal","Indore","Gwalior","Jabalpur","Jaipur","Jodhpur","Udaipur","Kota","Bikaner","Patna","Gaya","Bhagalpur","Bhubaneswar","Cuttack","Rourkela","Guwahati","Raipur","Ranchi","Dehradun"],z=()=>{const d=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...d?{Authorization:`Bearer ${d}`}:{}}};async function ge(){const d=await fetch(`${I}/admin/bookings`,{headers:z()});if(!d.ok)throw new Error("Failed to fetch bookings");const c=await d.json();return Array.isArray(c)?c:c.data||[]}async function je(){const d=await fetch(`${I}/admin/competitor-branches?limit=1000`,{headers:z()});if(!d.ok)throw new Error("Failed to fetch surveys");const c=await d.json();return Array.isArray(c)?c:c.data||[]}async function fe(d){const c=await fetch(`${I}/admin/crmbooking`,{method:"POST",headers:z(),body:JSON.stringify(d)});if(!c.ok){const j=await c.json().catch(()=>({}));throw new Error(j.error||"Failed to create booking")}return c.json()}function ye({open:d,onClose:c,onSave:j,clients:D,surveys:F}){var R;const[P,S]=u.useState(!1),[i,o]=u.useState({customer_name:"",customer_phone:"",pickupaddress:"",pickuppincode:"",deliveryaddress:"",deliverypincode:"",deliverycity:"",providercompany:"",providerlocation:"",notes:"",service_option:"Normal",finalprice:"",insuranceamount:"",needsinsurance:!1,declaredvalue:"",parcels:[{itemcategory:"",weight:"",length:"",width:"",height:""}]}),[f,y]=u.useState(!1),[B,g]=u.useState(null),[b,A]=u.useState(null);u.useEffect(()=>{d&&(o({customer_name:"",customer_phone:"",pickupaddress:"",pickuppincode:"",deliveryaddress:"",deliverypincode:"",deliverycity:"",providercompany:"",providerlocation:"",notes:"",service_option:"Normal",finalprice:"",insuranceamount:"",needsinsurance:!1,declaredvalue:"",parcels:[{itemcategory:"",weight:"",length:"",width:"",height:""}]}),g(null),A(null))},[d]);const m=a=>r=>o(t=>({...t,[a]:r.target.value})),s=(a,r)=>t=>{const C=[...i.parcels];C[a]={...C[a],[r]:t.target.value},o(O=>({...O,parcels:C}))},p=()=>{o(a=>({...a,parcels:[...a.parcels,{itemcategory:"",weight:"",length:"",width:"",height:""}]}))},v=a=>{if(i.parcels.length<=1)return;const r=[...i.parcels];r.splice(a,1),o(t=>({...t,parcels:r}))},M=async()=>{y(!0),g(null);try{if(!i.pickupaddress||!i.pickuppincode)throw new Error("Pickup Address and Pincode are required");const a={...i,finalprice:parseFloat(i.finalprice)||0,insuranceamount:parseFloat(i.insuranceamount)||0,parcels:i.parcels.map(r=>({itemcategory:r.itemcategory||"General",weight:parseFloat(r.weight)||1,length:parseFloat(r.length)||1,width:parseFloat(r.width)||1,height:parseFloat(r.height)||1}))};await fe(a),j(),c()}catch(a){console.error(a),g(a.message||"Failed to create booking")}finally{y(!1)}},$=async()=>{try{const a=await fetch(`${I}/admin/pricing/quote`,{method:"POST",headers:z(),body:JSON.stringify({parcels:i.parcels.map(r=>({weight:parseFloat(r.weight)||1,length:parseFloat(r.length)||1,width:parseFloat(r.width)||1,height:parseFloat(r.height)||1}))})});if(a.ok){const r=await a.json();A(r),i.finalprice||o(t=>{var C;return{...t,finalprice:((C=r.basequote)==null?void 0:C.toFixed(2))||""}})}}catch(a){console.error(a)}},K=Array.from(new Set(F.map(a=>a.company).filter(Boolean))),H=F.filter(a=>a.company===i.providercompany).map(a=>a.area||a.address).filter(Boolean);return e.jsxs(e.Fragment,{children:[e.jsx(he,{open:!!B,autoHideDuration:6e3,onClose:()=>g(null),anchorOrigin:{vertical:"top",horizontal:"right"},sx:{zIndex:9999},children:e.jsx(se,{onClose:()=>g(null),severity:"error",variant:"filled",sx:{width:"100%",boxShadow:3},children:B})}),e.jsxs(X,{open:d,onClose:f?void 0:c,maxWidth:"md",fullWidth:!0,children:[e.jsx(Y,{sx:{fontWeight:800},children:"Create New Booking"}),e.jsxs(Z,{dividers:!0,children:[e.jsx(x,{variant:"subtitle1",sx:{fontWeight:700,mb:1,color:"primary.main"},children:"Customer Details"}),e.jsx(n,{container:!0,spacing:2,sx:{mb:3},children:e.jsx(n,{item:!0,xs:12,children:e.jsxs(w,{sx:{display:"flex",gap:2,alignItems:"center"},children:[e.jsx(W,{options:D,sx:{flexGrow:1},getOptionLabel:a=>typeof a=="string"?a:`${a.first_name||""} ${a.last_name||""} - ${a.phone||""}`,onChange:(a,r)=>{o(r&&typeof r=="object"?t=>({...t,customer_name:`${r.first_name||""} ${r.last_name||""}`.trim(),customer_phone:r.phone||""}):t=>({...t,customer_name:"",customer_phone:""}))},renderInput:a=>e.jsx(l,{...a,label:"Select Existing Client",size:"small"})}),e.jsx(k,{variant:"outlined",startIcon:e.jsx(L,{}),onClick:()=>S(!0),sx:{whiteSpace:"nowrap"},children:"New Client"})]})})}),e.jsx(x,{variant:"subtitle1",sx:{fontWeight:700,mb:1,color:"primary.main"},children:"Assigned Provider (From Survey)"}),e.jsxs(n,{container:!0,spacing:2,sx:{mb:3},children:[e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsx(W,{options:K,value:i.providercompany,onChange:(a,r)=>o(t=>({...t,providercompany:r||"",providerlocation:""})),renderInput:a=>e.jsx(l,{...a,label:"Provider Name",size:"small"})})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsx(W,{options:H,value:i.providerlocation,onChange:(a,r)=>o(t=>({...t,providerlocation:r||""})),renderInput:a=>e.jsx(l,{...a,label:"Provider Location / Sub Hub",size:"small"}),disabled:!i.providercompany})})]}),e.jsx(x,{variant:"subtitle1",sx:{fontWeight:700,mb:1,color:"primary.main"},children:"Pickup Location"}),e.jsxs(n,{container:!0,spacing:2,sx:{mb:3},children:[e.jsx(n,{item:!0,xs:12,sm:8,children:e.jsx(l,{fullWidth:!0,label:"Pickup Address *",value:i.pickupaddress,onChange:m("pickupaddress"),size:"small"})}),e.jsx(n,{item:!0,xs:12,sm:4,children:e.jsx(l,{fullWidth:!0,label:"Pickup Pincode *",value:i.pickuppincode,onChange:m("pickuppincode"),size:"small"})})]}),e.jsx(x,{variant:"subtitle1",sx:{fontWeight:700,mb:1,color:"primary.main"},children:"Delivery Destination"}),e.jsxs(n,{container:!0,spacing:2,sx:{mb:3},children:[e.jsx(n,{item:!0,xs:12,sm:12,children:e.jsx(l,{fullWidth:!0,label:"Delivery Address",value:i.deliveryaddress,onChange:m("deliveryaddress"),size:"small"})}),e.jsx(n,{item:!0,xs:12,sm:8,children:e.jsx(W,{freeSolo:!0,options:xe,value:i.deliverycity,onChange:(a,r)=>o(t=>({...t,deliverycity:r||""})),onInputChange:(a,r)=>o(t=>({...t,deliverycity:r||""})),renderInput:a=>e.jsx(l,{...a,label:"Search City",size:"small"})})}),e.jsx(n,{item:!0,xs:12,sm:4,children:e.jsx(l,{fullWidth:!0,label:"Delivery Pincode",value:i.deliverypincode,onChange:m("deliverypincode"),size:"small"})})]}),e.jsxs(w,{sx:{display:"flex",justifyContent:"space-between",alignItems:"center",mb:1},children:[e.jsx(x,{variant:"subtitle1",sx:{fontWeight:700,color:"primary.main"},children:"Parcel Details"}),e.jsx(k,{size:"small",startIcon:e.jsx(L,{}),onClick:p,children:"Add Parcel"})]}),i.parcels.map((a,r)=>e.jsxs(n,{container:!0,spacing:1.5,sx:{mb:2,alignItems:"center"},children:[e.jsx(n,{item:!0,xs:12,sm:3,children:e.jsx(l,{fullWidth:!0,label:"Category (e.g. Box)",value:a.itemcategory,onChange:s(r,"itemcategory"),size:"small"})}),e.jsx(n,{item:!0,xs:6,sm:2,children:e.jsx(l,{fullWidth:!0,label:"Weight (kg)",type:"number",value:a.weight,onChange:s(r,"weight"),size:"small"})}),e.jsx(n,{item:!0,xs:6,sm:2,children:e.jsx(l,{fullWidth:!0,label:"Length (cm)",type:"number",value:a.length,onChange:s(r,"length"),size:"small"})}),e.jsx(n,{item:!0,xs:6,sm:2,children:e.jsx(l,{fullWidth:!0,label:"Width (cm)",type:"number",value:a.width,onChange:s(r,"width"),size:"small"})}),e.jsx(n,{item:!0,xs:4,sm:2,children:e.jsx(l,{fullWidth:!0,label:"Height (cm)",type:"number",value:a.height,onChange:s(r,"height"),size:"small"})}),e.jsx(n,{item:!0,xs:2,sm:1,children:e.jsx(U,{color:"error",onClick:()=>v(r),disabled:i.parcels.length===1,children:e.jsx(ae,{})})})]},r)),e.jsxs(w,{sx:{display:"flex",gap:2,alignItems:"center",mt:2,mb:1},children:[e.jsx(x,{variant:"subtitle1",sx:{fontWeight:700,color:"primary.main"},children:"Price & Insurance"}),e.jsx(k,{size:"small",variant:"outlined",onClick:$,children:"Check Price Estimate"})]}),b&&e.jsxs(x,{variant:"body2",color:"text.secondary",sx:{mb:2,px:1,borderLeft:"4px solid #3b82f6",bgcolor:"#eff6ff",py:1},children:["Estimated Base Price: ",e.jsxs("strong",{children:["₹",(R=b.basequote)==null?void 0:R.toFixed(2)]})," (Chargeable Weight: ",b.chargeableweight," kg)"]}),e.jsxs(n,{container:!0,spacing:2,sx:{mb:3},children:[e.jsx(n,{item:!0,xs:12,sm:3,children:e.jsx(l,{fullWidth:!0,label:"Final Price (₹)",type:"number",value:i.finalprice,onChange:m("finalprice"),size:"small"})}),e.jsx(n,{item:!0,xs:12,sm:9,children:e.jsxs(w,{sx:{display:"flex",gap:2,alignItems:"center"},children:[e.jsx(me,{control:e.jsx(pe,{checked:i.needsinsurance,onChange:a=>{const r=a.target.checked;o(t=>({...t,needsinsurance:r,insuranceamount:r&&t.declaredvalue?(parseFloat(t.declaredvalue)*.01).toFixed(2):t.insuranceamount}))}}),label:"Needs Insurance? (1%)",sx:{whiteSpace:"nowrap"}}),e.jsx(l,{label:"Declared Value (₹)",type:"number",size:"small",sx:{width:150},value:i.declaredvalue,onChange:a=>{const r=a.target.value;o(t=>({...t,declaredvalue:r,insuranceamount:t.needsinsurance&&r?(parseFloat(r)*.01).toFixed(2):t.insuranceamount}))}}),e.jsx(l,{label:"Insurance Amount (₹)",type:"number",size:"small",sx:{width:150},value:i.insuranceamount,onChange:m("insuranceamount")})]})})]}),e.jsx(x,{variant:"subtitle1",sx:{fontWeight:700,mb:1,color:"primary.main"},children:"Additional Information"}),e.jsxs(n,{container:!0,spacing:2,children:[e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsxs(l,{select:!0,fullWidth:!0,label:"Select Speed",value:i.service_option,onChange:m("service_option"),size:"small",children:[e.jsx(E,{value:"Normal",children:"Normal"}),e.jsx(E,{value:"Fast",children:"Fast Express"})]})}),e.jsx(n,{item:!0,xs:12,sm:6,children:e.jsx(l,{fullWidth:!0,label:"Notes / Remarks",value:i.notes,onChange:m("notes"),size:"small"})})]})]}),e.jsxs(ee,{sx:{p:2},children:[e.jsx(k,{onClick:c,disabled:f,children:"Cancel"}),e.jsx(k,{variant:"contained",onClick:M,disabled:f||!i.customer_phone,sx:{bgcolor:"primary.main","&:hover":{bgcolor:"primary.dark"}},children:f?"Creating...":"Create Booking"})]})]}),P&&e.jsx(ie,{open:P,mode:"create",onClose:()=>S(!1),onSaved:()=>{S(!1),j()}})]})}function _e(){const[d,c]=u.useState([]),[j,D]=u.useState([]),[F,P]=u.useState([]),[S,i]=u.useState(!0),[o,f]=u.useState(""),[y,B]=u.useState("All"),[g,b]=u.useState(!1),A=async()=>{i(!0);try{const[s,p,v]=await Promise.all([ge(),Q().catch(()=>[]),je().catch(()=>[])]);c(Array.isArray(s)?s:[]),D(Array.isArray(p)?p:[]),P(Array.isArray(v)?v:[])}catch(s){console.error(s)}finally{i(!1)}};u.useEffect(()=>{A()},[]);const m=d.filter(s=>{const p=(s.bookingno||"").toLowerCase().includes(o.toLowerCase())||(s.pickupaddress||"").toLowerCase().includes(o.toLowerCase())||(s.deliverycity||"").toLowerCase().includes(o.toLowerCase())||(s.providercompany||"").toLowerCase().includes(o.toLowerCase()),v=y==="All"||s.bookingsource===y;return p&&v});return e.jsxs(e.Fragment,{children:[e.jsx(re,{title:"Bookings Management",breadcrumbs:[{label:"Bookings"}],action:e.jsxs(G,{direction:"row",spacing:1.5,alignItems:"center",sx:{width:{xs:"100%",sm:"auto"}},children:[e.jsx(l,{placeholder:"Search bookings...",value:o,onChange:s=>f(s.target.value),size:"small",InputProps:{startAdornment:e.jsx(te,{position:"start",children:e.jsx(J,{fontSize:"small"})}),sx:{borderRadius:2}},sx:{width:{xs:"100%",sm:240},bgcolor:"background.paper",borderRadius:1}}),e.jsx(k,{variant:"contained",startIcon:e.jsx(L,{}),onClick:()=>b(!0),sx:{bgcolor:"#c01227","&:hover":{bgcolor:"#a00f20"},borderRadius:2,fontWeight:700,whiteSpace:"nowrap"},children:"New Booking"})]})}),e.jsxs(ne,{sx:{p:3,borderRadius:4,boxShadow:"0 12px 24px -4px rgba(0,0,0,0.05)"},children:[e.jsx(w,{sx:{borderBottom:1,borderColor:"divider",mb:3},children:e.jsxs(oe,{value:y,onChange:(s,p)=>B(p),"aria-label":"booking source filter tabs",children:[e.jsx(_,{label:"All Bookings",value:"All"}),e.jsx(_,{label:"App Bookings",value:"Customer_App"}),e.jsx(_,{label:"CRM Bookings",value:"CRM_Console"})]})}),S?e.jsx(w,{sx:{display:"flex",justifyContent:"center",py:8},children:e.jsx(q,{})}):e.jsx(le,{sx:{overflowX:"auto",width:"100%"},children:e.jsxs(ce,{children:[e.jsx(de,{children:e.jsxs(N,{sx:{bgcolor:V("#f1f5f9",.6)},children:[e.jsx(h,{sx:{fontWeight:700},children:"Booking No"}),e.jsx(h,{sx:{fontWeight:700},children:"Delivery City"}),e.jsx(h,{sx:{fontWeight:700},children:"Provider"}),e.jsx(h,{sx:{fontWeight:700},children:"Source"}),e.jsx(h,{sx:{fontWeight:700},children:"Price"}),e.jsx(h,{sx:{fontWeight:700},children:"Status"})]})}),e.jsx(ue,{children:m.length===0?e.jsx(N,{children:e.jsx(h,{colSpan:6,align:"center",sx:{py:4},children:"No bookings found."})}):m.map(s=>e.jsxs(N,{hover:!0,children:[e.jsx(h,{sx:{fontWeight:600},children:s.bookingno}),e.jsx(h,{children:s.deliverycity||s.deliverypincode||"N/A"}),e.jsx(h,{children:s.providercompany?e.jsx(T,{label:s.providercompany,size:"small",sx:{mr:1}}):"N/A"}),e.jsx(h,{children:e.jsx(T,{label:s.bookingsource==="CRM_Console"?"CRM":"App",size:"small",variant:"outlined",color:s.bookingsource==="CRM_Console"?"secondary":"primary"})}),e.jsx(h,{children:s.serviceoptions&&s.serviceoptions.length>0?`₹${s.serviceoptions[0].estimatedprice}`:"N/A"}),e.jsx(h,{children:e.jsx(T,{label:s.status,size:"small",color:s.status==="Pending_Pickup"?"warning":"primary"})})]},s.bookingid))})]})})]}),e.jsx(ye,{open:g,onClose:()=>b(!1),onSave:()=>{A()},clients:j,surveys:F})]})}export{_e as default};
|