7 lines
17 KiB
JavaScript
7 lines
17 KiB
JavaScript
import{r as l,j as e,T as A,S as G,B as x,f as J,L as V,l as Q}from"./index-TNj5auBr.js";import{B as K,P as U}from"./PageHeader-B9Ytwo9b.js";import{E as X}from"./EmptyState-DGz1kDMS.js";import{S as Y}from"./StatusChip-Bf11bHPP.js";import{T as Z}from"./TabLabelCount-NU3aDS1r.js";import{N as f,C as ee}from"./ClientFormDialog-4iTaiNea.js";import{p as W,a as E,T as ae}from"./Table-SizHLk8D.js";import{T as j,F as O}from"./TextInput-DL-5979h.js";import{P as $,D as re,L as ie,F as v,a as oe}from"./plus-CZCr3OEY.js";import{C as te}from"./Card-Cvh0wiAW.js";import{S as H}from"./Selector-D-0RMSQ0.js";import{T as ne}from"./trash-2-Mcf_1vVz.js";import{C as se}from"./CheckboxInput-BZ6EBRFv.js";import"./Banner-DVJsik2E.js";import"./map-pin-BQjktuvD.js";import"./TextArea-B4wLAlyG.js";const N="https://api.doormile.com/api/v1",le=["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"],_=()=>{const n=localStorage.getItem("auth_token");return{"Content-Type":"application/json",...n?{Authorization:`Bearer ${n}`}:{}}};async function de(){const n=await fetch(`${N}/admin/bookings`,{headers:_()});if(!n.ok)throw new Error("Failed to fetch bookings");const s=await n.json();return Array.isArray(s)?s:s.data||[]}async function ce(){const n=await fetch(`${N}/admin/competitor-branches?limit=1000`,{headers:_()});if(!n.ok)throw new Error("Failed to fetch surveys");const s=await n.json();return Array.isArray(s)?s:s.data||[]}async function pe(n){const s=await fetch(`${N}/admin/crmbooking`,{method:"POST",headers:_(),body:JSON.stringify(n)});if(!s.ok){const C=await s.json().catch(()=>({}));throw new Error(C.error||"Failed to create booking")}return s.json()}function ue({open:n,onClose:s,onSave:C,clients:F,surveys:P}){var y;const[T,b]=l.useState(!1),[o,d]=l.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:""}]}),[k,g]=l.useState(!1),[D,B]=l.useState(null),[w,m]=l.useState(null);l.useEffect(()=>{n&&(d({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:""}]}),B(null),m(null))},[n]);const c=a=>i=>{d(t=>({...t,[a]:i}))},p=(a,i)=>t=>{const S=[...o.parcels];S[a]={...S[a],[i]:t},d(q=>({...q,parcels:S}))},z=()=>{d(a=>({...a,parcels:[...a.parcels,{itemcategory:"",weight:"",length:"",width:"",height:""}]}))},L=a=>{if(o.parcels.length<=1)return;const i=[...o.parcels];i.splice(a,1),d(t=>({...t,parcels:i}))},h=async()=>{g(!0),B(null);try{if(!o.pickupaddress||!o.pickuppincode)throw new Error("Pickup Address and Pincode are required");const a={...o,finalprice:parseFloat(o.finalprice)||0,insuranceamount:parseFloat(o.insuranceamount)||0,parcels:o.parcels.map(i=>({itemcategory:i.itemcategory||"General",weight:parseFloat(i.weight)||1,length:parseFloat(i.length)||1,width:parseFloat(i.width)||1,height:parseFloat(i.height)||1}))};await pe(a),C(),s()}catch(a){console.error(a),B(a.message||"Failed to create booking")}finally{g(!1)}},I=async()=>{try{const a=await fetch(`${N}/admin/pricing/quote`,{method:"POST",headers:_(),body:JSON.stringify({parcels:o.parcels.map(i=>({weight:parseFloat(i.weight)||1,length:parseFloat(i.length)||1,width:parseFloat(i.width)||1,height:parseFloat(i.height)||1}))})});if(a.ok){const i=await a.json();m(i),o.finalprice||d(t=>{var S;return{...t,finalprice:((S=i.basequote)==null?void 0:S.toFixed(2))||""}})}}catch(a){console.error(a)}},R=Array.from(new Set(P.map(a=>a.company).filter(Boolean))),M=P.filter(a=>a.company===o.providercompany).map(a=>a.area||a.address).filter(Boolean),r=a=>a===""||a==null?void 0:Number(a),u=a=>i=>c(a)(i??"");return e.jsxs(e.Fragment,{children:[e.jsx(re,{isOpen:n,onOpenChange:a=>{a||s()},width:800,maxHeight:"90dvh",purpose:"form",children:e.jsx(V,{header:e.jsx(oe,{title:"Create New Booking",onOpenChange:a=>{a||s()}}),content:e.jsx(Q,{children:e.jsxs(v,{children:[D&&e.jsx("div",{style:{padding:"12px 16px",backgroundColor:"#fef2f2",border:"1px solid #fca5a5",borderRadius:"8px",color:"#b91c1c",fontSize:"0.875rem"},children:D}),e.jsxs("div",{children:[e.jsx("div",{style:{fontWeight:700,marginBottom:"8px",color:"#C01227",fontSize:"0.9rem",textTransform:"uppercase"},children:"Customer Details"}),e.jsxs("div",{style:{display:"flex",gap:"16px",alignItems:"flex-end",flexWrap:"wrap"},children:[e.jsx("div",{style:{flexGrow:1,minWidth:"200px"},children:e.jsxs(O,{label:"Select Client",inputID:"booking-client-input",children:[e.jsx("input",{id:"booking-client-input",list:"booking-clients",onChange:a=>{const i=F.find(t=>`${t.first_name||""} ${t.last_name||""} - ${t.phone||""}`===a.target.value);d(i?t=>({...t,customer_name:`${i.first_name||""} ${i.last_name||""}`.trim(),customer_phone:i.phone||""}):t=>({...t,customer_name:"",customer_phone:""}))}}),e.jsx("datalist",{id:"booking-clients",children:F.map(a=>e.jsx("option",{value:`${a.first_name||""} ${a.last_name||""} - ${a.phone||""}`},a.id))})]})}),e.jsx(x,{variant:"secondary",icon:e.jsx($,{size:14}),onClick:()=>b(!0),label:"New Client"})]})]}),e.jsxs("div",{children:[e.jsx("div",{style:{fontWeight:700,marginBottom:"8px",color:"#C01227",fontSize:"0.9rem",textTransform:"uppercase"},children:"Assigned Provider (From Survey)"}),e.jsxs(v,{direction:"horizontal",children:[e.jsx(H,{label:"Provider Name",placeholder:"Select Provider",hasClear:!0,options:R,value:o.providercompany,onChange:a=>d(i=>({...i,providercompany:a??"",providerlocation:""}))}),e.jsx(H,{label:"Provider Location / Sub Hub",placeholder:"Select Location",hasClear:!0,options:M,value:o.providerlocation,onChange:a=>c("providerlocation")(a??""),isDisabled:!o.providercompany})]})]}),e.jsxs("div",{children:[e.jsx("div",{style:{fontWeight:700,marginBottom:"8px",color:"#C01227",fontSize:"0.9rem",textTransform:"uppercase"},children:"Pickup Location"}),e.jsxs(v,{direction:"horizontal",children:[e.jsx(j,{label:"Pickup Address",isRequired:!0,value:o.pickupaddress,onChange:c("pickupaddress")}),e.jsx(j,{label:"Pickup Pincode",isRequired:!0,value:o.pickuppincode,onChange:c("pickuppincode")})]})]}),e.jsxs("div",{children:[e.jsx("div",{style:{fontWeight:700,marginBottom:"8px",color:"#C01227",fontSize:"0.9rem",textTransform:"uppercase"},children:"Delivery Destination"}),e.jsxs(v,{children:[e.jsx(j,{label:"Delivery Address",value:o.deliveryaddress,onChange:c("deliveryaddress")}),e.jsxs(v,{direction:"horizontal",children:[e.jsxs(O,{label:"City",inputID:"delivery-city-input",children:[e.jsx("input",{id:"delivery-city-input",list:"delivery-cities",value:o.deliverycity,onChange:a=>c("deliverycity")(a.target.value)}),e.jsx("datalist",{id:"delivery-cities",children:le.map(a=>e.jsx("option",{value:a},a))})]}),e.jsx(j,{label:"Delivery Pincode",value:o.deliverypincode,onChange:c("deliverypincode")})]})]})]}),e.jsxs("div",{children:[e.jsxs("div",{style:{display:"flex",justifyContent:"space-between",alignItems:"center",marginBottom:"12px"},children:[e.jsx("div",{style:{fontWeight:700,color:"#C01227",fontSize:"0.9rem",textTransform:"uppercase"},children:"Parcel Details"}),e.jsx(x,{variant:"secondary",size:"sm",icon:e.jsx($,{size:12}),onClick:z,label:"Add Parcel"})]}),o.parcels.map((a,i)=>e.jsxs("div",{style:{display:"flex",gap:"12px",alignItems:"flex-end",marginBottom:"12px",flexWrap:"wrap"},children:[e.jsx("div",{style:{flex:"1 1 150px"},children:e.jsx(j,{label:"Category",isLabelHidden:!0,placeholder:"Category (e.g. Box)",value:a.itemcategory,onChange:p(i,"itemcategory")})}),e.jsx("div",{style:{width:"90px"},children:e.jsx(f,{label:"Weight (kg)",isLabelHidden:!0,placeholder:"Wt (kg)",hasClear:!0,value:r(a.weight),onChange:t=>p(i,"weight")(t??"")})}),e.jsx("div",{style:{width:"90px"},children:e.jsx(f,{label:"Length (cm)",isLabelHidden:!0,placeholder:"L (cm)",hasClear:!0,value:r(a.length),onChange:t=>p(i,"length")(t??"")})}),e.jsx("div",{style:{width:"90px"},children:e.jsx(f,{label:"Width (cm)",isLabelHidden:!0,placeholder:"W (cm)",hasClear:!0,value:r(a.width),onChange:t=>p(i,"width")(t??"")})}),e.jsx("div",{style:{width:"90px"},children:e.jsx(f,{label:"Height (cm)",isLabelHidden:!0,placeholder:"H (cm)",hasClear:!0,value:r(a.height),onChange:t=>p(i,"height")(t??"")})}),e.jsx("button",{onClick:()=>L(i),disabled:o.parcels.length===1,style:{background:"none",border:"none",cursor:"pointer",color:"#dc2626",display:"flex",padding:"9px",borderRadius:"4px"},className:"action-btn",children:e.jsx(ne,{size:16})})]},i))]}),e.jsxs("div",{children:[e.jsxs("div",{style:{display:"flex",gap:"12px",alignItems:"center",marginBottom:"12px",flexWrap:"wrap"},children:[e.jsx("div",{style:{fontWeight:700,color:"#C01227",fontSize:"0.9rem",textTransform:"uppercase"},children:"Price & Insurance"}),e.jsx(x,{variant:"secondary",size:"sm",onClick:I,label:"Check Price Estimate"})]}),w&&e.jsxs("div",{style:{padding:"12px",borderLeft:"4px solid #3b82f6",backgroundColor:"#eff6ff",fontSize:"0.875rem",marginBottom:"16px",color:"#1e3a8a"},children:["Estimated Base Price: ",e.jsxs("strong",{children:["₹",(y=w.basequote)==null?void 0:y.toFixed(2)]})," (Chargeable Weight: ",w.chargeableweight," kg)"]}),e.jsxs(v,{direction:"horizontal",children:[e.jsx(f,{label:"Final Price (₹)",hasClear:!0,value:r(o.finalprice),onChange:u("finalprice")}),e.jsx("div",{children:e.jsx(se,{label:"Needs Insurance? (1%)",value:o.needsinsurance,onChange:a=>{d(i=>({...i,needsinsurance:a,insuranceamount:a&&i.declaredvalue?(parseFloat(i.declaredvalue)*.01).toFixed(2):i.insuranceamount}))}})}),e.jsx(f,{label:"Declared Value",hasClear:!0,value:r(o.declaredvalue),onChange:a=>{d(i=>({...i,declaredvalue:a??"",insuranceamount:i.needsinsurance&&a!=null?(a*.01).toFixed(2):i.insuranceamount}))}}),e.jsx(f,{label:"Insurance Amount",hasClear:!0,value:r(o.insuranceamount),onChange:u("insuranceamount")})]})]}),e.jsxs("div",{children:[e.jsx("div",{style:{fontWeight:700,marginBottom:"8px",color:"#C01227",fontSize:"0.9rem",textTransform:"uppercase"},children:"Additional Information"}),e.jsxs(v,{direction:"horizontal",children:[e.jsx(H,{label:"Select Speed",options:[{value:"Normal",label:"Normal"},{value:"Fast",label:"Fast Express"}],value:o.service_option,onChange:c("service_option")}),e.jsx(j,{label:"Notes / Remarks",value:o.notes,onChange:c("notes")})]})]})]})}),footer:e.jsx(ie,{hasDivider:!0,children:e.jsxs("div",{style:{display:"flex",justifyContent:"flex-end",gap:"12px"},children:[e.jsx(x,{label:"Cancel",variant:"ghost",onClick:s,isDisabled:k}),e.jsx(x,{label:k?"Creating...":"Create Booking",variant:"primary",onClick:h,isDisabled:k||!o.customer_phone,isLoading:k})]})})})}),T&&e.jsx(ee,{open:T,mode:"create",onClose:()=>b(!1),onSaved:()=>{b(!1),C()}})]})}function Pe(){const[n,s]=l.useState([]),[C,F]=l.useState([]),[P,T]=l.useState([]),[b,o]=l.useState(!0),[d,k]=l.useState(""),[g,D]=l.useState("All"),[B,w]=l.useState(!1),[m,c]=l.useState(0),[p,z]=l.useState(10),L=async()=>{o(!0);try{const[r,u,y]=await Promise.all([de(),J().catch(()=>[]),ce().catch(()=>[])]);s(Array.isArray(r)?r:[]),F(Array.isArray(u)?u:[]),T(Array.isArray(y)?y:[])}catch(r){console.error(r)}finally{o(!1)}};l.useEffect(()=>{L()},[]);const h=n.filter(r=>{const u=(r.bookingno||"").toLowerCase().includes(d.toLowerCase())||(r.pickupaddress||"").toLowerCase().includes(d.toLowerCase())||(r.deliverycity||"").toLowerCase().includes(d.toLowerCase())||(r.providercompany||"").toLowerCase().includes(d.toLowerCase()),y=g==="All"||r.bookingsource===g;return u&&y}),I=h.slice(m*p,m*p+p),R=l.useMemo(()=>[{key:"bookingno",header:e.jsx("div",{style:{paddingLeft:"12px"},children:"Booking No"}),width:W(1.5),renderCell:r=>e.jsx("div",{style:{paddingLeft:"12px"},children:e.jsx(A,{type:"body",weight:"semibold",children:r.bookingno})})},{key:"deliverycity",header:"Delivery City",width:W(1),renderCell:r=>{const u=r.deliverycity||r.deliverypincode;return u?e.jsx(A,{type:"body",children:u}):e.jsx(A,{type:"supporting",color:"disabled",children:"N/A"})}},{key:"providercompany",header:"Provider",width:W(1),renderCell:r=>r.providercompany?e.jsx(K,{variant:"neutral",label:r.providercompany}):e.jsx(A,{type:"supporting",color:"disabled",children:"N/A"})},{key:"bookingsource",header:"Source",width:E(100),renderCell:r=>e.jsx(K,{variant:r.bookingsource==="CRM_Console"?"neutral":"red",label:r.bookingsource==="CRM_Console"?"CRM":"App"})},{key:"price",header:"Price",width:E(120),renderCell:r=>e.jsx(A,{type:"body",weight:"semibold",hasTabularNumbers:!0,children:r.serviceoptions&&r.serviceoptions.length>0?`₹${r.serviceoptions[0].estimatedprice}`:"N/A"})},{key:"status",header:"Status",width:E(150),renderCell:r=>e.jsx(Y,{status:r.status})}],[]),M=l.useMemo(()=>{const r={All:n.length,Customer_App:0,CRM_Console:0};return n.forEach(u=>{u.bookingsource==="CRM_Console"?r.CRM_Console+=1:r.Customer_App+=1}),r},[n]);return e.jsxs("div",{style:{fontFamily:"system-ui, sans-serif"},children:[e.jsx(U,{title:"Bookings Management",action:e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"12px",flexWrap:"nowrap"},children:[e.jsx("div",{style:{flex:"0 1 320px",minWidth:"200px"},children:e.jsx(j,{label:"Search bookings",isLabelHidden:!0,placeholder:"Search bookings…",startIcon:e.jsx(G,{size:16}),hasClear:!0,value:d,onChange:r=>{k(r),c(0)}})}),e.jsx(x,{variant:"primary",icon:e.jsx($,{size:14}),onClick:()=>w(!0),label:"New Booking",style:{flexShrink:0}})]})}),e.jsxs(te,{style:{marginTop:"24px",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.jsx("div",{style:{display:"flex",padding:"0 24px",borderBottom:"1px solid rgba(5, 54, 89, 0.06)",gap:"24px",overflowX:"auto"},children:[{label:"All Bookings",value:"All"},{label:"App Bookings",value:"Customer_App"},{label:"CRM Bookings",value:"CRM_Console"}].map(r=>e.jsx("div",{onClick:()=>{D(r.value),c(0)},style:{padding:"16px 0",cursor:"pointer",borderBottom:g===r.value?"2px solid #C01227":"2px solid transparent",color:g===r.value?"#C01227":"#64748b",fontWeight:g===r.value?600:500,fontSize:"0.875rem",transition:"all 0.2s"},children:e.jsx(Z,{label:r.label,count:M[r.value]||0,active:g===r.value})},r.value))}),b?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"}})}):h.length===0?e.jsx(X,{title:"No bookings found",caption:"Try a different filter or search term, or create a new booking."}):e.jsx("div",{style:{paddingTop:"12px",paddingBottom:"12px"},children:e.jsx(ae,{data:I,columns:R,idKey:"bookingid",density:"balanced",dividers:"rows",hasHover:!0,isStriped:!0,textOverflow:"truncate"})}),!b&&h.length>0&&e.jsxs("div",{style:{display:"flex",justifyContent:"flex-end",alignItems:"center",padding:"12px 24px",borderTop:"1px solid #e2e8f0",gap:"20px",flexWrap:"wrap"},children:[e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px",fontSize:"0.875rem",color:"#475569"},children:[e.jsx("span",{children:"Rows per page:"}),e.jsx("select",{value:p,onChange:r=>{z(Number(r.target.value)),c(0)},className:"rpp-select",children:[5,10,25].map(r=>e.jsx("option",{value:r,children:r},r))})]}),e.jsxs("span",{style:{fontSize:"0.875rem",color:"#475569"},children:[h.length===0?"0-0":`${m*p+1}-${Math.min((m+1)*p,h.length)}`," of ",h.length]}),e.jsxs("div",{style:{display:"flex",gap:"8px"},children:[e.jsx(x,{label:"Prev",variant:"secondary",size:"sm",onClick:()=>c(r=>Math.max(0,r-1)),isDisabled:m===0}),e.jsx(x,{label:"Next",variant:"secondary",size:"sm",onClick:()=>c(r=>Math.min(Math.ceil(h.length/p)-1,r+1)),isDisabled:m>=Math.ceil(h.length/p)-1})]})]})]}),e.jsx(ue,{open:B,onClose:()=>w(!1),onSave:()=>{L()},clients:C,surveys:P}),e.jsx("style",{children:`
|
|
.action-btn:hover {
|
|
background-color: #f1f5f9;
|
|
}
|
|
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
|
|
`})]})}export{Pe as default};
|