2 lines
14 KiB
JavaScript
2 lines
14 KiB
JavaScript
import{b as u,j as e,B as g,z as R,l as J,C as q,f as V,T as x,I as Q,M}from"./index-DL6WD4dx.js";import{d as L,D as U,a as X,b as Y,c as Z}from"./Close-VT01TMVB.js";import{d as ee}from"./DeleteOutline-e0zZxTBj.js";import{P as ae,C as T,A as re,G as n}from"./PageHeader-BeRWmE7s.js";import{A as W,C as se}from"./ClientFormDialog-BYAGTUZW.js";import{C as ie}from"./Link-CuCW2FcM.js";import{T as l}from"./TextField-DEgFhXxt.js";import{I as te}from"./InputAdornment-CtWzGXj_.js";import{B as w}from"./Button-Bslat0pn.js";import{T as ne,a as _}from"./Tabs-Bc9uopQa.js";import{T as oe,a as le,b as ce,c as N,d as de}from"./TableRow-Dw3UOEXA.js";import{T as h}from"./TableCell-Bm3XvvAz.js";import{S as ue}from"./Snackbar-DC9qF3L-.js";import{F as he,C as me}from"./FormControlLabel-BFovcFY3.js";import"./SwitchBase--u-Td-rZ.js";const I="https://api.doormile.com/api/v1",pe=["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 xe(){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 ge(){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 je(d){const c=await fetch(`${I}/admin/crmbooking`,{method:"POST",headers:z(),body:JSON.stringify(d)});if(!c.ok){const f=await c.json().catch(()=>({}));throw new Error(f.error||"Failed to create booking")}return c.json()}function fe({open:d,onClose:c,onSave:f,clients:D,surveys:F}){var E;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:""}]}),[y,b]=u.useState(!1),[B,j]=u.useState(null),[v,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:""}]}),j(null),A(null))},[d]);const m=a=>r=>o(t=>({...t,[a]:r.target.value})),s=(a,r)=>t=>{const k=[...i.parcels];k[a]={...k[a],[r]:t.target.value},o(G=>({...G,parcels:k}))},p=()=>{o(a=>({...a,parcels:[...a.parcels,{itemcategory:"",weight:"",length:"",width:"",height:""}]}))},C=a=>{if(i.parcels.length<=1)return;const r=[...i.parcels];r.splice(a,1),o(t=>({...t,parcels:r}))},$=async()=>{b(!0),j(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 je(a),f(),c()}catch(a){console.error(a),j(a.message||"Failed to create booking")}finally{b(!1)}},H=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 k;return{...t,finalprice:((k=r.basequote)==null?void 0:k.toFixed(2))||""}})}}catch(a){console.error(a)}},K=Array.from(new Set(F.map(a=>a.company).filter(Boolean))),O=F.filter(a=>a.company===i.providercompany).map(a=>a.area||a.address).filter(Boolean);return e.jsxs(e.Fragment,{children:[e.jsx(ue,{open:!!B,autoHideDuration:6e3,onClose:()=>j(null),anchorOrigin:{vertical:"top",horizontal:"right"},sx:{zIndex:9999},children:e.jsx(re,{onClose:()=>j(null),severity:"error",variant:"filled",sx:{width:"100%",boxShadow:3},children:B})}),e.jsxs(U,{open:d,onClose:y?void 0:c,maxWidth:"md",fullWidth:!0,children:[e.jsx(X,{sx:{fontWeight:800},children:"Create New Booking"}),e.jsxs(Y,{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(g,{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(w,{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:O,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:pe,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(g,{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(w,{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(Q,{color:"error",onClick:()=>C(r),disabled:i.parcels.length===1,children:e.jsx(ee,{})})})]},r)),e.jsxs(g,{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(w,{size:"small",variant:"outlined",onClick:H,children:"Check Price Estimate"})]}),v&&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:["₹",(E=v.basequote)==null?void 0:E.toFixed(2)]})," (Chargeable Weight: ",v.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(g,{sx:{display:"flex",gap:2,alignItems:"center"},children:[e.jsx(he,{control:e.jsx(me,{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(M,{value:"Normal",children:"Normal"}),e.jsx(M,{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(Z,{sx:{p:2},children:[e.jsx(w,{onClick:c,disabled:y,children:"Cancel"}),e.jsx(w,{variant:"contained",onClick:$,disabled:y||!i.customer_phone,sx:{bgcolor:"primary.main","&:hover":{bgcolor:"primary.dark"}},children:y?"Creating...":"Create Booking"})]})]}),P&&e.jsx(se,{open:P,mode:"create",onClose:()=>S(!1),onSaved:()=>{S(!1),f()}})]})}function Te(){const[d,c]=u.useState([]),[f,D]=u.useState([]),[F,P]=u.useState([]),[S,i]=u.useState(!0),[o,y]=u.useState(""),[b,B]=u.useState("All"),[j,v]=u.useState(!1),A=async()=>{i(!0);try{const[s,p,C]=await Promise.all([xe(),V().catch(()=>[]),ge().catch(()=>[])]);c(Array.isArray(s)?s:[]),D(Array.isArray(p)?p:[]),P(Array.isArray(C)?C:[])}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()),C=b==="All"||s.bookingsource===b;return p&&C});return e.jsxs(g,{sx:{p:3,bgcolor:"#f8fafc",minHeight:"80vh"},children:[e.jsx(ae,{title:"Bookings Management",breadcrumbs:[{label:"Bookings"}]}),e.jsxs(ie,{sx:{mt:3,p:3,borderRadius:4,boxShadow:"0 12px 24px -4px rgba(0,0,0,0.05)"},children:[e.jsxs(g,{sx:{display:"flex",justifyContent:"space-between",mb:3},children:[e.jsx(l,{placeholder:"Search bookings...",value:o,onChange:s=>y(s.target.value),size:"small",InputProps:{startAdornment:e.jsx(te,{position:"start",children:e.jsx(J,{})}),sx:{borderRadius:3,bgcolor:R("#e2e8f0",.4)}}}),e.jsx(w,{variant:"contained",startIcon:e.jsx(L,{}),onClick:()=>v(!0),sx:{borderRadius:2},children:"New Booking"})]}),e.jsx(g,{sx:{borderBottom:1,borderColor:"divider",mb:3},children:e.jsxs(ne,{value:b,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(g,{sx:{display:"flex",justifyContent:"center",py:8},children:e.jsx(q,{})}):e.jsx(oe,{children:e.jsxs(le,{children:[e.jsx(ce,{children:e.jsxs(N,{sx:{bgcolor:R("#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(de,{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(fe,{open:j,onClose:()=>v(!1),onSave:()=>{A()},clients:f,surveys:F})]})}export{Te as default};
|