1import{r as u,j as e,D as L,g as B,v as $,w as H,O as _,k as P,a8 as G,a1 as K,a2 as X,a3 as Y,a4 as ee,a5 as z,ai as W,B as b,f as w,t as q,ah as U,aa as se,ad as ae,ab as Z,E as ie,u as le,Z as ne,H as ce,n as de}from"./index-DtinW7og.js";import{C as O}from"./card-ZY0Td6MB.js";import{T as me,a as xe,b as M,c as I}from"./tabs-CAMbxCVb.js";import{B as k}from"./badge-CYwYsgea.js";import{u as F}from"./use-toast-DKsFbTKa.js";import{g as ue,e as he}from"./slugUtils-BJw85F3D.js";import{C as te}from"./chevron-up-B9u2W_KD.js";import{C as pe}from"./checkbox-mVGirDjw.js";import{T as ge,a as fe,b as re,c as V,d as je,e as Q}from"./table-DOm-_2pJ.js";import{A as Ne,b as ve,c as be,d as we,e as ye,f as Se,g as De,h as Ce}from"./alert-dialog-DwV0Dvrj.js";import{E as Ee}from"./jspdf.plugin.autotable-CCKlb5LO.js";import{D as _e}from"./download-DyrIafRc.js";import{S as oe}from"./settings-qjOeVFsJ.js";import{L as Te}from"./list-bx160MCU.js";import{H as Oe}from"./help-circle-Dcg8yYUk.js";import{B as Re}from"./bar-chart-3-svfBX_Rz.js";const Pe=({open:p,onClose:j,survey:l,onSuccess:n})=>{const{toast:E}=F(),[s,f]=u.useState({nome:"",descricao:"",url_capa:"",data_limite:"",status:"ativo",slug:""}),[S,r]=u.useState(!1);u.useEffect(()=>{l&&f({nome:l.nome||"",descricao:l.descricao||"",url_capa:l.url_capa||"",data_limite:l.data_limite?l.data_limite.split("T")[0]:"",status:l.status||"ativo",slug:l.slug||""})},[l]);const C=async g=>{if(g.preventDefault(),!!l){r(!0);try{const{data:h}=await w.from("pesquisas").select("slug").neq("id",l.id),D=h?.map(o=>o.slug)||[],y=he(s.slug,D),d={nome:s.nome,descricao:s.descricao||null,url_capa:s.url_capa||null,data_limite:s.data_limite?new Date(s.data_limite).toISOString():null,status:s.status,slug:y},{error:v}=await w.from("pesquisas").update(d).eq("id",l.id);if(v)throw v;E({title:"Pesquisa atualizada!",description:"As alterações foram salvas com sucesso."}),n(),j()}catch(h){console.error("Error updating survey:",h),E({variant:"destructive",title:"Erro ao salvar",description:h.message})}finally{r(!1)}}};return e.jsx(L,{open:p,onOpenChange:j,children:e.jsxs(B,{className:"max-w-2xl max-h-[90vh] overflow-y-auto rounded-3xl",children:[e.jsx($,{children:e.jsx(H,{className:"text-2xl font-black",children:"Editar Detalhes da Pesquisa"})}),e.jsxs("form",{onSubmit:C,className:"space-y-6 pt-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs(_,{htmlFor:"nome",className:"text-base font-bold",children:["Nome da Pesquisa ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(P,{id:"nome",value:s.nome,onChange:g=>f({...s,nome:g.target.value}),placeholder:"Digite o nome da pesquisa",className:"h-12 text-base",required:!0})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(_,{htmlFor:"slug",className:"text-base font-bold",children:["Slug (URL) ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(P,{id:"slug",value:s.slug,onChange:g=>f({...s,slug:ue(g.target.value)}),placeholder:"slug-da-pesquisa",className:"h-12 text-base font-mono",required:!0}),s.slug&&e.jsxs("p",{className:"text-sm text-muted-foreground",children:["URL: ",e.jsxs("span",{className:"font-mono font-semibold",children:["/pesquisas/",s.slug]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{htmlFor:"descricao",className:"text-base font-bold",children:"Descrição"}),e.jsx(G,{id:"descricao",value:s.descricao,onChange:g=>f({...s,descricao:g.target.value}),placeholder:"Descreva o objetivo da pesquisa",className:"min-h-[100px] text-base"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{htmlFor:"url_capa",className:"text-base font-bold",children:"URL da Imagem de Capa"}),e.jsx(P,{id:"url_capa",value:s.url_capa,onChange:g=>f({...s,url_capa:g.target.value}),placeholder:"https://exemplo.com/imagem.jpg",className:"h-12 text-base"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{htmlFor:"data_limite",className:"text-base font-bold",children:"Data Limite"}),e.jsx(P,{id:"data_limite",type:"date",value:s.data_limite,onChange:g=>f({...s,data_limite:g.target.value}),className:"h-12 text-base"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs(_,{htmlFor:"status",className:"text-base font-bold",children:["Status ",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsxs(K,{value:s.status,onValueChange:g=>f({...s,status:g}),children:[e.jsx(X,{className:"h-12 text-base",children:e.jsx(Y,{})}),e.jsxs(ee,{children:[e.jsx(z,{value:"ativo",children:"Ativo"}),e.jsx(z,{value:"inativo",children:"Inativo"}),e.jsx(z,{value:"fechado",children:"Fechado"})]})]})]})]}),e.jsxs(W,{className:"gap-3",children:[e.jsx(b,{type:"button",variant:"outline",onClick:j,disabled:S,className:"rounded-full font-bold",children:"Cancelar"}),e.jsx(b,{type:"submit",disabled:S,className:"rounded-full font-bold",children:S?"Salvando...":"Salvar Alterações"})]})]})]})})},Fe=({pesquisaId:p})=>{const{toast:j}=F(),[l,n]=u.useState([]),[E,s]=u.useState(!0),[f,S]=u.useState(!1),[r,C]=u.useState(null),[g,h]=u.useState({titulo:"",descricao:"",ordem:0});u.useEffect(()=>{p&&D()},[p]);const D=async()=>{try{s(!0);const{data:a,error:i}=await w.from("secoes").select("*").eq("pesquisa_id",p).order("ordem");if(i)throw i;n(a||[])}catch(a){console.error("Error fetching sections:",a),j({variant:"destructive",title:"Erro",description:a.message})}finally{s(!1)}},y=(a=null)=>{a?(C(a),h({titulo:a.titulo,descricao:a.descricao||"",ordem:a.ordem||0})):(C(null),h({titulo:"",descricao:"",ordem:l.length})),S(!0)},d=()=>{S(!1),C(null),h({titulo:"",descricao:"",ordem:0})},v=async a=>{a.preventDefault();try{if(r){const{error:i}=await w.from("secoes").update(g).eq("id",r.id);if(i)throw i;j({title:"Seção atualizada!"})}else{const{error:i}=await w.from("secoes").insert([{...g,pesquisa_id:p}]);if(i)throw i;j({title:"Seção criada!"})}D(),d()}catch(i){console.error("Error saving section:",i),j({variant:"destructive",title:"Erro",description:i.message})}},o=async a=>{if(confirm("Tem certeza? Isso excluirá todas as perguntas desta seção."))try{const{error:i}=await w.from("secoes").delete().eq("id",a);if(i)throw i;j({title:"Seção excluÃda!"}),D()}catch(i){console.error("Error deleting section:",i),j({variant:"destructive",title:"Erro",description:i.message})}},m=async(a,i)=>{const N=a.ordem||0,c=i==="up"?N-1:N+1;try{const{error:T}=await w.from("secoes").update({ordem:c}).eq("id",a.id);if(T)throw T;D()}catch(T){console.error("Error reordering section:",T),j({variant:"destructive",title:"Erro",description:T.message})}};return E?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(q,{className:"w-8 h-8 animate-spin text-primary"})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-xl font-black",children:"Seções da Pesquisa"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Organize sua pesquisa em seções temáticas"})]}),e.jsxs(b,{onClick:()=>y(),className:"rounded-full font-bold",children:[e.jsx(U,{className:"w-4 h-4 mr-2"}),"Adicionar Seção"]})]}),l.length===0?e.jsxs(O,{className:"p-12 text-center border-2 border-dashed",children:[e.jsx("p",{className:"text-muted-foreground mb-4",children:"Nenhuma seção criada ainda"}),e.jsxs(b,{onClick:()=>y(),variant:"outline",className:"rounded-full",children:[e.jsx(U,{className:"w-4 h-4 mr-2"}),"Criar primeira seção"]})]}):e.jsx("div",{className:"space-y-3",children:l.map((a,i)=>e.jsx(O,{className:"p-6 hover:shadow-lg transition-shadow",children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("h4",{className:"text-lg font-bold mb-1",children:a.titulo}),a.descricao&&e.jsx("p",{className:"text-sm text-muted-foreground",children:a.descricao}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-2",children:["Ordem: ",a.ordem||0]})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(b,{size:"sm",variant:"ghost",onClick:()=>m(a,"up"),disabled:i===0,className:"h-9 w-9 p-0",children:e.jsx(te,{className:"w-4 h-4"})}),e.jsx(b,{size:"sm",variant:"ghost",onClick:()=>m(a,"down"),disabled:i===l.length-1,className:"h-9 w-9 p-0",children:e.jsx(se,{className:"w-4 h-4"})}),e.jsx(b,{size:"sm",variant:"ghost",onClick:()=>y(a),className:"h-9 w-9 p-0",children:e.jsx(ae,{className:"w-4 h-4"})}),e.jsx(b,{size:"sm",variant:"ghost",onClick:()=>o(a.id),className:"h-9 w-9 p-0 text-destructive hover:text-destructive",children:e.jsx(Z,{className:"w-4 h-4"})})]})]})},a.id))}),e.jsx(L,{open:f,onOpenChange:d,children:e.jsxs(B,{className:"sm:max-w-xl rounded-3xl",children:[e.jsx($,{children:e.jsx(H,{className:"text-2xl font-black",children:r?"Editar Seção":"Nova Seção"})}),e.jsxs("form",{onSubmit:v,className:"space-y-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{className:"font-bold",children:"TÃtulo da Seção *"}),e.jsx(P,{value:g.titulo,onChange:a=>h({...g,titulo:a.target.value}),placeholder:"Ex: Dados Pessoais",required:!0,className:"h-12 rounded-xl"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{className:"font-bold",children:"Descrição"}
1),e.jsx(G,{value:g.descricao,onChange:a=>h({...g,descricao:a.target.value}),placeholder:"Instruções ou informações adicionais...",rows:3,className:"rounded-xl"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{className:"font-bold",children:"Ordem"}),e.jsx(P,{type:"number",value:g.ordem,onChange:a=>h({...g,ordem:parseInt(a.target.value)||0}),className:"h-12 rounded-xl w-32"})]}),e.jsxs(W,{className:"gap-2",children:[e.jsx(b,{type:"button",variant:"outline",onClick:d,className:"rounded-full font-bold",children:"Cancelar"}),e.jsx(b,{type:"submit",className:"bg-primary hover:bg-primary/90 rounded-full font-bold",children:"Salvar Seção"})]})]})]})})]})},ze=({perguntaId:p,options:j,onUpdate:l})=>{const{toast:n}=F(),[E,s]=u.useState(!1),[f,S]=u.useState(null),[r,C]=u.useState({texto:"",ordem:0}),g=(o=null)=>{o?(S(o),C({texto:o.texto,ordem:o.ordem||0})):(S(null),C({texto:"",ordem:j.length})),s(!0)},h=()=>{s(!1),S(null),C({texto:"",ordem:0})},D=async o=>{o.preventDefault();try{if(f){const{error:m}=await w.from("opcoes").update(r).eq("id",f.id);if(m)throw m;n({title:"Opção atualizada!"})}else{const{error:m}=await w.from("opcoes").insert([{...r,pergunta_id:p}]);if(m)throw m;n({title:"Opção criada!"})}l(),h()}catch(m){console.error("Error saving option:",m),n({variant:"destructive",title:"Erro",description:m.message})}},y=async o=>{if(confirm("Tem certeza que deseja excluir esta opção?"))try{const{error:m}=await w.from("opcoes").delete().eq("id",o);if(m)throw m;n({title:"Opção excluÃda!"}),l()}catch(m){console.error("Error deleting option:",m),n({variant:"destructive",title:"Erro",description:m.message})}},d=async(o,m)=>{const a=o.ordem||0,i=m==="up"?a-1:a+1;try{const{error:N}=await w.from("opcoes").update({ordem:i}).eq("id",o.id);if(N)throw N;l()}catch(N){console.error("Error reordering option:",N),n({variant:"destructive",title:"Erro",description:N.message})}},v=[...j].sort((o,m)=>(o.ordem||0)-(m.ordem||0));return e.jsxs("div",{className:"space-y-3 pl-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(_,{className:"text-sm font-bold text-muted-foreground",children:"Opções de Resposta"}),e.jsxs(b,{type:"button",size:"sm",onClick:()=>g(),className:"h-8 rounded-lg",children:[e.jsx(U,{className:"w-3 h-3 mr-1"}),"Adicionar"]})]}),v.length===0?e.jsx("p",{className:"text-sm text-muted-foreground italic",children:"Nenhuma opção adicionada"}):e.jsx("div",{className:"space-y-2",children:v.map((o,m)=>e.jsx(O,{className:"p-3 bg-muted/50",children:e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx("span",{className:"text-sm font-medium flex-1",children:o.texto}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(b,{type:"button",size:"sm",variant:"ghost",onClick:()=>d(o,"up"),disabled:m===0,className:"h-7 w-7 p-0",children:e.jsx(te,{className:"w-3 h-3"})}),e.jsx(b,{type:"button",size:"sm",variant:"ghost",onClick:()=>d(o,"down"),disabled:m===v.length-1,className:"h-7 w-7 p-0",children:e.jsx(se,{className:"w-3 h-3"})}),e.jsx(b,{type:"button",size:"sm",variant:"ghost",onClick:()=>g(o),className:"h-7 w-7 p-0",children:e.jsx(ae,{className:"w-3 h-3"})}),e.jsx(b,{type:"button",size:"sm",variant:"ghost",onClick:()=>y(o.id),className:"h-7 w-7 p-0 text-destructive hover:text-destructive",children:e.jsx(Z,{className:"w-3 h-3"})})]})]})},o.id))}),e.jsx(L,{open:E,onOpenChange:h,children:e.jsxs(B,{className:"sm:max-w-md rounded-3xl",children:[e.jsx($,{children:e.jsx(H,{className:"text-xl font-black",children:f?"Editar Opção":"Nova Opção"})}),e.jsxs("form",{onSubmit:D,className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{children:"Texto da Opção *"}),e.jsx(P,{value:r.texto,onChange:o=>C({...r,texto:o.target.value}),placeholder:"Digite o texto da opção...",required:!0,className:"h-11 rounded-xl"})]}),e.jsxs(W,{className:"gap-2",children:[e.jsx(b,{type:"button",variant:"outline",onClick:h,className:"rounded-full",children:"Cancelar"}),e.jsx(b,{type:"submit",className:"rounded-full",children:"Salvar"})]})]})]})})]})},ke=({pesquisaId:p})=>{const{toast:j}=F(),[l,n]=u.useState([]),[E,s]=u.useState([]),[f,S]=u.useState([]),[r,C]=u.useState(!0),[g,h]=u.useState(!1),[D,y]=u.useState(null),[d,v]=u.useState({secao_id:"",tipo:"aberta",texto:"",descricao:"",obrigatoria:!1,ordem:0});u.useEffect(()=>{p&&o()},[p]);const o=async()=>{try{C(!0);const[t,x,R]=await Promise.all([w.from("secoes").select("*").eq("pesquisa_id",p).order("ordem"),w.from("perguntas").select("*, secoes!inner(pesquisa_id)").eq("secoes.pesquisa_id",p).order("ordem"),w.from("opcoes").select("*").order("ordem")]);if(t.error)throw t.error;if(x.error)throw x.error;if(R.error)throw R.error;n(t.data||[]),s(x.data||[]),S(R.data||[])}catch(t){console.error("Error fetching data:",t),j({variant:"destructive",title:"Erro",description:t.message})}finally{C(!1)}},m=(t=null)=>{t?(y(t),v({secao_id:t.secao_id,tipo:t.tipo,texto:t.texto,descricao:t.descricao||"",obrigatoria:t.obrigatoria||!1,ordem:t.ordem||0})):(y(null),v({secao_id:l[0]?.id||"",tipo:"aberta",texto:"",descricao:"",obrigatoria:!1,ordem:E.length})),h(!0)},a=()=>{h(!1),y(null)},i=async t=>{t.preventDefault();try{if(D){const{error:x}=await w.from("perguntas").update(d).eq("id",D.id);if(x)throw x;j({title:"Pergunta atualizada!"})}else{const{error:x}=await w.from("perguntas").insert([d]);if(x)throw x;j({title:"Pergunta criada!"})}o(),a()}catch(x){console.error("Error saving question:",x),j({variant:"destructive",title:"Erro",description:x.message})}},N=async t=>{if(confirm("Tem certeza que deseja excluir esta pergunta?"))try{const{error:x}=await w.from("perguntas").delete().eq("id",t);if(x)throw x;j({title:"Pergunta excluÃda!"}),o()}catch(x){console.error("Error deleting question:",x),j({variant:"destructive",title:"Erro",description:x.message})}},c=async(t,x)=>{const R=t.ordem||0,J=x==="up"?R-1:R+1;try{const{error:A}=await w.from("perguntas").update({ordem:J}).eq("id",t.id);if(A)throw A;o()}catch(A){console.error("Error reordering question:",A),j({variant:"destructive",title:"Erro",description:A.message})}};if(r)return e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(q,{className:"w-8 h-8 animate-spin text-primary"})});if(l.length===0)return e.jsx(O,{className:"p-12 text-center border-2 border-dashed",children:e.jsx("p",{className:"text-muted-foreground",children:"Crie seções antes de adicionar perguntas"})});const T=l.map(t=>({...t,questions:E.filter(x=>x.secao_id===t.id)}));return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-xl font-black",children:"Perguntas da Pesquisa"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Gerencie todas as perguntas organizadas por seção"})]}),e.jsxs(b,{onClick:()=>m(),className:"rounded-full font-bold",children:[e.jsx(U,{className:"w-4 h-4 mr-2"}),"Adicionar Pergunta"]})]}),T.map(t=>e.jsxs(O,{className:"p-6",children:[e.jsxs("div",{className:"mb-4",children:[e.jsxs("h4",{className:"text-lg font-bold flex items-center gap-2",children:[t.titulo,e.jsxs(k,{variant:"outline",children:[t.questions.length," perguntas"]})]}),t.descricao&&e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:t.descricao})]}),t.questions.length===0?e.jsx("p",{className:"text-sm text-muted-foreground italic",children:"Nenhuma pergunta nesta seção"}):e.jsx("div",{className:"space-y-3",children:t.questions.map((x,R)=>e.jsx(O,{className:"p-4 bg-muted/30",children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(k,{variant:x.tipo==="aberta"?"default":"secondary",children:x.tipo==="aberta"?"Aberta":"Múltipla Escolha"}),x.obrigatoria&&e.jsx(k,{variant:"destructive",className:"text-xs",children:"Obrigatória"})]}),e.jsx("p",{className:"font-semibold",children:x.texto}),x.descricao&&e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:x.descricao}),x.tipo==="multipla_escolha"&&e.jsx(ze,{perguntaId:x.id,options:f.filter(J=>J.pergunta_id===x.id),onUpdate:o})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(b,{size:"sm",variant:"ghost",onClick:()=>c(x,"up"),disabled:R===0,className:"h-9 w-9 p-0",children:e.jsx(te,{className:"w-4 h-4"})}),e.jsx(b,{size:"sm",variant:"ghost",onClick:()=>c(x,"down"),disabled:R===t.questions.length-1,className:"h-9 w-9 p-0",children:e.jsx(se,{className:"w-4 h-4"})}),e.jsx(b,{size:"sm",variant:"ghost",onClick:()=>m(x),className:"h-9 w-9 p-0",children:e.jsx(ae,{className:"w-4 h-4"})}),e.jsx(b,{size:"sm",variant:"ghost",onClick:()=>N(x.id),className:"h-9 w-9 p-0 text-destructive hover:text-destructive",children:e.jsx(Z,{className:"w-4 h-4"})})]})]})},x.id))})]},t.id)),e.jsx(L,{open:g,onOpenChange:a,children:e.jsxs(B,{className:"sm:max-w-2xl max-h-[90vh] overflow-y-auto rounded-3xl",children:[e.jsx($,{children:e.jsx(H,{className:"text-2xl font-black",children:D?"Editar Pergunta":"Nova Pergunta"})}),e.jsxs("form",{onSubmit:i,className:"space-y-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{className:"font-bold",children:"Seção *"}),e.jsxs(K,{value:d.secao_id,onValueChange:t=>
1v({...d,secao_id:t}),children:[e.jsx(X,{className:"h-12 rounded-xl",children:e.jsx(Y,{})}),e.jsx(ee,{children:l.map(t=>e.jsx(z,{value:t.id,children:t.titulo},t.id))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{className:"font-bold",children:"Tipo de Pergunta *"}),e.jsxs(K,{value:d.tipo,onValueChange:t=>v({...d,tipo:t}),children:[e.jsx(X,{className:"h-12 rounded-xl",children:e.jsx(Y,{})}),e.jsxs(ee,{children:[e.jsx(z,{value:"aberta",children:"Resposta Aberta"}),e.jsx(z,{value:"multipla_escolha",children:"Múltipla Escolha"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{className:"font-bold",children:"Texto da Pergunta *"}),e.jsx(G,{value:d.texto,onChange:t=>v({...d,texto:t.target.value}),placeholder:"Digite a pergunta...",required:!0,rows:3,className:"rounded-xl"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{className:"font-bold",children:"Descrição / Instruções"}),e.jsx(G,{value:d.descricao,onChange:t=>v({...d,descricao:t.target.value}),placeholder:"Informações adicionais (opcional)...",rows:2,className:"rounded-xl"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"flex items-center space-x-3 bg-muted p-4 rounded-xl",children:[e.jsx(pe,{id:"obrigatoria",checked:d.obrigatoria,onCheckedChange:t=>v({...d,obrigatoria:t})}),e.jsx(_,{htmlFor:"obrigatoria",className:"font-bold cursor-pointer",children:"Resposta Obrigatória"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(_,{className:"font-bold",children:"Ordem"}),e.jsx(P,{type:"number",value:d.ordem,onChange:t=>v({...d,ordem:parseInt(t.target.value)||0}),className:"h-12 rounded-xl"})]})]}),e.jsxs(W,{className:"gap-2",children:[e.jsx(b,{type:"button",variant:"outline",onClick:a,className:"rounded-full font-bold",children:"Cancelar"}),e.jsx(b,{type:"submit",className:"bg-primary hover:bg-primary/90 rounded-full font-bold",children:"Salvar Pergunta"})]})]})]})})]})},qe=({open:p,onClose:j,response:l,pesquisaId:n})=>{const{toast:E}=F(),[s,f]=u.useState(!0),[S,r]=u.useState([]),[C,g]=u.useState([]),[h,D]=u.useState([]),[y,d]=u.useState([]);u.useEffect(()=>{p&&l&&v()},[p,l]);const v=async()=>{try{f(!0);const[a,i,N,c]=await Promise.all([w.from("secoes").select("*").eq("pesquisa_id",n).order("ordem"),w.from("perguntas").select("*, secoes!inner(pesquisa_id)").eq("secoes.pesquisa_id",n).order("ordem"),w.from("opcoes").select("*"),w.from("respostas_perguntas").select("*").eq("resposta_id",l.id)]);if(a.error)throw a.error;if(i.error)throw i.error;if(N.error)throw N.error;if(c.error)throw c.error;r(a.data||[]),g(i.data||[]),D(N.data||[]),d(c.data||[])}catch(a){console.error("Error fetching response details:",a),E({variant:"destructive",title:"Erro",description:a.message})}finally{f(!1)}},o=a=>y.find(i=>i.pergunta_id===a),m=(a,i)=>{if(!i)return e.jsx("span",{className:"text-muted-foreground italic",children:"Não respondido"});if(a.tipo==="multipla_escolha"&&i.opcao_id){const N=h.find(c=>c.id===i.opcao_id);return N?N.texto:"Opção não encontrada"}return i.valor_texto||e.jsx("span",{className:"text-muted-foreground italic",children:"Sem resposta"})};return e.jsx(L,{open:p,onOpenChange:j,children:e.jsxs(B,{className:"max-w-4xl max-h-[90vh] overflow-y-auto rounded-3xl",children:[e.jsx($,{children:e.jsx(H,{className:"text-2xl font-black",children:"Detalhes da Resposta"})}),s?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(q,{className:"w-8 h-8 animate-spin text-primary"})}):e.jsxs("div",{className:"space-y-6",children:[e.jsx(O,{className:"p-6 bg-muted/30",children:e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-bold text-muted-foreground mb-1",children:"Respondente"}),e.jsx("p",{className:"font-semibold",children:l.respondente_nome||"Anônimo"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-bold text-muted-foreground mb-1",children:"E-mail"}),e.jsx("p",{className:"font-semibold",children:l.respondente_email||"Não informado"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-bold text-muted-foreground mb-1",children:"Data/Hora"}),e.jsx("p",{className:"font-semibold",children:new Date(l.data_resposta).toLocaleStr
1ing("pt-BR")})]})]})}),S.map(a=>{const i=C.filter(N=>N.secao_id===a.id);return i.length===0?null:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"border-l-4 border-primary pl-4",children:[e.jsx("h3",{className:"text-lg font-black",children:a.titulo}),a.descricao&&e.jsx("p",{className:"text-sm text-muted-foreground",children:a.descricao})]}),e.jsx("div",{className:"space-y-3",children:i.map(N=>{const c=o(N.id);return e.jsx(O,{className:"p-4",children:e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(k,{variant:N.tipo==="aberta"?"default":"secondary",children:N.tipo==="aberta"?"Aberta":"Múltipla Escolha"}),N.obrigatoria&&e.jsx(k,{variant:"destructive",className:"text-xs",children:"Obrigatória"})]}),e.jsx("p",{className:"font-bold",children:N.texto}),N.descricao&&e.jsx("p",{className:"text-sm text-muted-foreground italic",children:N.descricao}),e.jsxs("div",{className:"bg-muted/50 p-3 rounded-lg mt-2",children:[e.jsx("p",{className:"text-sm font-semibold text-muted-foreground mb-1",children:"Resposta:"}),e.jsx("p",{className:"text-base",children:m(N,c)})]})]})},N.id)})})]},a.id)})]})]})})},Ae=async(p,j,l,n,E)=>{const s=new Ee,f=s.internal.pageSize.getWidth(),S=s.internal.pageSize.getHeight();if(s.setFontSize(24),s.setFont(void 0,"bold"),s.text(p.nome,f/2,30,{align:"center"}),s.setFontSize(12),s.setFont(void 0,"normal"),p.descricao){const h=s.splitTextToSize(p.descricao,f-40);s.text(h,f/2,45,{align:"center"})}s.setFontSize(10),s.text(`Total de Respostas: ${j.length}`,f/2,60,{align:"center"}),s.text(`Gerado em: ${new Date().toLocaleDateString("pt-BR")} às ${new Date().toLocaleTimeString("pt-BR")}`,f/2,67,{align:"center"});let r=85;j.forEach((h,D)=>{r>
1S-40&&(s.addPage(),r=20),s.setFontSize(14),s.setFont(void 0,"bold"),s.text(`Resposta #${D+1}`,20,r),r+=8,s.setFontSize(10),s.setFont(void 0,"normal"),s.text(`Respondente: ${h.respondente_nome||"Anônimo"}`,20,r),r+=6,s.text(`E-mail: ${h.respondente_email||"Não informado"}`,20,r),r+=6,s.text(`Data: ${new Date(h.data_resposta).toLocaleString("pt-BR")}`,20,r),r+=10,[...l].sort((d,v)=>(d.ordem||0)-(v.ordem||0)).forEach(d=>{const v=n.filter(o=>o.secao_id===d.id).sort((o,m)=>(o.ordem||0)-(m.ordem||0));if(v.length!==0){if(r>S-60&&(s.addPage(),r=20),s.setFontSize(12),s.setFont(void 0,"bold"),s.setTextColor(0,100,200),s.text(d.titulo,20,r),s.setTextColor(0,0,0),r+=8,d.descricao){s.setFontSize(9),s.setFont(void 0,"italic");const o=s.splitTextToSize(d.descricao,f-40);s.text(o,20,r),r+=o.length*5}r+=5,v.forEach(o=>{const m=h.answers?.find(i=>i.pergunta_id===o.id);r>S-30&&(s.addPage(),r=20),s.setFontSize(10),s.setFont(void 0,"bold");const a=s.splitTextToSize(`${o.texto}${o.obrigatoria?" *":""}`,f-45);if(s.text(a,25,r),r+=a.length*5+3,s.setFont(void 0,"normal"),m){let i="";if(o.tipo==="multipla_escolha"&&m.opcao_id){const c=E.find(T=>T.id===m.opcao_id);i=c?c.texto:"Opção não encontrada"}else m.valor_texto?i=m.valor_texto:i="Sem resposta";const N=s.splitTextToSize(i,f-50);s.setTextColor(60,60,60),s.text(N,30,r),s.setTextColor(0,0,0),r+=N.length*5+5}else s.setTextColor(150,150,150),s.text("Não respondido",30,r),s.setTextColor(0,0,0),r+=8}),r+=5}}),D<j.length-1&&(r+=5,s.setDrawColor(200,200,200),s.line(20,r,f-20,r),r+=15)});const C=s.internal.getNumberOfPages();for(let h=1;h<=C;h++)s.setPage(h),s.setFontSize(9),s.setTextColor(150,150,150),s.text(`Página ${h} de ${C}`,f/2,S-10,{align:"center"}),s.setTextColor(0,0,0);const g=`${p.slug||"pesquisa"}-respostas-${new Date().toISOString().split("T")[0]}.pdf`;s.save(g)},Le=({pesquisaId:p,survey:j})=>{const{toast:l}=F(),[n,E]=u.useState([]),[s,f]=u.useState(!0),[S,r]=u.useState(null),[C,g]=u.useState(!1),[h,D]=u.useState(!1),[y,d]=u.useState(null),[v,o]=u.useState(!1);u.useEffect(()=>{p&&m()},[p]);const m=async()=>{try{f(!0);const{data:c,error:T}=await w.from("respostas").select("*").eq("pesquisa_id",p).order("data_resposta",{ascending:!1});if(T)throw T;const t=await Promise.all((c||[]).map(async x=>{const{data:R}=await w.from("respostas_perguntas").select("*").eq("resposta_id",x.id);return{...x,answers:R||[]}}));E(t)}catch(c){console.error("Error fetching responses:",c),l({variant:"destructive",title:"Erro",description:c.message})}finally{f(!1)}},a=c=>{r(c),g(!0)},i=async()=>{if(y)try{const{error:c}=await w.from("respostas").delete().eq("id",y.id);if(c)throw c;l({title:"Resposta excluÃda!"}),D(!1),d(null),m()}catch(c){console.error("Error deleting response:",c),l({variant:"destructive",title:"Erro",description:c.message})}},N=async()=>{try{o(!0),l({title:"Gerando PDF...",description:"Aguarde enquanto preparamos o arquivo."});const[c,T,t]=await Promise.all([w.from("secoes").select("*").eq("pesquisa_id",p).order("ordem"),w.from("perguntas").select("*, secoes!inner(pesquisa_id)").eq("secoes.pesquisa_id",p).order("ordem"),w.from("opcoes").select("*").order("ordem")]);if(c.error)throw c.error;if(T.error)throw T.error;if(t.error)throw t.error;await Ae(j,n,c.data||[],T.data||[],t.data||[]),l({title:"PDF gerado com sucesso!",description:"O download foi iniciado."})}catch(c){console.error("Error exporting PDF:",c),l({variant:"destructive",title:"Erro ao exportar PDF",description:c.message})}finally{o(!1)}};return s?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(q,{className:"w-8 h-8 animate-spin text-primary"})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-xl font-black",children:"Respostas da Pesquisa"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Total de respostas: ",e.jsx("span",{className:"font-bold",children:n.length})]})]}),e.jsx(b,{onClick:N,disabled:n.length===0||v,className:"rounded-full font-bold",children:v?e.jsxs(e.Fragment,{children:[e.jsx(q,{className:"w-4 h-4 mr-2 animate-spin"}),"Gerando..."]}):e.jsxs(e.Fragment,{children:[e.jsx(_e,{className:"w-4 h-4 mr-2"}),"Exportar PDF"]})})]}),n.length===0?e.jsx(O,{className:"p-12 text-center border-2 border-dashed",children:e.jsx("p",{className:"text-muted-foreground",children:"Nenhuma resposta recebida ainda"})}):e.jsx(O,{className:"overflow-hidden rounded-2xl",children:e.jsxs(ge,{children:[e.jsx(fe,{children:e.jsxs(re,{className:"bg-muted/50",children:[e.jsx(V,{className:"font-bold",children:"Respondente"}),e.jsx(V,{className:"font-bold",children:"E-mail"}),e.jsx(V,{className:"font-bold",children:"Data/Hora"}),e.jsx(V,{className:"text-right font-bold",children:"Ações"})]})}),e.jsx(je,{children:n.map(c=>e.jsxs(re,{className:"hover:bg-muted/30",children:[e.jsx(Q,{className:"font-semibold",children:c.respondente_nome||"Anônimo"}),e.jsx(Q,{className:"text-muted-foreground",children:c.respondente_email||"Não informado"}),e.jsx(Q,{className:"text-muted-foreground",children:new Date(c.data_resposta).toLocaleStr
1ing("pt-BR")}),e.jsx(Q,{className:"text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[e.jsxs(b,{size:"sm",variant:"ghost",onClick:()=>a(c),className:"h-9 rounded-lg hover:bg-primary/10 hover:text-primary",children:[e.jsx(ie,{className:"w-4 h-4 mr-1"}),"Ver Detalhes"]}),e.jsx(b,{size:"sm",variant:"ghost",onClick:()=>{d(c),D(!0)},className:"h-9 rounded-lg hover:bg-destructive/10 hover:text-destructive",children:e.jsx(Z,{className:"w-4 h-4"})})]})})]},c.id))})]})}),S&&e.jsx(qe,{open:C,onClose:()=>{g(!1),r(null)},response:S,pesquisaId:p}),e.jsx(Ne,{open:h,onOpenChange:D,children:e.jsxs(ve,{children:[e.jsxs(be,{children:[e.jsx(we,{children:"Confirmar Exclusão"}),e.jsx(ye,{children:"Tem certeza que deseja excluir esta resposta? Esta ação não pode ser desfeita."})]}),e.jsxs(Se,{children:[e.jsx(De,{className:"rounded-full",children:"Cancelar"}),e.jsx(Ce,{onClick:i,className:"rounded-full bg-destructive hover:bg-destructive/90",children:"Excluir"})]})]})})]})},ss=()=>{const{id:p}=le(),j=ne(),{toast:l}=F(),[n,E]=u.useState(null),[s,f]=u.useState(!0),[S,r]=u.useState(!1),[C,g]=u.useState("details");u.useEffect(()=>{p&&h()},[p]);const h=async()=>{try{f(!0);const{data:y,error:d}=await w.from("pesquisas").select("*").eq("id",p).single();if(d)throw d;if(!y){l({variant:"destructive",title:"Pesquisa não encontrada"}),j("/admin");return}E(y)}catch(y){console.error("Error fetching survey:",y),l({variant:"destructive",title:"Erro",description:y.message}),j("/admin")}finally{f(!1)}},D=y=>{const d={ativo:{className:"bg-green-500/10 text-green-700 border-green-200",label:"Ativo"},inativo:{className:"bg-gray-500/10 text-gray-700 border-gray-200",label:"Inativo"},fechado:{className:"bg-red-500/10 text-red-700 border-red-200",label:"Fechado"}},{className:v,label:o}=d[y]||d.inativo;return e.jsx(k,{className:v,children:o})};return s?e.jsx("div",{className:"flex min-h-screen items-center justify-center bg-muted",children:e.jsx(q,{className:"w-12 h-12 animate-spin text-primary"})}
1):n?e.jsxs("div",{className:"min-h-screen bg-muted",children:[e.jsx(ce,{children:e.jsxs("title",{children:["Editar Pesquisa: ",n.nome," | Admin MOV"]})}),e.jsxs("div",{className:"max-w-7xl mx-auto p-6 lg:p-10 space-y-6",children:[e.jsx("div",{className:"flex items-center gap-4",children:e.jsxs(b,{variant:"ghost",size:"sm",onClick:()=>j("/admin"),className:"rounded-full",children:[e.jsx(de,{className:"w-4 h-4 mr-2"}),"Voltar"]})}),e.jsx(O,{className:"p-8 bg-gradient-to-br from-primary/5 to-primary/10 border-primary/20",children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-2",children:[e.jsx("h1",{className:"text-3xl font-black text-foreground",children:n.nome}),D(n.status)]}),n.descricao&&e.jsx("p",{className:"text-muted-foreground mb-3",children:n.descricao}),e.jsxs("div",{className:"flex items-center gap-4 text-sm text-muted-foreground",children:[e.jsxs("span",{className:"font-mono",children:["/",n.slug]}),n.data_limite&&e.jsxs("span",{children:["Prazo: ",new Date(n.data_limite).toLocaleDateString("pt-BR")]})]})]}),e.jsxs(b,{onClick:()=>r(!0),className:"rounded-full font-bold",children:[e.jsx(oe,{className:"w-4 h-4 mr-2"}),"Editar Detalhes"]})]})}),e.jsxs(me,{value:C,onValueChange:g,className:"space-y-6",children:[e.jsxs(xe,{className:"grid w-full grid-cols-4 h-14 rounded-2xl bg-card p-1",children:[e.jsxs(M,{value:"details",className:"rounded-xl font-bold data-[state=active]:bg-primary data-[state=active]:text-primary-foreground",children:[e.jsx(oe,{className:"w-4 h-4 mr-2"}),"Detalhes"]}),e.jsxs(M,{value:"sections",className:"rounded-xl font-bold data-[state=active]:bg-primary data-[state=active]:text-primary-foreground",children:[e.jsx(Te,{className:"w-4 h-4 mr-2"}),"Seções"]}),e.jsxs(M,{value:"questions",className:"rounded-xl font-bold data-[state=active]:bg-primary data-[state=active]:text-primary-foreground",children:[e.jsx(Oe,{className:"w-4 h-4 mr-2"}),"Perguntas"]}),e.jsxs(M,{value:"responses",className:"rounded-xl font-bold data-[state=active]:bg-primary data-[state=active]:text-primary-foreground",children:[e.jsx(Re,{className:"w-4 h-4 mr-2"}),"Respostas"]})]}),e.jsx(I,{value:"details",className:"space-y-6",children:e.jsxs(O,{className:"p-8",children:[e.jsx("h3",{className:"text-xl font-black mb-4",children:"Informações da Pesquisa"}),e.jsxs("div",{className:"grid grid-cols-2 gap-6",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-bold text-muted-foreground mb-1",children:"Nome"}),e.jsx("p",{className:"font-semibold",children:n.nome})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-bold text-muted-foreground mb-1",children:"Status"}),D(n.status)]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-bold text-muted-foreground mb-1",children:"Slug"}),e.jsx("p",{className:"font-mono text-sm",children:n.slug})]}),n.data_limite&&e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-bold text-muted-foreground mb-1",children:"Data Limite"}),e.jsx("p",{className:"font-semibold",children:new Date(n.data_limite).toLocaleDateString("pt-BR")})]}),n.descricao&&e.jsxs("div",{className:"col-span-2",children:[e.jsx("p",{className:"text-sm font-bold text-muted-foreground mb-1",children:"Descrição"}),e.jsx("p",{className:"text-muted-foreground",children:n.descricao})]})]})]})}),e.jsx(I,{value:"sections",children:e.jsx(Fe,{pesquisaId:p})}),e.jsx(I,{value:"questions",children:e.jsx(ke,{pesquisaId:p})}),e.jsx(I,{value:"responses",children:e.jsx(Le,{pesquisaId:p,survey:n})})]})]}),e.jsx(Pe,{open:S,onClose:()=>r(!1),survey:n,onSuccess:h})]}):null};export{ss 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.