1import{z as Z,g as ee,r as p,s as g,j as s,B as h,P as te,f as _,K as se}from"./index-Cokc-GQZ.js";import{g as v,c as ae}from"./pdvMasterSession-DBJzWTBY.js";import{u as re}from"./useThermalPrinter-3JDQYXPx.js";import{M as ie,f as oe}from"./loteFilters-Baa6BJ-2.js";import{B as ne,a as de,P as le,T as ce,b as me}from"./TerminalChargeDialog-MiHQ_O_S.js";import{P as pe}from"./PdvSeatPickerDialog-BEwmee6X.js";import{L as ue}from"./loader-circle-DB1BNH8B.js";import{A as xe}from"./arrow-left-DHeiQWhd.js";import{T as B}from"./trash-2-mL6vrI-E.js";import{C as I}from"./credit-card-QiC-QRE1.js";import{G as ge}from"./gift-B9-qo-jM.js";import"./dialog-CzTGhqT3.js";import"./index-CbmzzImk.js";import"./Combination-DzednwTt.js";import"./masks-DtSNhuR_.js";import"./pdvSession-CG4F53pd.js";import"./datetime-CyBskthS.js";import"./circle-check-CVsvZdOY.js";import"./printer-Hi--7TYz.js";import"./mail-CVBREMIu.js";import"./message-circle-DVysKLXc.js";import"./circle-x-jbrflg5z.js";import"./triangle-alert-hPX5BsVb.js";import"./SeatSelector-B3gUSjtw.js";import"./badge-ByZJNgBF.js";import"./file-code-BP-aug_p.js";const R={dinheiro:{label:"Dinheiro",icon:me,color:"bg-emerald-600"},debito:{label:"Débito",icon:I,color:"bg-blue-600"},credito:{label:"Crédito",icon:I,color:"bg-purple-600"},pix:{label:"PIX",icon:se,color:"bg-teal-600"},cortesia:{label:"Cortesia",icon:ge,color:"bg-amber-600"}};function Ge(){var M,A,D;const{token:d,eventId:l}=Z(),N=ee(),[n,O]=p.useState(null),[G,$]=p.useState(!0),[x,f]=p.useState([]),[K,b]=p.useState(!1),[P,y]=p.useState(null),[V,T]=p.useState("dinheiro"),[Q,X]=p.useState(new Set),[u,w]=p.useState(null),[m,j]=p.useState(null),{name:k,connect:H,connecting:z,supported:J,hasSaved:F,savedName:_e}=re(),C=((M=n==null?void 0:n.event)==null?void 0:M.pdv_modo_pagamento)==="integrado"&&!!((A=n==null?void 0:n.pdv)!=null&&A.terminal_id),q=async()=>{if(!d||!l)return;const t=v(d);if(!t){N(`/pdv/m/${d}`);return}$(!0);const{data:e}=await g.rpc("get_pdv_master_event_data",{_token:d,_password:t.password,_event_id:l});if(!(e!=null&&e.ok)){_.error("Sessão expirada"),ae(d),N(`/pdv/m/${d}`);return}O(e),$(!1)};if(p.useEffect(()=>{q()},[d,l]),p.useEffect(()=>{var t;!l||!((t=n==null?void 0:n.tipos)!=null&&t.length)||(async()=>{const e=n.tipos.map(i=>i.id),{data:r}=await g.from("lugares").select("tipo_ingresso_id").eq("event_id",l).in("tipo_ingresso_id",e),a=new Set;(r||[]).forEach(i=>a.add(i.tipo_ingresso_id)),X(a)})()},[l,n==null?void 0:n.tipos]),G||!n)return s.jsx("div",{className:"min-h-screen flex items-center justify-center",children:s.jsx(ue,{className:"h-8 w-8 animate-spin text-primary"})});const U=t=>oe((n.lotes||[]).filter(e=>e.tipo_ingresso_id===t),"pdv_master").filter(e=>e.quantidade>e.quantidade_vendida),L=(t,e)=>{const r=Number(e?e.preco:t.preco);if(Q.has(t.id)){w({tipo:t,lote_id:(e==null?void 0:e.id)||null,preco:r});return}f(a=>{const i=`${t.id}::${(e==null?void 0:e.id)||""}`,o=a.find(c=>`${c.tipo_ingresso_id}::${c.lote_id||""}`===i&&!(c.lugar_ids&&c.lugar_ids.length));return o?a.map(c=>c===o?{...c,quantidade:c.quantidade+1}:c):[...a,{tipo_ingresso_id:t.id,lote_id:(e==null?void 0:e.id)||null,tipo_nome:t.nome,preco:r,quantidade:1}]})},W=t=>{if(!u)return;const e=u.tipo,r=(e==null?void 0:e.emissao_modo)==="unico"?"unico":"por_integrante",a=Math.max(1,Number(e==null?void 0:e.unidade_camarote)||1),i=t.map(o=>r==="por_integrante"&&a>1?{tipo_ingresso_id:e.id,lote_id:u.lote_id,tipo_nome:e.nome,preco:u.preco,quantidade:a,lugar_ids:Array(a).fill(o)}:{tipo_ingresso_id:e.id,lote_id:u.lote_id,tipo_nome:e.nome,preco:u.preco*a,quantidade:1,lugar_ids:[o],preco_unit_display:u.preco,qtd_display:a});f(o=>[...o,...i]),w(null)},S=(t,e)=>f(r=>{const a=[...r];return a[t].lugar_ids&&a[t].lugar_ids.length>0?(e<0&&a.splice(t,1),a):(a[t].quantidade+=e,a[t].quantidade<=0&&a.splice(t,1),a)}),E=x.reduce((t,e)=>t+e.preco*e.quantidade,0),Y=async t=>{var r;if(x.length===0){_.error("Carrinho vazio");return}if(!d||!l)return;const e=v(d);if(e){if(C&&(t==="credito"||t==="debito"||t==="pix")){b(!0);try{const a=x.map(c=>({tipo_ingresso_id:c.tipo_ingresso_id,lote_id:c.lote_id,quantidade:c.quantidade,preco_unitario:c.preco,tipo_nome:c.tipo_nome,lugar_ids:c.lugar_ids})),{data:i,error:o}=await g.functions.invoke("pdv-terminal",{body:{action:"charge",token:d,password:e.password,master:!0,event_id:l,forma_pagamento:t,parcelas:1,itens:a,dados_comprador:(r=n.link)!=null&&r.nome_operador?{operador_nome:n.link.nome_operador}:{}}});if(o||!(i!=null&&i.ok)){_.error("Erro maquininha: "+((o==null?void 0:o.message)||(i==null?void 0:i.details)||(i==null?void 0:i.error)||""));return}j({id:i.intent_id,forma:t})}finally{b(!1)}return}b(!0);try{const{data:a,error:i}=await g.rpc("pdv_master_create_sale",{_token:d,_password:e.password,_event_id:l,_itens:x.map(o=>({tipo_ingresso_id:o.tipo_ingresso_id,lote_id:o.lote_id,quantidade:o.quantidade,preco_unitario:o.preco,lugar_ids:o.lugar_ids&&o.lugar_ids.length>0?o.lugar_ids:void 0})),_dados_comprador:{},_forma_pagamento:t});if(i||!(a!=null&&a.ok)){_.error("Erro: "+((i==null?void 0:i.message)||(a==null?void 0:a.error)||""));return}_.success(`Venda ${a.pedido_codigo} confirmada!`),f([]),T(t),y(a),q()}finally{b(!1)}}};return s.jsxs("div",{className:"min-h-screen bg-background flex flex-col",children:[s.jsx("header",{className:"bg-primary text-primary-foreground px-3 py-2 sticky top-0 z-10",children:s.jsxs("div",{className:"flex items-center justify-between gap-2",children:[s.jsx(h,{size:"icon",variant:"ghost",className:"text-primary-foreground h-8 w-8",onClick:()=>N(`/pdv/m/${d}/eventos`),children:s.jsx(xe,{className:"h-4 w-4"})}),s.jsxs("div",{className:"min-w-0 flex-1 text-center",children:[s.jsx("h1",{className:"font-bold text-sm truncate",children:n.event.titulo}),s.jsxs("p",{className:"text-[11px] opacity-90 truncate",children:[n.pdv.nome," · trocar evento â"]})]}),s.jsxs(h,{size:"sm",onClick:H,disabled:z||!J,className:`h-8 gap-1 text-[11px] ${k?"bg-emerald-500 text-white":F?"bg-amber-500 text-white":"bg-white text-primary"}`,children:[k?s.jsx(ne,{className:"h-3.5 w-3.5"}):s.jsx(de,{className:"h-3.5 w-3.5"}),z?"...":k?"OK":F?"Reconectar":"Conectar"]})]})}),s.jsx("div",{className:"flex-1 p-3 space-y-3 overflow-y-auto pb-[280px]",children:s.jsx("div",{className:"grid grid-cols-2 gap-2",children:(n.tipos||[]).flatMap(t=>{const e=U(t.id);if(e.length===0){const r=t.quantidade_total-t.quantidade_vendida,a=r<=0;return[s.jsxs("button",{onClick:()=>!a&&L(t,null),disabled:a,className:`rounded-lg border-2 p-3 text-left transition active:scale-95 ${a?"opacity-40 border-muted bg-muted":"border-primary/30 bg-card hover:border-primary"}`,children:[s.jsx("div",{className:"font-bold text-sm leading-tight line-clamp-2",children:t.nome}),s.jsxs("div",{className:"text-lg font-extrabold text-primary mt-1",children:["R$ ",Number(t.preco).toFixed(2)]}),s.jsx("div",{className:"text-[10px] text-muted-foreground",children:a?"Esgotado":`${r} disp.`})]},t.id)]}return e.map(r=>{const a=Number(r.preco),i=r.quantidade-r.quantidade_vendida,o=i<=0;return s.jsxs("button",{onClick:()=>!o&&L(t,r),disabled:o,className:`rounded-lg border-2 p-3 text-left transition active:scale-95 ${o?"opacity-40 border-muted bg-muted":"border-primary/30 bg-card hover:border-primary"}`,children:[s.jsx("div",{className:"font-bold text-sm leading-tight line-clamp-2",children:t.nome}),s.jsx("div",{className:"text-[10px] text-muted-foreground mt-0.5 truncate",children:r.nome}),s.jsxs("div",{className:"text-lg font-extrabold text-primary mt-1",children:["R$ ",a.toFixed(2)]}),s.jsx("div",{className:"text-[10px] text-muted-foreground",children:o?"Esgotado":`${i} disp.`})]},`${t.id}-${r.id}`)})})})}),s.jsxs("div",{className:"fixed bottom-0 left-0 right-0 bg-card border-t shadow-2xl",children:[x.length>0&&s.jsx("div",{className:"px-3 py-2 max-h-32 overflow-y-auto border-b",children:x.map((t,e)=>{const r=!!(t.lugar_ids&&t.lugar_ids.length>0);return s.jsxs("div",{className:"flex items-center gap-2 py-1",children:[s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"text-sm font-medium truncate",children:[t.tipo_nome,r&&s.jsx("span",{className:"text-primary",children:" · lugar marcado"})]}),s.jsxs("div",{className:"text-[11px] text-muted-foreground",children:["R$ ",(t.preco_unit_display??t.preco).toFixed(2)," à ",t.qtd_display??t.quantidade]})]}),!r&&s.jsxs(s.Fragment,{children:[s.jsx(h,{size:"icon",variant:"ghost",className:"h-7 w-7",onClick:()=>S(e,-1),children:s.jsx(ie,{className:"h-3 w-3"})}),s.jsx("span",{className:"w-6 text-center text-sm font-bold",children:t.quantidade}),s.jsx(h,{size:"icon",variant:"ghost",className:"h-7 w-7",onClick:()=>S(e,1),children:s.jsx(te,{className:"h-3 w-3"})})]}),s.jsx(h,{size:"icon",variant:"ghost",className:"h-7 w-7 text-destructive",onClick:()=>S(e,-999),children:s.jsx(B,{className:"h-3 w-3"})})]},e)})}),s.jsxs("div",{className:"px-3 py-2 flex items-center justify-between bg-primary/5",children:[s.jsxs("div",{children:[s.jsx("div",{className:"text-[10px] text-muted-foreground uppercase",children:"Total"}),s.jsxs("div",{className:"text-2xl font-extrabold text-primary",children:["R$ ",E.toFixed(2)]})]}),x.length>0&&s.jsxs(h,{variant:"outline",size:"sm",onClick:()=>f([]),className:"gap-1",children:[s.jsx(B,{className:"h-3.5 w-3.5"})," Limpar"]})]}),s.jsx("div",{className:"grid grid-cols-3 gap-1.5 p-2",children:Object.keys(R).filter(t=>{var e;return t==="cortesia"?!!((e=n.link)!=null&&e.cortesia_habilitada):!0}).map(t=>{const e=R[t],r=e.icon;return s.jsxs("button",{onClick:()=>Y(t),disabled:K||x.length===0,className:`${e.color} text-white rounded-md py-2.5 flex flex-col items-center gap-0.5 disabled:opacity-40 active:scale-95`,children:[s.jsx(r,{className:"h-4 w-4"}),s.jsx("span",{className:"text-[10px] font-bold",children:e.label})]},t)})})]}),s.jsx(le,{open:!!P,sale:P,event:n.event,pdv:n.pdv,forma:V,operadorNome:(D=n.link)==null?void 0:D.nome_operador,pdvToken:d,master:!0,integrated:C,eventId:l||void 0,autoPrint:C,onClose:()=>y(null)}),u&&s.jsx(pe,{open:!0,eventId:l,tipoIngressoId:u.tipo.id,ticketName:u.tipo.nome,required:1,onCancel:()=>w(null),onConfirm:W}),s.jsx(ce,{open:!!m,intentId:(m==null?void 0:m.id)||null,forma:(m==null?void 0:m.forma)||"credito",totalLabel:`R$ ${E.toFixed(2)}`,onClose:()=>j(null),onCancel:async()=>
1{if(!m||!d||!l)return;const t=v(d);if(!t)return;const{data:e,error:r}=await g.functions.invoke("pdv-terminal",{body:{action:"cancel",token:d,password:t.password,master:!0,event_id:l,intent_id:m.id}});if(r||!(e!=null&&e.ok)){_.error((e==null?void 0:e.details)||(r==null?void 0:r.message)||"Não foi possÃvel cancelar na maquininha.");return}j(null)},onPollStatus:async()=>{if(!m||!d||!l)return null;const t=v(d);if(!t)return null;const{data:e}=await g.functions.invoke("pdv-terminal",{body:{action:"status",token:d,password:t.password,master:!0,event_id:l,intent_id:m.id}});return e},onApproved:async()=>{if(!m||!d||!l)return;const t=v(d);if(!t)return;const{data:e}=await g.functions.invoke("pdv-terminal",{body:{action:"status",token:d,password:t.password,master:!0,event_id:l,intent_id:m.id}}),r=e==null?void 0:e.pedido,a=m.forma;j(null),f([]),r?(T(a),y(r)):_.success("Pagamento aprovado. Atualizando vendas..."),q()}})]})}export{Ge as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.