1import{j as e,u as he}from"./vendor-query-skQMsM6X.js";import{r as d}from"./vendor-react-DyYvSSk1.js";import{am as se,an as ae,ao as re,ap as le,az as pe,aq as B,I as V,r as te,v as ne,x as ie,y as ce,z as S,as as oe,p as N,c as x,b as y,at as je,aQ as fe,ak as f,aU as K,aV as X,ab as A,aR as Y,X as ge}from"./index-BGwtrXIa.js";import{T as Ne,a as ve,b as F,c as g,d as Ce,e as i}from"./table-DnRDtd-j.js";import{D as be,a as we,b as _e,g as Z,c as Se}from"./dropdown-menu-BXFyvXcT.js";import{U as ye}from"./UserSubscriptionModal-ChNexcSj.js";import{A as Pe,a as Ee}from"./alert-BptgmgmS.js";import{f as de,c as Me}from"./validation-Ckkm_oms.js";import{a as De}from"./errorTranslations-lTGRyFpS.js";import{i as Ue,Y as Ae,aR as Fe,cb as Te,a as Re,O as ke,br as Ie,bg as Le,t as qe}from"./vendor-ui-CCmu0bzQ.js";import{p as ee}from"./pt-BR-0bEujIJO.js";import"./vendor-supabase-BfoV2C50.js";const Oe=["AC","AL","AP","AM","BA","CE","DF","ES","GO","MA","MT","MS","MG","PA","PB","PR","PE","PI","RJ","RN","RS","RO","RR","SC","SP","SE","TO"];function ze({user:t,open:T,onOpenChange:u,onSaved:R}){const[c,P]=d.useState(""),[a,p]=d.useState(""),[E,M]=d.useState(""),[v,o]=d.useState(!1);d.useEffect(()=>{t&&(P(t.crm||""),p(t.crm_uf||""),M(t.cpf||""))},[t]);const D=r=>{const j=de(r);M(j)},k=async()=>{if(!t)return;o(!0);const r=E.replace(/\D/g,"");if(r&&!Me(r)){x.error("CPF inválido. Verifique os dÃgitos."),o(!1);return}if(c&&!a){x.error("Selecione o estado (UF) do CRM"),o(!1);return}try{const{data:j,error:C}=await y.rpc("check_profile_duplicates",{p_cpf:r||null,p_crm:c||null,p_crm_uf:a||null,p_exclude_user_id:t.id});if(C){console.error("Error checking duplicates:",C),x.error("Erro ao verificar duplicatas"),o(!1);return}const h=j;if(h!=null&&h.cpf_exists){x.error("Este CPF já está cadastrado para outro usuário"),o(!1);return}if(h!=null&&h.crm_exists){x.error("Este CRM já está cadastrado neste estado para outro usuário"),o(!1);return}const{error:b}=await y.from("profiles").update({crm:c||null,crm_uf:a||null,cpf:r||null,updated_at:new Date().toISOString()}).eq("id",t.id);if(b){console.error("Error updating profile:",b),x.error(De(b,"Erro ao atualizar perfil")),o(!1);return}x.success("Dados cadastrais atualizados com sucesso"),R(),u(!1)}catch(j){console.error("Error:",j),x.error("Erro inesperado ao salvar")}finally{o(!1)}};return e.jsx(se,{open:T,onOpenChange:u,children:e.jsxs(ae,{className:"max-w-md",children:[e.jsxs(re,{children:[e.jsx(le,{children:"Editar Dados Cadastrais"}),e.jsxs(pe,{children:["Editando: ",(t==null?void 0:t.full_name)||"Usuário sem nome"]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[e.jsxs("div",{className:"space-y-2 col-span-2",children:[e.jsx(B,{children:"CRM (números)"}),e.jsx(V,{value:c,onChange:r=>P(r.target.value.replace(/\D/g,"")),placeholder:"123456",maxLength:7})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(B,{children:"UF"}),e.jsxs(te,{value:a,onValueChange:p,children:[e.jsx(ne,{children:e.jsx(ie,{placeholder:"UF"})}),e.jsx(ce,{children:Oe.map(r=>e.jsx(S,{value:r,children:r},r))})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(B,{children:"CPF"}),e.jsx(V,{value:E,onChange:r=>D(r.target.value),placeholder:"000.000.000-00",maxLength:14})]}),e.jsxs(Pe,{children:[e.jsx(Ue,{className:"h-4 w-4"}),e.jsx(Ee,{className:"text-xs",children:"Alterações em CRM e CPF são campos sensÃveis. Certifique-se de que os dados estão corretos antes de salvar."})]})]}),e.jsxs(oe,{children:[e.jsx(N,{variant:"outline",onClick:()=>u(!1),disabled:v,children:"Cancelar"}),e.jsx(N,{onClick:k,disabled:v,children:v?"Salvando...":"Salvar Alterações"})]})]})})}function es(){var Q;const[t,T]=d.useState(""),[u,R]=d.useState("all"),[c,P]=d.useState(1),[a,p]=d.useState(null),[E,M]=d.useState(null),[v,o]=d.useState(null),D=15,k=async()=>{try{let s=y.from("profiles").select("*",{count:"exact"}).order("created_at",{ascending:!1}).range((c-1)*D,c*D-1);const{data:l,count:n,error:U}=await s;if(U)throw U;const w=(l==null?void 0:l.map(m=>m.id))||[],{data:L}=await y.from("user_ai_balance").select("user_id, balance, plan_type").in("user_id",w),{data:me}=await y.from("user_subscriptions").select("user_id").eq("status","trialing").in("user_id",w),xe=new Set((me||[]).map(m=>m.user_id)),{data:q}=await y.from("user_roles").select("user_id, role").in("user_id",w),W=(l||[]).map(m=>{const _=L==null?void 0:L.find(z=>z.user_id===m.id),O=q==null?void 0:q.find(z=>z.user_id===m.id),J=(_==null?void 0:_.plan_type)||"free",ue=xe.has(m.id);return{...m,ai_balance:(_==null?void 0:_.balance)||0,plan_type:J,subscription_status:ue?"trialing":J==="free"?"none":"active",role:(O==null?void 0:O.role)||"user"}});let $=W;return u!=="all"&&($=W.filter(m=>m.plan_type===u)),{users:$,totalCount:n||0}}catch(s){throw ge.error("Error fetching users:",s),x.error("Erro ao carregar usuários"),s}},{data:r={users:[],totalCount:0},isLoading:j,refetch:C}
1=he({queryKey:["admin-users-list",c,u],queryFn:k,staleTime:6e4,refetchOnWindowFocus:!1}),h=r.users,b=r.totalCount,G=h.filter(s=>{var n,U,w;if(!t)return!0;const l=t.toLowerCase();return((n=s.full_name)==null?void 0:n.toLowerCase().includes(l))||((U=s.specialty)==null?void 0:U.toLowerCase().includes(l))||((w=s.crm)==null?void 0:w.toLowerCase().includes(l))}),H=s=>{const l={premium:{label:"ClÃnica Pro",className:"bg-slate-500/20 text-slate-400 border-slate-500/30"},professional:{label:"Especialista",className:"bg-green-500/20 text-green-400 border-green-500/30"},basic:{label:"Essencial",className:"bg-blue-500/20 text-blue-400 border-blue-500/30"},free:{label:"Free",className:"bg-muted text-muted-foreground"}},n=l[s]||l.free;return e.jsx(A,{variant:"outline",className:n.className,children:n.label})},I=Math.ceil(b/D);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row gap-3",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(Ae,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(V,{placeholder:"Buscar por nome, especialidade ou CRM...",value:t,onChange:s=>T(s.target.value),className:"pl-9"})]}),e.jsxs(te,{value:u,onValueChange:R,children:[e.jsx(ne,{className:"w-[150px]",children:e.jsx(ie,{placeholder:"Plano"})}),e.jsxs(ce,{children:[e.jsx(S,{value:"all",children:"Todos"}),e.jsx(S,{value:"free",children:"Gratuito"}),e.jsx(S,{value:"basic",children:"Essencial"}),e.jsx(S,{value:"professional",children:"Especialista"}),e.jsx(S,{value:"premium",children:"ClÃnica Diagnóstico Pro"})]})]}),e.jsx(N,{variant:"outline",size:"icon",onClick:()=>{C()},children:e.jsx(Fe,{className:"h-4 w-4"})})]}),e.jsx(je,{children:e.jsx(fe,{className:"h-[500px]",children:e.jsxs(Ne,{children:[e.jsx(ve,{children:e.jsxs(F,{children:[e.jsx(g,{children:"Usuário"}),e.jsx(g,{children:"Especialidade"}),e.jsx(g,{children:"Plano"}),e.jsx(g,{className:"text-center",children:"Créditos AI"}),e.jsx(g,{className:"text-center",children:"Role"}),e.jsx(g,{children:"Cadastro"}),e.jsx(g,{className:"w-[50px]"})]})}),e.jsx(Ce,{children:j?[...Array(5)].map((s,l)=>e.jsxs(F,{children:[e.jsx(i,{children:e.jsx(f,{className:"h-8 w-32"})}),e.jsx(i,{children:e.jsx(f,{className:"h-4 w-24"})}),e.jsx(i,{children:e.jsx(f,{className:"h-5 w-16"})}),e.jsx(i,{children:e.jsx(f,{className:"h-4 w-12 mx-auto"})}),e.jsx(i,{children:e.jsx(f,{className:"h-4 w-16 mx-auto"})}),e.jsx(i,{children:e.jsx(f,{className:"h-4 w-20"})}),e.jsx(i,{children:e.jsx(f,{className:"h-8 w-8"})})]},l)):G.length===0?e.jsx(F,{children:e.jsx(i,{colSpan:7,className:"text-center py-8 text-muted-foreground",children:"Nenhum usuário encontrado"})}):G.map(s=>{var l;return e.jsxs(F,{className:"cursor-pointer hover:bg-muted/50",onClick:()=>p(s),children:[e.jsx(i,{children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(K,{className:"h-8 w-8",children:e.jsx(X,{className:"text-xs",children:((l=s.full_name)==null?void 0:l.split(" ").map(n=>n[0]).join("").slice(0,2).toUpperCase())||"??"})}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.full_name||"Sem nome"}),s.crm&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["CRM: ",s.crm]})]})]})}),e.jsx(i,{className:"text-muted-foreground",children:s.specialty||"-"}),e.jsx(i,{children:e.jsxs("div",{className:"flex items-center gap-1.5",children:[H(s.plan_type||"free"),s.subscription_status==="trialing"&&e.jsx(A,{variant:"outline",className:"border-amber-500/40 text-amber-500",children:"Teste"})]})}),e.jsx(i,{className:"text-center",children:e.jsx(A,{variant:"secondary",children:s.ai_balance||0})}),e.jsx(i,{className:"text-center",children:s.role==="admin"?e.jsx(A,{className:"bg-red-500/20 text-red-400",children:"Admin"}):s.role==="moderator"?e.jsx(A,{className:"bg-yellow-500/20 text-yellow-400",children:"Mod"}):e.jsx("span",{className:"text-muted-foreground text-sm",children:"User"})}),e.jsx(i,{className:"text-muted-foreground text-sm",children:s.created_at?Y(new Date(s.created_at),"dd/MM/yy",{locale:ee}):"-"}),e.jsx(i,{children:e.jsxs(be,{children:[e.jsx(we,{asChild:!0,onClick:n=>n.stopPropagation(),children:e.jsx(N,{variant:"ghost",size:"icon",className:"h-8 w-8",children:e.jsx(Te,{className:"h-4 w-4"})})}
1),e.jsxs(_e,{align:"end",children:[e.jsxs(Z,{onClick:n=>{n.stopPropagation(),p(s)},children:[e.jsx(Re,{className:"h-4 w-4 mr-2"})," Ver Detalhes"]}),e.jsx(Se,{}),e.jsxs(Z,{onClick:n=>{n.stopPropagation(),M(s)},children:[e.jsx(ke,{className:"h-4 w-4 mr-2"})," Gerenciar Assinatura"]})]})]})})]},s.id)})})]})})}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:[b," usuários encontrados"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(N,{variant:"outline",size:"sm",onClick:()=>P(s=>Math.max(1,s-1)),disabled:c===1,children:e.jsx(Ie,{className:"h-4 w-4"})}),e.jsxs("span",{className:"text-sm",children:["Página ",c," de ",I||1]}),e.jsx(N,{variant:"outline",size:"sm",onClick:()=>P(s=>Math.min(I,s+1)),disabled:c>=I,children:e.jsx(Le,{className:"h-4 w-4"})})]})]}),e.jsx(se,{open:!!a,onOpenChange:()=>p(null),children:e.jsxs(ae,{className:"max-w-md",children:[e.jsx(re,{children:e.jsx(le,{children:"Detalhes do Usuário"})}),a&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(K,{className:"h-16 w-16",children:e.jsx(X,{className:"text-lg",children:((Q=a.full_name)==null?void 0:Q.split(" ").map(s=>s[0]).join("").slice(0,2).toUpperCase())||"??"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-lg",children:a.full_name||"Sem nome"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:a.specialty||"Especialidade não informada"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3 text-sm",children:[e.jsxs("div",{className:"p-3 rounded-lg bg-muted/50",children:[e.jsx("p",{className:"text-muted-foreground",children:"CRM"}),e.jsx("p",{className:"font-medium",children:a.crm?`${a.crm}-${a.crm_uf||"??"}`:"-"})]}),e.jsxs("div",{className:"p-3 rounded-lg bg-muted/50",children:[e.jsx("p",{className:"text-muted-foreground",children:"CPF"}),e.jsx("p",{className:"font-medium",children:a.cpf?de(a.cpf):"-"})]}),e.jsxs("div",{className:"p-3 rounded-lg bg-muted/50",children:[e.jsx("p",{className:"text-muted-foreground",children:"Instituição"}),e.jsx("p",{className:"font-medium",children:a.institution||"-"})]}),e.jsxs("div",{className:"p-3 rounded-lg bg-muted/50",children:[e.jsx("p",{className:"text-muted-foreground",children:"Plano"}),e.jsx("div",{className:"mt-1",children:H(a.plan_type||"free")})]}),e.jsxs("div",{className:"p-3 rounded-lg bg-muted/50",children:[e.jsx("p",{className:"text-muted-foreground",children:"Créditos AI"}),e.jsx("p",{className:"font-medium",children:a.ai_balance||0})]})]}),e.jsxs("div",{className:"p-3 rounded-lg bg-muted/50 text-sm",children:[e.jsx("p",{className:"text-muted-foreground",children:"Cadastrado em"}),e.jsx("p",{className:"font-medium",children:a.created_at?Y(new Date(a.created_at),"dd 'de' MMMM 'de' yyyy",{locale:ee}):"-"})]}),e.jsx(oe,{children:e.jsxs(N,{variant:"outline",onClick:()=>{p(null),o(a)},children:[e.jsx(qe,{className:"h-4 w-4 mr-2"}),"Editar Dados Cadastrais"]})})]})]})}),e.jsx(ye,{user:E,open:!!E,onOpenChange:s=>!s&&M(null),onSaved:C}),e.jsx(ze,{user:v,open:!!v,onOpenChange:s=>!s&&o(null),onSaved:C})]})}export{es 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.