PageSourceSearch

https://www.getcorral.com/assets/AdminPage-Bif-o05m.js

js getcorral.com collected 2026-10-04 07:11:27 UTC 35,219 bytes, 21 lines download raw bytes

1import{j as e}from"./vendor-ui-CjDLSmxh.js";import{r as l,h as Le}from"./vendor-react-CQVsrxCm.js";import{c as Y,d as Ae,s as p,t as r,B as c,R as U,S as ue,I as f,X as pe}from"./index-CjcTq_NW.js";import{B as he}from"./badge-BYsp4HOX.js";import{P as ge,U as oe,D as je,g as ce,h as de,i as me,j as xe,G as Xe,I as Pe,J as De,K as qe,m as ze,n as Ee,o as Te,p as Me,q as Oe,w as Ie,X as L,r as Ue,R as fe}from"./Dashboard-Dnzn5Ywu.js";import{T as Re,a as $e,b as G,c as V}from"./tabs-kcC016vW.js";import{L as u}from"./label-VoV2-zg3.js";import{C as ae,a as te,b as ne,c as re}from"./card-DLIBGkpi.js";import{S as Ge}from"./separator-CUNCJJ7G.js";import{S as Ne}from"./switch-CUa9AVXb.js";import{T as Ve}from"./trending-up-DGSW_k-q.js";import{C as K,a as Be}from"./circle-x-DVeHG0kj.js";import{C as ve}from"./copy-Dw-nSzd5.js";import{E as Fe}from"./external-link-CjSkC2h4.js";import{E as He}from"./eye-xEvymwSp.js";import{S as Je}from"./shield-B8tVABIT.js";import{U as _e}from"./user-CRwTreel.js";import{S as be}from"./star-VJ8E3jPe.js";import{C as ye}from"./check-DbORdTZY.js";import"./vendor-charts-Dc140NK5.js";import"./vendor-supabase-DdNJOnXE.js";import"./logo-corral-D8qXYpi7.js";/**
2 * @license lucide-react v0.462.0 - ISC
3 *
4 * This source code is licensed under the ISC license.
5 * See the LICENSE file in the root directory of this source tree.
6 */const we=Y("Instagram",[["rect",{width:"20",height:"20",x:"2",y:"2",rx:"5",ry:"5",key:"2e1cvw"}],["path",{d:"M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z",key:"9exkf1"}],["line",{x1:"17.5",x2:"17.51",y1:"6.5",y2:"6.5",key:"r4j83e"}]]);/**
7 * @license lucide-react v0.462.0 - ISC
8 *
9 * This source code is licensed under the ISC license.
10 * See the LICENSE file in the root directory of this source tree.
11 */const We=Y("MessageSquareQuote",[["path",{d:"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z",key:"1lielz"}],["path",{d:"M8 12a2 2 0 0 0 2-2V8H8",key:"1jfesj"}],["path",{d:"M14 12a2 2 0 0 0 2-2V8h-2",key:"1dq9mh"}]]);/**
12 * @license lucide-react v0.462.0 - ISC
13 *
14 * This source code is licensed under the ISC license.
15 * See the LICENSE file in the root directory of this source tree.
16 */const Qe=Y("MessageSquare",[["path",{d:"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z",key:"1lielz"}]]);/**
17 * @license lucide-react v0.462.0 - ISC
18 *
19 * This source code is licensed under the ISC license.
20 * See the LICENSE file in the root directory of this source tree.
21 */const Ce=Y("Tag",[["path",{d:"M12.586 2.586A2 2 0 0 0 11.172 2H4a2 2 0 0 0-2 2v7.172a2 2 0 0 0 .586 1.414l8.704 8.704a2.426 2.426 0 0 0 3.42 0l6.58-6.58a2.426 2.426 0 0 0 0-3.42z",key:"vktsd0"}],["circle",{cx:"7.5",cy:"7.5",r:".5",fill:"currentColor",key:"kqv944"}]]),Se={prospecto:{label:"Prospecto",variant:"secondary"},contactado:{label:"Contactado",variant:"default"},activo:{label:"Activo",variant:"default"},pausado:{label:"Pausado",variant:"outline"},baja:{label:"Baja",variant:"destructive"}},Ke={prospecto:"bg-gray-100 text-gray-700",contactado:"bg-blue-100 text-blue-700",activo:"bg-green-100 text-green-700",pausado:"bg-yellow-100 text-yellow-700",baja:"bg-red-100 text-red-700"},Ye={pendiente:"bg-yellow-100 text-yellow-700",pagado:"bg-green-100 text-green-700",cancelado:"bg-gray-100 text-gray-500"},ke={nombre:"",instagram_handle:null,youtube_handle:null,tiktok_handle:null,whatsapp:null,email:null,estado:"prospecto",comision_porcentaje:30,descuento_primer_mes:50,stripe_coupon_id:null,codigo_descuento:null,notas:null,perfil_instagram_url:null,tiene_corral_gratis:!1,minimo_posts_mes:4,convenio_firmado:!1,fecha_inicio_contrato:null,fecha_fin_contrato:null};function Ze(){Ae();const[x,B]=l.useState([]),[w,F]=l.useState(!0),[X,C]=l.useState(!1),[d,_]=l.useState(!1),[i,M]=l.useState(null),[P,H]=l.useState([]),[h,S]=l.useState(!1),[n,m]=l.useState({...ke}),[k,v]=l.useState(!1),[b,D]=l.useState(null),[N,J]=l.useState(!1),[A,O]=l.useState(null);l.useEffect(()=>{z()},[]);async function z(){F(!0);const{data:s,error:t}=await p.from("affiliates").select("*").order("created_at",{ascending:!1});t?r({title:"Error al cargar afiliados",description:t.message,variant:"destructive"}):B(s||[]),F(!1)}async function E(s){S(!0);const{data:t,error:y}=await p.from("affiliate_commissions").select("*").eq("affiliate_id",s).order("periodo",{ascending:!1});y||H(t||[]),S(!1)}function R(s){M(s),m({nombre:s.nombre,instagram_handle:s.instagram_handle,youtube_handle:s.youtube_handle,tiktok_handle:s.tiktok_handle,whatsapp:s.whatsapp,email:s.email,estado:s.estado,comision_porcentaje:s.comision_porcentaje,descuento_primer_mes:s.descuento_primer_mes,stripe_coupon_id:s.stripe_coupon_id,codigo_descuento:s.codigo_descuento,notas:s.notas,perfil_instagram_url:s.perfil_instagram_url,tiene_corral_gratis:s.tiene_corral_gratis,minimo_posts_mes:s.minimo_posts_mes,convenio_firmado:s.convenio_firmado,fecha_inicio_contrato:s.fecha_inicio_contrato,fecha_fin_contrato:s.fecha_fin_contrato}),E(s.id),_(!0)}function q(){m({...ke}),C(!0)}async function $(){if(!n.nombre.trim()){r({title:"Nombre requerido",variant:"destructive"});return}v(!0);const s={...n,instagram_handle:n.instagram_handle||null,youtube_handle:n.youtube_handle||null,tiktok_handle:n.tiktok_handle||null,whatsapp:n.whatsapp||null,email:n.email||null,notas:n.notas||null,perfil_instagram_url:n.perfil_instagram_url||null,fecha_inicio_contrato:n.fecha_inicio_contrato||null,fecha_fin_contrato:n.fecha_fin_contrato||null},{error:t}=await p.from("affiliates").insert([s]);t?r({title:"Error al guardar",description:t.message,variant:"destructive"}):(r({title:"Afiliado creado correctamente"}),C(!1),z()),v(!1)}async function Z(){if(!i)return;v(!0);const s={...n,instagram_handle:n.instagram_handle||null,youtube_handle:n.youtube_handle||null,tiktok_handle:n.tiktok_handle||null,whatsapp:n.whatsapp||null,email:n.email||null,notas:n.notas||null,perfil_instagram_url:n.perfil_instagram_url||null,fecha_inicio_contrato:n.fecha_inicio_contrato||null,fecha_fin_contrato:n.fecha_fin_contrato||null},{error:t}=await p.from("affiliates").update(s).eq("id",i.id);t?r({title:"Error al actualizar",description:t.message,variant:"destructive"}):(r({title:"Afiliado actualizado"}),M({...i,...s}),z()),v(!1)}async function W(s){D(s.id);try{const{data:t,error:y}=await p.functions.invoke("create-affiliate-code",{body:{affiliate_id:s.id,nombre:s.nombre,descuento:s.descuento_primer_mes}});if(y)throw new Error(y.message);t!=null&&t.coupon_code&&(await p.from("affiliates").update({stripe_coupon_id:t.stripe_coupon_id,codigo_descuento:t.coupon_code}
21).eq("id",s.id),r({title:"Código creado",description:`Código: ${t.coupon_code}`}),z(),(i==null?void 0:i.id)===s.id&&M(T=>T&&{...T,codigo_descuento:t.coupon_code,stripe_coupon_id:t.stripe_coupon_id}))}catch(t){r({title:"Error al crear código",description:t.message,variant:"destructive"})}D(null)}async function ee(s){const{error:t}=await p.from("affiliate_commissions").update({estado:"pagado",pagado_at:new Date().toISOString()}).eq("id",s);t?r({title:"Error",description:t.message,variant:"destructive"}):(r({title:"Comisión marcada como pagada"}),i&&E(i.id))}async function Q(s){try{const{error:t}=await p.functions.invoke("affiliate-monthly-report",{body:{affiliate_id:s}});if(t)throw new Error(t.message);r({title:"Reporte enviado por WhatsApp"})}catch(t){r({title:"Error al enviar reporte",description:t.message,variant:"destructive"})}}async function se(){J(!0);try{const{error:s}=await p.functions.invoke("affiliate-monthly-report",{body:{run_all:!0}});if(s)throw new Error(s.message);r({title:"Reportes ejecutados correctamente"}),z()}catch(s){r({title:"Error al ejecutar reportes",description:s.message,variant:"destructive"})}J(!1)}function I(s){navigator.clipboard.writeText(s),O(s),setTimeout(()=>O(null),2e3),r({title:"Copiado al portapapeles"})}const a=x.filter(s=>s.estado==="activo").length,o=x.reduce((s,t)=>s+(t.mrr_generado||0),0),j=x.reduce((s,t)=>s+(t.comision_mes||0),0),g=({isEdit:s=!1})=>e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"col-span-2",children:[e.jsx(u,{children:"Nombre *"}),e.jsx(f,{value:n.nombre,onChange:t=>m({...n,nombre:t.target.value}),placeholder:"Nombre del afiliado"})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Instagram"}),e.jsx(f,{value:n.instagram_handle||"",onChange:t=>m({...n,instagram_handle:t.target.value||null}),placeholder:"@usuario"})]}),e.jsxs("div",{children:[e.jsx(u,{children:"TikTok"}),e.jsx(f,{value:n.tiktok_handle||"",onChange:t=>m({...n,tiktok_handle:t.target.value||null}),placeholder:"@usuario"})]}),e.jsxs("div",{children:[e.jsx(u,{children:"YouTube"}),e.jsx(f,{value:n.youtube_handle||"",onChange:t=>m({...n,youtube_handle:t.target.value||null}),placeholder:"@canal"})]}),e.jsxs("div",{children:[e.jsx(u,{children:"WhatsApp"}),e.jsx(f,{value:n.whatsapp||"",onChange:t=>m({...n,whatsapp:t.target.value||null}),placeholder:"52XXXXXXXXXX"})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Email"}),e.jsx(f,{type:"email",value:n.email||"",onChange:t=>m({...n,email:t.target.value||null}),placeholder:"[email protected]"})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Estado"}),e.jsxs(ze,{value:n.estado,onValueChange:t=>m({...n,estado:t}),children:[e.jsx(Ee,{children:e.jsx(Te,{})}),e.jsx(Me,{children:Object.entries(Se).map(([t,y])=>e.jsx(Oe,{value:t,children:y.label},t))})]})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Comisión %"}),e.jsx(f,{type:"number",min:0,max:100,value:n.comision_porcentaje,onChange:t=>m({...n,comision_porcentaje:Number(t.target.value)})})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Descuento primer mes %"}),e.jsx(f,{type:"number",min:0,max:100,value:n.descuento_primer_mes,onChange:t=>m({...n,descuento_primer_mes:Number(t.target.value)})})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Mínimo posts/mes"}),e.jsx(f,{type:"number",min:0,value:n.minimo_posts_mes,onChange:t=>m({...n,minimo_posts_mes:Number(t.target.value)})})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx(u,{children:"URL perfil Instagram"}),e.jsx(f,{value:n.perfil_instagram_url||"",onChange:t=>m({...n,perfil_instagram_url:t.target.value||null}),placeholder:"https://instagram.com/..."})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Inicio contrato"}),e.jsx(f,{type:"date",value:n.fecha_inicio_contrato||"",onChange:t=>m({...n,fecha_inicio_contrato:t.target.value||null})})]}),e.jsxs("div",{children:[e.jsx(u,{children:"Fin contrato"}),e.jsx(f,{type:"date",value:n.fecha_fin_contrato||"",onChange:t=>m({...n,fecha_fin_contrato:t.target.value||null})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ne,{checked:n.tiene_corral_gratis,onCheckedChange:t=>m({...n,tiene_corral_gratis:t})}),e.jsx(u,{children:"CORRAL gratis"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ne,{checked:n.convenio_firmado,onCheckedChange:t=>m({...n,convenio_firmado:t})}),e.jsx(u,{children:"Convenio firmado"})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx(u,{children:"Notas"}),e.jsx(Ie,{value:n.notas||"",onChange:t=>m({...n,notas:t.target.value||null}),placeholder:"Notas internas...",rows:3})]})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(c,{variant:"outline",onClick:()=>s?_(!1):C(!1),children:"Cancelar"}),e.jsx(c,{onClick:s?Z:$,disabled:k,children:k?"Guardando...":s?"Guardar cambios":"Crear afiliado"})]})]});return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h2",{className:"text-xl font-semibold",children:"Afiliados"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(c,{variant:"outline",size:"sm",onClick:se,disabled:N,children:[N?e.jsx(U,{className:"w-4 h-4 animate-spin mr-1"}):e.jsx(ue,{className:"w-4 h-4 mr-1"}),"Ejecutar reportes del mes"]}),e.jsxs(c,{size:"sm",onClick:q,children:[e.jsx(ge,{className:"w-4 h-4 mr-1"})," Nuevo afiliado"]})]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs(ae,{children:[e.jsx(te,{className:"pb-2",children:e.jsxs(ne,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(oe,{className:"w-4 h-4"})," Afiliados activos"]})}),e.jsxs(re,{children:[e.jsx("p",{className:"text-2xl font-bold",children:a}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[x.length," en total"]})]})]}),e.jsxs(ae,{children:[e.jsx(te,{className:"pb-2",children:e.jsxs(ne,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(Ve,{className:"w-4 h-4"})," MRR generado (mes)"]})}),e.jsxs(re,{children:[e.jsxs("p",{className:"text-2xl font-bold",children:["$",o.toLocaleStr
21ing("es-MX")]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"por afiliados activos"})]})]}),e.jsxs(ae,{children:[e.jsx(te,{className:"pb-2",children:e.jsxs(ne,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(je,{className:"w-4 h-4"})," Comisiones pendientes"]})}),e.jsxs(re,{children:[e.jsxs("p",{className:"text-2xl font-bold",children:["$",j.toLocaleString("es-MX")]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"por pagar este mes"})]})]})]}),w?e.jsx("div",{className:"text-center py-12 text-muted-foreground",children:"Cargando afiliados..."}):x.length===0?e.jsxs("div",{className:"text-center py-16 border-2 border-dashed rounded-xl",children:[e.jsx(oe,{className:"w-10 h-10 mx-auto mb-3 text-muted-foreground/50"}),e.jsx("p",{className:"font-medium text-muted-foreground",children:"Sin afiliados registrados"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-4",children:"Agrega influencers y creadores de contenido para tu programa de afiliados."}),e.jsxs(c,{onClick:q,children:[e.jsx(ge,{className:"w-4 h-4 mr-1"})," Nuevo afiliado"]})]}):e.jsx("div",{className:"border rounded-xl overflow-hidden",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-muted/50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left px-4 py-3 font-medium",children:"Afiliado"}),e.jsx("th",{className:"text-left px-4 py-3 font-medium",children:"Código"}),e.jsx("th",{className:"text-left px-4 py-3 font-medium",children:"Instagram"}),e.jsx("th",{className:"text-right px-4 py-3 font-medium",children:"Subs"}),e.jsx("th",{className:"text-right px-4 py-3 font-medium",children:"MRR"}),e.jsx("th",{className:"text-right px-4 py-3 font-medium",children:"Comisión"}),e.jsx("th",{className:"text-center px-4 py-3 font-medium",children:"Convenio"}),e.jsx("th",{className:"text-right px-4 py-3 font-medium",children:"Acciones"})]})}),e.jsx("tbody",{className:"divide-y",children:x.map(s=>e.jsxs("tr",{className:"hover:bg-muted/30 transition-colors",children:[e.jsx("td",{className:"px-4 py-3",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"font-medium",children:s.nombre}),e.jsx("span",{className:`text-xs px-2 py-0.5 rounded-full font-medium ${Ke[s.estado]}`,children:Se[s.estado].label})]})}),e.jsx("td",{className:"px-4 py-3",children:s.codigo_descuento?e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("span",{className:"bg-green-100 text-green-700 text-xs font-mono px-2 py-0.5 rounded",children:s.codigo_descuento}),e.jsx("button",{onClick:()=>I(s.codigo_descuento),className:"text-muted-foreground hover:text-foreground",children:A===s.codigo_descuento?e.jsx(K,{className:"w-3.5 h-3.5 text-green-600"}):e.jsx(ve,{className:"w-3.5 h-3.5"})})]}):e.jsx("span",{className:"text-muted-foreground text-xs",children:"Sin código"})}),e.jsx("td",{className:"px-4 py-3",children:s.instagram_handle?s.perfil_instagram_url?e.jsxs("a",{href:s.perfil_instagram_url,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1 text-blue-600 hover:underline",children:[e.jsx(we,{className:"w-3.5 h-3.5"}),s.instagram_handle,e.jsx(Fe,{className:"w-3 h-3"})]}):e.jsxs("span",{className:"flex items-center gap-1 text-muted-foreground",children:[e.jsx(we,{className:"w-3.5 h-3.5"}),s.instagram_handle]}):e.jsx("span",{className:"text-muted-foreground text-xs",children:"—"})}),e.jsx("td",{className:"px-4 py-3 text-right",children:s.subs_activas??"—"}),e.jsx("td",{className:"px-4 py-3 text-right",children:s.mrr_generado!=null?`$${s.mrr_generado.toLocaleString("es-MX")}`:"—"}),e.jsx("td",{className:"px-4 py-3 text-right",children:s.comision_mes!=null?`$${s.comision_mes.toLocaleString("es-MX")}`:"—"}),e.jsx("td",{className:"px-4 py-3 text-center",children:s.convenio_firmado?e.jsx(K,{className:"w-4 h-4 text-green-600 mx-auto"}):e.jsx(Be,{className:"w-4 h-4 text-red-400 mx-auto"})}),e.jsx("td",{className:"px-4 py-3",children:e.jsxs("div",{className:"flex items-center justify-end gap-1",children:[e.jsx(c,{variant:"ghost",size:"sm",onClick:()=>R(s),children:e.jsx(He,{className:"w-3.5 h-3.5"})}),!s.stripe_coupon_id&&e.jsx(c,{variant:"ghost",size:"sm",onClick:()=>W(s),disabled:b===s.id,title:"Crear código Stripe",children:b===s.id?e.jsx(U,{className:"w-3.5 h-3.5 animate-spin"}):e.jsx(Ce,{className:"w-3.5 h-3.5"})}),s.whatsapp&&e.jsx(c,{variant:"ghost",size:"sm",onClick:()=>Q(s.id),title:"Enviar reporte por WhatsApp",children:e.jsx(Qe,{className:"w-3.5 h-3.5"})})]})})]},s.id))})]})}),e.jsx(ce,{open:X,onOpenChange:C,children:e.jsxs(de,{className:"max-w-lg max-h-[90vh] overflow-y-auto",children:[e.jsx(me,{children:e.jsx(xe,{children:"Nuevo afiliado"})}),e.jsx(g,{})]})}),e.jsx(Xe,{open:d,onOpenChange:_,children:e.jsxs(Pe,{className:"w-full sm:max-w-2xl overflow-y-auto",children:[e.jsx(De,{children:e.jsx(qe,{children:i==null?void 0:i.nombre})}),i&&e.jsxs(Re,{defaultValue:"info",className:"mt-4",children:[e.jsxs($e,{children:[e.jsx(G,{value:"info",children:"Información"}),e.jsx(G,{value:"comisiones",children:"Historial comisiones"})]}),e.jsxs(V,{value:"info",className:"mt-4 space-y-4",children:[i.codigo_descuento?e.jsxs("div",{className:"p-3 bg-green-50 border border-green-200 rounded-lg flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-green-700 font-medium",children:"Código de descuento activo"}),e.jsx("p",{className:"font-mono text-green-800 font-bold",children:i.codigo_descuento})]}),e.jsx(c,{variant:"outline",size:"sm",onClick:()=>
21I(i.codigo_descuento),children:A===i.codigo_descuento?e.jsx(K,{className:"w-4 h-4 text-green-600"}):e.jsx(ve,{className:"w-4 h-4"})})]}):e.jsxs(c,{variant:"outline",className:"w-full",onClick:()=>W(i),disabled:b===i.id,children:[b===i.id?e.jsx(U,{className:"w-4 h-4 mr-2 animate-spin"}):e.jsx(Ce,{className:"w-4 h-4 mr-2"}),"Crear código Stripe (",i.descuento_primer_mes,"% descuento)"]}),i.whatsapp&&e.jsxs(c,{variant:"outline",className:"w-full",onClick:()=>Q(i.id),children:[e.jsx(ue,{className:"w-4 h-4 mr-2"}),"Generar reporte WA del mes"]}),e.jsx(Ge,{}),e.jsx(g,{isEdit:!0})]}),e.jsx(V,{value:"comisiones",className:"mt-4",children:h?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"Cargando..."}):P.length===0?e.jsxs("div",{className:"text-center py-12 text-muted-foreground",children:[e.jsx(je,{className:"w-8 h-8 mx-auto mb-2 opacity-30"}),e.jsx("p",{children:"Sin historial de comisiones"})]}):e.jsx("div",{className:"space-y-2",children:P.map(s=>e.jsxs("div",{className:"border rounded-lg p-3 flex items-center justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.periodo}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[s.suscripciones_activas," subs · MRR $",s.mrr_generado.toLocaleString("es-MX")]})]}),e.jsxs("div",{className:"text-right flex items-center gap-2",children:[e.jsxs("div",{children:[e.jsxs("p",{className:"font-bold",children:["$",s.comision_calculada.toLocaleString("es-MX")]}),e.jsx("span",{className:`text-xs px-2 py-0.5 rounded-full ${Ye[s.estado]}`,children:s.estado})]}),s.estado==="pendiente"&&e.jsxs(c,{size:"sm",variant:"outline",onClick:()=>ee(s.id),children:[e.jsx(K,{className:"w-3.5 h-3.5 mr-1 text-green-600"}),"Marcar pagado"]})]})]},s.id))})})]})]})})]})}const ie="[email protected]",le={free:"bg-gray-100 text-gray-700",basico:"bg-blue-100 text-blue-700",rancho:"bg-green-100 text-green-700",empresario:"bg-purple-100 text-purple-700",maquila:"bg-orange-100 text-orange-700",cortesia:"bg-amber-100 text-amber-700"};function bs(){var I;const{user:x}=Ae(),B=Le(),[w,F]=l.useState([]),[X,C]=l.useState(!0),[d,_]=l.useState(null),[i,M]=l.useState("free"),[P,H]=l.useState(!1),[h,S]=l.useState(null),[n,m]=l.useState(!1),[k,v]=l.useState([]),[b,D]=l.useState(!1),[N,J]=l.useState("pending"),[A,O]=l.useState(null),[z,E]=l.useState(null);l.useEffect(()=>{x&&x.email!==ie&&B("/dashboard",{replace:!0})},[x,B]);const R=async()=>{C(!0);const{data:a,error:o}=await p.rpc("admin_list_users");o?r({title:"Error",description:o.message,variant:"destructive"}):F(a??[]),C(!1)};l.useEffect(()=>{(x==null?void 0:x.email)===ie&&(R(),q())},[x]);const q=async()=>{D(!0);const{data:a,error:o}=await p.from("testimonials").select("*").order("created_at",{ascending:!1});o||v(a??[]),D(!1)},$=async(a,o)=>{O(a);const{error:j}=await p.from("testimonials").update({status:o,...o==="approved"?{approved_at:new Date().toISOString()}:{}}).eq("id",a);if(j){r({title:"Error",description:j.message,variant:"destructive"}),O(null);return}if(v(g=>g.map(s=>s.id===a?{...s,status:o}:s)),o==="approved"){r({title:"Testimonio aprobado ✓",description:"Generando código de descuento…"}),E(a);try{const{data:{session:g}}=await p.auth.getSession(),t=await(await fetch("https://tlwlptawstzcoestqfco.supabase.co/functions/v1/create-promotion-code",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${g==null?void 0:g.access_token}`},body:JSON.stringify({testimonial_id:a})})).json();t.code?(v(y=>y.map(T=>T.id===a?{...T,coupon_code:t.code}:T)),r({title:`Código generado: ${t.code}`,description:"El usuario lo verá en su panel de Facturación."})):r({title:"No se pudo generar el código",description:t.error??"Verifica que el cupón GANADERO15 exista en Stripe.",variant:"destructive"})}catch{r({title:"Error al generar código Stripe",variant:"destructive"})}finally{E(null)}}else r({title:"Testimonio rechazado",description:""});O(null)},Z=async()=>{if(h){m(!0);try{const{data:{session:a}}=await p.auth.getSession(),o=await fetch("https://tlwlptawstzcoestqfco.supabase.co/functions/v1/admin-delete-user",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${a==null?void 0:a.access_token}`},body:JSON.stringify({user_id:h.user_id})}),j=await o.json();o.ok?(r({title:"Usuario eliminado",description:`${h.email} fue eliminado.`}),S(null),R()):r({title:"Error",description:j.error,variant:"destructive"})}catch{r({title:"Error al eliminar",variant:"destructive"})}m(!1)}},W=a=>{_(a),M(a.plan??"free")},ee=async()=>
21{if(!d)return;H(!0);const{error:a}=await p.rpc("admin_update_user_plan",{target_user_id:d.user_id,new_plan:i});a?r({title:"Error",description:a.message,variant:"destructive"}):(r({title:"Plan actualizado",description:`${d.full_name} → ${L[i].label}`}),_(null),R()),H(!1)};if((x==null?void 0:x.email)!==ie)return null;const Q=w.filter(a=>a.plan!=="free").length,se=w.reduce((a,o)=>a+({basico:199,rancho:499,empresario:999,maquila:1499}[o.plan]??0),0);return e.jsxs("div",{className:"space-y-6 max-w-5xl",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Je,{className:"h-5 w-5 text-purple-600"}),e.jsx("h2",{className:"text-lg font-semibold",children:"Panel Admin"}),e.jsx(he,{variant:"outline",className:"text-purple-600 border-purple-300 text-xs",children:"Solo tú"})]}),e.jsxs(c,{variant:"outline",size:"sm",onClick:R,disabled:X,children:[e.jsx(U,{className:`h-3.5 w-3.5 mr-1.5 ${X?"animate-spin":""}`}),"Actualizar"]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"rounded-lg border p-4",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Usuarios totales"}),e.jsx("p",{className:"text-2xl font-bold",children:w.length})]}),e.jsxs("div",{className:"rounded-lg border p-4",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Usuarios de pago"}),e.jsx("p",{className:"text-2xl font-bold text-green-600",children:Q})]}),e.jsxs("div",{className:"rounded-lg border p-4",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"MRR estimado"}),e.jsxs("p",{className:"text-2xl font-bold text-green-600",children:["$",se.toLocaleString("es-MX")," ",e.jsx("span",{className:"text-sm font-normal text-muted-foreground",children:"MXN"})]})]})]}),e.jsxs(Re,{defaultValue:"usuarios",children:[e.jsxs($e,{className:"mb-4",children:[e.jsxs(G,{value:"usuarios",className:"gap-1.5",children:[e.jsx(_e,{className:"h-3.5 w-3.5"}),"Usuarios"]}),e.jsxs(G,{value:"testimonios",className:"gap-1.5",children:[e.jsx(be,{className:"h-3.5 w-3.5"}),"Testimonios"]}),e.jsxs(G,{value:"afiliados",className:"gap-1.5",children:[e.jsx(oe,{className:"h-3.5 w-3.5"}),"Afiliados"]})]}),e.jsx(V,{value:"usuarios",children:e.jsx("div",{className:"rounded-lg border overflow-hidden",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-muted/40",children:[e.jsx("th",{className:"text-left px-4 py-2.5 font-medium text-muted-foreground",children:"Usuario"}),e.jsx("th",{className:"text-left px-4 py-2.5 font-medium text-muted-foreground",children:"Rancho"}),e.jsx("th",{className:"text-left px-4 py-2.5 font-medium text-muted-foreground",children:"Plan"}),e.jsx("th",{className:"text-left px-4 py-2.5 font-medium text-muted-foreground",children:"Stripe"}),e.jsx("th",{className:"text-left px-4 py-2.5 font-medium text-muted-foreground",children:"Registro"}),e.jsx("th",{className:"px-4 py-2.5"})]})}),e.jsx("tbody",{children:X?e.jsx("tr",{children:e.jsx("td",{colSpan:6,className:"text-center py-10 text-muted-foreground text-sm",children:"Cargando usuarios..."})}):w.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:6,className:"text-center py-10 text-muted-foreground text-sm",children:"No hay usuarios registrados aún."})}):w.map(a=>{var o;return e.jsxs("tr",{className:"border-b last:border-0 hover:bg-muted/20 transition-colors",children:[e.jsx("td",{className:"px-4 py-3",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"h-7 w-7 rounded-full bg-sidebar-accent flex items-center justify-center text-xs font-bold shrink-0",children:(a.full_name||a.email||"?").charAt(0).toUpperCase()}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"font-medium truncate max-w-[180px]",children:a.full_name||"Sin nombre"}),e.jsx("p",{className:"text-xs text-muted-foreground truncate max-w-[180px]",children:a.email})]})]})}),e.jsx("td",{className:"px-4 py-3 text-muted-foreground truncate max-w-[140px]",children:a.ranch_n
21ame}),e.jsx("td",{className:"px-4 py-3",children:e.jsx("span",{className:`inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium ${le[a.plan]??le.free}`,children:((o=L[a.plan])==null?void 0:o.label)??a.plan})}),e.jsx("td",{className:"px-4 py-3",children:a.stripe_sub_id?e.jsx("span",{className:"text-xs text-green-600 font-medium",children:a.sub_status==="active"?"✓ Activo":a.sub_status}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"—"})}),e.jsx("td",{className:"px-4 py-3 text-muted-foreground text-xs",children:a.registered_at?new Date(a.registered_at).toLocaleDateString("es-MX",{day:"2-digit",month:"short",year:"numeric"}):"—"}),e.jsx("td",{className:"px-4 py-3 text-right",children:e.jsxs("div",{className:"flex items-center gap-1.5 justify-end",children:[e.jsxs(c,{variant:"outline",size:"sm",className:"h-7 text-xs",onClick:()=>W(a),children:[e.jsx(_e,{className:"h-3 w-3 mr-1"}),"Plan"]}),e.jsx(c,{variant:"outline",size:"sm",className:"h-7 text-xs text-red-600 border-red-200 hover:bg-red-50",onClick:()=>S(a),children:e.jsx(Ue,{className:"h-3 w-3"})})]})})]},a.user_id)})})]})})}),e.jsx(V,{value:"testimonios",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(We,{className:"h-4 w-4 text-amber-600"}),e.jsx("h3",{className:"font-semibold text-sm",children:"Testimonios"}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(",k.filter(a=>a.status==="pending").length," pendientes)"]})]}),e.jsx(c,{variant:"ghost",size:"sm",onClick:q,disabled:b,children:e.jsx(U,{className:`h-3.5 w-3.5 ${b?"animate-spin":""}`})})]}),e.jsx("div",{className:"flex gap-2",children:["pending","approved","rejected"].map(a=>{const o={pending:"Pendientes",approved:"Aprobados",rejected:"Rechazados"},j={pending:N===a?"bg-amber-600 text-white":"bg-amber-50 text-amber-700",approved:N===a?"bg-green-600 text-white":"bg-green-50 text-green-700",rejected:N===a?"bg-red-600 text-white":"bg-red-50 text-red-700"},g=k.filter(s=>s.status===a).length;return e.jsxs("button",{onClick:()=>J(a),className:`text-xs px-3 py-1 rounded-full font-medium transition-colors ${j[a]}`,children:[o[a]," (",g,")"]},a)})}),e.jsx("div",{className:"space-y-3",children:b?e.jsx("p",{className:"text-sm text-muted-foreground py-4 text-center",children:"Cargando testimonios..."}):k.filter(a=>a.status===N).length===0?e.jsxs("p",{className:"text-sm text-muted-foreground py-4 text-center",children:["No hay testimonios ",N==="pending"?"pendientes":N==="approved"?"aprobados":"rechazados","."]}):k.filter(a=>a.status===N).map(a=>e.jsxs("div",{className:"rounded-lg border p-4 space-y-2",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("span",{className:"font-medium text-sm",children:a.name}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"·"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:a.ranch_name}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"·"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:a.location}),e.jsx(he,{variant:"outline",className:"text-xs",children:a.role})]}),e.jsx("div",{className:"flex items-center gap-0.5 mt-0.5",children:Array.from({length:5}).map((o,j)=>e.jsx(be,{className:`h-3 w-3 ${j<a.rating?"fill-amber-400 text-amber-400":"text-gray-200"}`},j))})]}),e.jsx("span",{className:"text-xs text-muted-foreground shrink-0",children:new Date(a.created_at).toLocaleDateString("es-MX",{day:"2-digit",month:"short"})})]}),e.jsxs("p",{className:"text-sm text-muted-foreground italic leading-relaxed",children:['"',a.quote,'"']}),a.status==="pending"&&e.jsxs("div",{className:"flex gap-2 pt-1",children:[e.jsxs(c,{size:"sm",className:"h-7 text-xs bg-green-600 hover:bg-green-700",disabled:A===a.id,onClick:()=>$(a.id,"approved"),children:[e.jsx(ye,{className:"h-3 w-3 mr-1"}),"Aprobar"]}),e.jsxs(c,{size:"sm",variant:"outline",className:"h-7 text-xs text-red-600 border-red-200 hover:bg-red-50",disabled:A===a.id,onClick:()=>$(a.id,"rejected"),children:[e.jsx(pe,{className:"h-3 w-3 mr-1"}),"Rechazar"]})]}),a.status==="approved"&&e.jsxs("div",{className:"flex flex-wrap items-center gap-2 pt-1",children:[z===a.id?e.jsx("span",{className:"text-xs text-muted-foreground animate-pulse",children:"Generando código…"}):a.coupon_code?e.jsxs("span",{className:"inline-flex items-center gap-1.5 rounded-md bg-green-50 border border-green-200 px-2.5 py-1 text-xs font-mono font-semibold text-green-700",children:["🎟️ ",a.coupon_code,e.jsx("button",{className:"ml-1 hover:text-green-900",title:"Copiar",onClick:()=>{navigator.clipboard.writeText(a.coupon_code),r({title:"Código copiado"})},children:"⎘"})]}):e.jsx(c,{size:"sm",variant:"outline",className:"h-7 text-xs border-green-200 text-green-700 hover:bg-green-50",onClick:async()=>{E(a.id);try{const{data:{session:o}}=await p.auth.getSession(),g=await(await fetch("https://tlwlptawstzcoestqfco.supabase.co/functions/v1/create-promotion-code",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${o==null?void 0:o.access_token}`},body:JSON.stringify({testimonial_id:a.id})})).json();g.code?(v(s=>s.map(t=>
21t.id===a.id?{...t,coupon_code:g.code}:t)),r({title:`Código: ${g.code}`})):r({title:"Error",description:g.error,variant:"destructive"})}finally{E(null)}},children:"🎟️ Generar código"}),e.jsxs(c,{size:"sm",variant:"outline",className:"h-7 text-xs text-red-600 border-red-200 hover:bg-red-50",disabled:A===a.id,onClick:()=>$(a.id,"rejected"),children:[e.jsx(pe,{className:"h-3 w-3 mr-1"}),"Quitar aprobación"]})]}),a.status==="rejected"&&e.jsx("div",{className:"flex gap-2 pt-1",children:e.jsxs(c,{size:"sm",className:"h-7 text-xs bg-green-600 hover:bg-green-700",disabled:A===a.id,onClick:()=>$(a.id,"approved"),children:[e.jsx(ye,{className:"h-3 w-3 mr-1"}),"Aprobar"]})})]},a.id))})]})}),e.jsx(V,{value:"afiliados",children:e.jsx(Ze,{})})]}),e.jsx(ce,{open:!!h,onOpenChange:a=>!a&&S(null),children:e.jsxs(de,{className:"max-w-sm",children:[e.jsx(me,{children:e.jsx(xe,{className:"text-red-600",children:"Eliminar usuario"})}),e.jsxs("div",{className:"py-2 space-y-2",children:[e.jsxs("p",{className:"text-sm",children:["¿Eliminar permanentemente a ",e.jsx("strong",{children:(h==null?void 0:h.full_name)||(h==null?void 0:h.email)}),"?"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:h==null?void 0:h.email}),e.jsx("p",{className:"text-xs text-red-600 bg-red-50 rounded px-2 py-1.5",children:"Esta acción es irreversible. Se eliminarán la cuenta y todos sus datos."})]}),e.jsxs(fe,{children:[e.jsx(c,{variant:"outline",size:"sm",onClick:()=>S(null),children:"Cancelar"}),e.jsx(c,{size:"sm",variant:"destructive",onClick:Z,disabled:n,children:n?"Eliminando...":"Sí, eliminar"})]})]})}),e.jsx(ce,{open:!!d,onOpenChange:a=>!a&&_(null),children:e.jsxs(de,{className:"max-w-sm",children:[e.jsx(me,{children:e.jsxs(xe,{children:["Cambiar plan — ",d==null?void 0:d.full_name]})}),e.jsxs("div",{className:"py-2 space-y-3",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Plan actual: ",e.jsx("span",{className:`inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium ml-1 ${le[(d==null?void 0:d.plan)??"free"]}`,children:((I=L[d==null?void 0:d.plan])==null?void 0:I.label)??(d==null?void 0:d.plan)})]}),e.jsxs(ze,{value:i,onValueChange:a=>M(a),children:[e.jsx(Ee,{children:e.jsx(Te,{})}),e.jsx(Me,{children:Object.keys(L).map(a=>e.jsxs(Oe,{value:a,children:[L[a].label," — ",L[a].price]},a))})]}),i!==(d==null?void 0:d.plan)&&e.jsx("p",{className:"text-xs text-amber-600 bg-amber-50 rounded px-2 py-1.5",children:"Esto cambia el plan manualmente sin tocar Stripe. Úsalo solo para pruebas o cortesías."})]}),e.jsxs(fe,{children:[e.jsx(c,{variant:"outline",size:"sm",onClick:()=>_(null),children:"Cancelar"}),e.jsx(c,{size:"sm",onClick:ee,disabled:P||i===(d==null?void 0:d.plan),children:P?"Guardando...":"Guardar"})]})]})})]})}export{bs 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.