import{r as i,j as e}from"./client-BbMi5ewG.js";import{B as u}from"./Button-1dacbE2T.js";import{I as E}from"./input-DhCVOJC-.js";import{S as L,a as z,b as T,c as X,f as C}from"./select-CPHcJnpt.js";import{a as $,s as V}from"./store-jX25q14N.js";import{D as g}from"./DollarCircle-DjPBQZLr.js";import{f as S}from"./priceFormatter-Dg4GlACg.js";import{L as _}from"./loader-circle--S58donA.js";import{C as q}from"./circle-check-big-uMTsaxsq.js";import{C as p}from"./check-DMod5j-l.js";import{c as G,X as H}from"./utils-Q0fnGDhj.js";/**
 * @license lucide-react v0.525.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const J=[["path",{d:"M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8",key:"1357e3"}],["path",{d:"M3 3v5h5",key:"1xhq8a"}]],K=G("rotate-ccw",J);function me({appointment:s,...k}){var w,v;const{module:b}=k,[h,f]=i.useState(!1),[O,Q]=i.useState(!1),[r,y]=i.useState(""),[U,W]=i.useState(""),[N,j]=i.useState("paid"),[l,n]=i.useState(!1),{updateAppointmentPayment:F}=$(V),t=(w=s==null?void 0:s.payment)==null?void 0:w.status,d=parseFloat(((v=s==null?void 0:s.payment)==null?void 0:v.amount)||0)||0,x=()=>{var m,c;const a=parseFloat(((m=s==null?void 0:s.payment)==null?void 0:m.total_amount)||(s==null?void 0:s.price)||0)||0,o=parseFloat(((c=s==null?void 0:s.payment)==null?void 0:c.amount)||0)||0;return t==="partial_payment"?Math.max(0,a-o):a},A=async()=>{n(!0);try{const a="TXN_"+Date.now(),o=await F(s.id,{appointment:s,amount:parseFloat(r),status:N,transaction_id:a},b);y(""),j("paid"),f(!1)}catch(a){console.error("Payment failed:",a)}finally{n(!1)}},M=async()=>{var a,o,m;n(!0);try{const c=parseFloat(((a=s==null?void 0:s.customer)==null?void 0:a.price)||(s==null?void 0:s.price)||0)||0,R=parseFloat(((o=s==null?void 0:s.payment)==null?void 0:o.amount)||0)||0,B=parseFloat(((m=s==null?void 0:s.payment)==null?void 0:m.due_amount)||0)||0,Y=t==="partial_payment"?Math.max(0,c-R):c,I="TXN_"+Date.now();await F(s.id,{appointment:s,amount:B,status:"paid",transaction_id:I},b)}catch(c){console.error("Mark as paid failed:",c)}finally{n(!1)}},D=["pending","failed","partial_payment"].includes(t),P=(s==null?void 0:s.status)==="confirmed"&&!l;return e.jsxs("div",{className:"space-y-3",children:[D&&e.jsx(e.Fragment,{children:h?e.jsxs("div",{className:"space-y-3 border p-4 rounded-lg bg-gray-50",children:[e.jsx("h4",{className:"font-medium text-title",children:"Process Payment"}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(E,{type:"number",placeholder:"Enter amount",value:r,onChange:a=>y(a.target.value),disabled:l,min:"0",step:"1",className:`bg-white
                                        ${r&&parseFloat(r)>x()?" border-red-300 focus:border-red-500":""}
                                        `}),e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsxs("span",{className:"text-title2",children:["Max: $",(x()||0).toFixed(2)]}),r&&parseFloat(r)>x()&&e.jsx("span",{className:"text-red-500",children:"Amount exceeds maximum"})]})]}),e.jsxs(L,{value:N,onValueChange:j,disabled:l,children:[e.jsx(z,{className:"bg-white",children:e.jsx(T,{placeholder:"Select status"})}),e.jsxs(X,{children:[e.jsx(C,{value:"paid",children:"Paid"}),e.jsx(C,{value:"partial_payment",children:"Partial Payment"})]})]}),e.jsxs("div",{className:"flex gap-2 flex-wrap",children:[e.jsxs(u,{onClick:A,className:"flex-1 bg-[#16803C] hover:bg-green-600 text-white font-medium py-2.5 gap-2",disabled:!r||parseFloat(r)<=0||parseFloat(r)>x()||l,children:[l?e.jsx(_,{className:"w-4 h-4 mr-2 animate-spin"}):e.jsx(g,{size:"14px",className:"text-white"}),l?"Confirming...":"Confirm Payment"]}),e.jsx(u,{variant:"outline",className:"border border-red-700 text-red-700 hover:bg-red-700 hover:text-white",onClick:()=>{f(!1),y(""),j("paid")},disabled:l,children:"Cancel"})]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mb-4",children:[e.jsx("div",{className:"flex gap-2 mb-2",children:e.jsxs(u,{onClick:M,className:"flex-1 bg-[#16803C] hover:bg-green-600 text-white font-medium py-2.5 h-[46px] gap-1",disabled:!P,children:[l?e.jsx(_,{className:"w-4 h-4 mr-2 animate-spin"}):e.jsx(q,{className:"w-4 h-4 mr-2"}),l?"Processing...":"Mark as Paid"]})}),e.jsx("div",{className:"flex gap-2",children:e.jsxs(u,{onClick:()=>f(!0),variant:"outline",className:"flex-1  bg-[#F5FAFF] border-[#BDCFFF] text-primary hover:bg-[#E4F1FF] hover-border-[#9CB7FF] font-medium py-2.5 h-[46px] gap-1",disabled:!P,children:[e.jsx(g,{size:"16px",className:"text-primary"}),"Custom Payment"]})})]}),(s==null?void 0:s.status)!=="confirmed"&&e.jsx("p",{className:"text-xs text-title2 text-center",children:"Payment can only be processed for confirmed appointments"})]})}),t==="paid"&&e.jsxs("div",{className:"flex items-center justify-between p-3 bg-green-50 text-green-700 rounded-lg border border-green-200 md:gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(p,{className:"w-5 h-5"}),e.jsx("span",{className:"font-medium md:text-sm",children:"Payment Completed"})]}),d>0&&e.jsx("span",{className:"text-sm font-medium break-all",children:S(parseFloat(d).toFixed(2),s.currency)})]}),t==="failed"&&!h&&e.jsxs("div",{className:"flex items-center justify-center gap-2 p-3 bg-red-50 text-red-700 rounded-lg border border-red-200",children:[e.jsx(H,{className:"w-5 h-5"}),e.jsx("span",{className:"font-medium",children:"Payment Failed"})]}),t==="partial_payment"&&!h&&e.jsxs("div",{className:"flex items-center justify-between p-3 bg-yellow-50 text-yellow-700 rounded-lg border border-yellow-300",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(g,{size:"16px"}),e.jsx("span",{className:"font-medium",children:"Partially Paid"})]}),d>0&&e.jsx("span",{className:"text-sm font-medium",children:S(parseFloat(d).toFixed(2),s.currency)})]}),t==="refunded"&&e.jsxs("div",{className:"flex items-center justify-between p-3 bg-blue-50 text-primary rounded-lg border border-blue-200",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(K,{className:"w-5 h-5"}),e.jsx("span",{className:"font-medium",children:"Refund Processed"})]}),d>0&&e.jsxs("span",{className:"text-sm font-medium",children:["$",d.toFixed(2)]})]})]})}export{me as P};
//# sourceMappingURL=PaymentSection-BdBEJKqf.js.map
