1import{r,g as F,j as e,L as q,z as K,a as Z,n as l,A as Q}from"./index-DJx_SGrm.js";const W=30,X=300;function B({apiUrl:h,value:i,onChange:$,placeholder:g="Selecione um órgão",emptyOptionLabel:v,disabled:x=!1,id:m,selectedOrgaoNome:I}){const[c,E]=r.useState(!1),[o,n]=r.useState(""),[p,b]=r.useState([]),[j,k]=r.useState(!1),f=r.useRef(null),R=r.useRef(null),u=r.useCallback(async(d="")=>{const y=F();if(!y.Authorization){b([]);return}k(!0);try{const N=new URLSearchParams;N.set("page","1"),N.set("pageSize",String(W)),d.trim()&&N.set("search",d.trim());const z=await fetch(`${h}/admin/orgaos?${N.toString()}`,{headers:y});if(!z.ok){b([]);return}const T=await z.json(),_=Array.isArray(T==null?void 0:T.items)?T.items:[];b(_)}catch{b([])}finally{k(!1)}},[h]);r.useEffect(()=>{if(i===""){c||n("");return}if(I){n(I);return}const d=p.find(y=>y.id===i);d&&n(d.nome)},[i,I,p,c]),r.useEffect(()=>{c&&p.length===0&&!o.trim()&&u("")},[c,u,p.length,o]),r.useEffect(()=>{if(!c){f.current&&(clearTimeout(f.current),f.current=null);return}if(f.current&&(clearTimeout(f.current),f.current=null),!o.trim()){u("");return}return f.current=setTimeout(()=>{f.current=null,u(o)},X),()=>{f.current&&clearTimeout(f.current)}},[c,o,u]);const O=()=>{setTimeout(()=>E(!1),150)},A=(d,y)=>{$(d),n(y??""),E(!1)},P=d=>{n(d.target.value),E(!0)},D=()=>{x||(E(!0),p.length===0&&!o.trim()&&u(""))},U=i===""&&!o?g:null;return e.jsxs("div",{ref:R,className:"relative",children:[e.jsx("input",{id:m,type:"text",value:o,onChange:P,onFocus:D,onBlur:O,placeholder:U??void 0,disabled:x,autoComplete:"off",role:"combobox","aria-expanded":c,"aria-haspopup":"listbox","aria-controls":m?`${m}-listbox`:void 0,className:"mt-1 w-full rounded-md border border-charcoal bg-ink px-3 py-2 text-sm text-canvas placeholder:text-muted [font-family:var(--font-data)] disabled:opacity-50"}),c&&e.jsxs("ul",{id:m?`${m}-listbox`:void 0,role:"listbox",className:"absolute z-10 mt-1 max-h-60 w-full overflow-auto rounded-md border border-charcoal bg-ink py-1 shadow-lg",children:[v&&e.jsx("li",{role:"option","aria-selected":i==="",className:"cursor-pointer px-3 py-2 text-sm text-silver hover:bg-charcoal/50 hover:text-canvas",onMouseDown:d=>{d.preventDefault(),A("",v)},children:v}),j&&e.jsxs("li",{className:"flex items-center gap-2 px-3 py-4 text-sm text-muted",children:[e.jsx(q,{size:"sm"}),"Carregando..."]}),!j&&p.map(d=>{const y=d.id===i;return e.jsx("li",{role:"option","aria-selected":y,className:`cursor-pointer px-3 py-2 text-sm hover:bg-charcoal/50 ${y?"bg-charcoal/30 text-canvas":"text-silver hover:text-canvas"}`,onMouseDown:N=>{N.preventDefault(),A(d.id,d.nome)},children:d.nome},d.id)}),!j&&p.length===0&&!v&&e.jsx("li",{className:"px-3 py-2 text-sm text-muted",children:"Nenhum órgão encontrado."})]})]})}var V;const C=((V=Z.api)==null?void 0:V.baseUrl)||"https://api.bizzu.ai",Y="w-full rounded-[var(--ds-radius-control)] border border-ds-border-subtle bg-ds-panel px-3 py-2 text-sm text-ds-text-primary placeholder:text-ds-text-tertiary [font-family:var(--ds-font-metric)]";function J({label:h,children:i}){return e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-xs font-medium uppercase tracking-wide text-ds-text-tertiary",children:h}),i]})}function ee({item:h,onClose:i,onSaved:$}){const[g,v]=r.useState({nome:h.nome||"",orgaoId:h.orgaoId||""}),[x,m]=r.useState(!1),I=r.useRef(null),c=Q(i);r.useEffect(()=>{const o=n=>{n.key==="Escape"&&i()};return window.addEventListener("keydown",o),()=>window.removeEventListener("keydown",o)},[i]);const E=async o=>{o.preventDefault();const n=g.nome.trim();if(!n){l.error("Informe o nome do cargo.");return}if(!g.orgaoId){l.error("Informe o órgão.");return}const p=F();m(!0);try{const b=await fetch(`${C}/admin/cargos/${h.id}`,{method:"PATCH",headers:{...p,"Content-Type":"application/json"},body:JSON.stringify({nome:n,orgaoId:g.orgaoId})});if(!b.ok){const k=await b.json().catch(()=>({}));l.error(k.message||"Erro ao salvar cargo.");return}const j=await b.json();l.success("Cargo atualizado com sucesso."),$(j),i()}catch{l.error("Falha ao conectar. Tente novamente.")}finally{m(!1)}};return e.jsx("div",{ref:I,className:"fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4 backdrop-blur-sm",...c,children:e.jsxs("div",{className:"w-full max-w-lg rounded-[var(--ds-radius-card)] border border-ds-border bg-ds-panel shadow-[var(--ds-shadow-3)]",children:[e.jsxs("div",{className:"flex items-center justify-between border-b border-ds-border-subtle px-6 py-4",children:[e.jsx("h2",{className:"text-base font-semibold text-ds-text-primary ",children:"Editar cargo"}),e.jsx("button",{type:"button",onClick:i,className:"text-xl leading-none text-ds-text-tertiary hover:text-ds-text-primary",children:"Ã"})]}),e.jsxs("form",{onSubmit:E,className:"space-y-4 px-6 py-5",children:[e.jsx(J,{label:"Nome do cargo",children:e.jsx("input",{type:"text",value:g.nome,onChange:o=>v(n=>({...n,nome:o.target.value})),className:Y,disabled:x,autoFocus:!0})}),e.jsx(J,{label:"Ãrgão",children:e.jsx(B,{apiUrl:C,value:g.orgaoId,onChange:o=>v(n=>({...n,orgaoId:o})),placeholder:"Selecione um órgão",disabled:x})}),e.jsxs("div",{className:"flex justify-end gap-3 pt-2",children:[e.jsx("button",{type:"button",onClick:i,disabled:x,className:"rounded-[var(--ds-radius-control)] border border-ds-border-subtle px-4 py-2 text-sm text-ds-text-secondary hover:text-ds-text-primary",children:"Cancelar"}),e.jsx("button",{type:"submit",disabled:x||!g.nome.trim()||!g.orgaoId,className:"rounded-full shadow-[var(--ds-shadow-accent)] bg-ds-accent px-4 py-2 text-sm font-medium text-white hover:bg-[var(--ds-accent-solid-hover)] disabled:cursor-not-allowed disabled:opacity-50",children:x?"Salvandoâ¦":"Salvar alterações"})]})]})]})})}function re(){var H;const[h,i]=K(),[$,g]=r.useState([]),[v,x]=r.useState([]),[m,I]=r.useState(""),[c,E]=r.useState(""),[o,n]=r.useState([]),[p,b]=r.useState(!0),[j,k]=r.useState(!1),[f,R]=r.useState(null),[u,O]=r.useState({nome:"",orgaoId:""}),[A,P]=r.useState(!1),D=h.get("edit"),U=D?o.find(t=>
1t.id===D)??null:null,G=t=>{const s=new URLSearchParams(h);s.set("edit",t.id),i(s)},d=()=>{const t=new URLSearchParams(h);t.delete("edit"),i(t)},y=r.useCallback(async()=>{const t=F();if(!t.Authorization){b(!1);return}b(!0);try{const s=await fetch(`${C}/editais`,{headers:t});if(!s.ok){g([]);return}const a=await s.json();g(Array.isArray(a)?a:[])}finally{b(!1)}},[]),N=r.useCallback(async()=>{const t=F();if(t.Authorization)try{const s=await fetch(`${C}/admin/orgaos?page=1&pageSize=500`,{headers:t});if(!s.ok){x([]);return}const a=await s.json();Array.isArray(a.items)?x(a.items):Array.isArray(a)?x(a):x([])}catch{x([])}},[]),z=r.useCallback(async(t,s)=>{const a=F();if(a.Authorization){k(!0),R(null);try{const w=new URLSearchParams;t?w.set("editalId",t):s&&w.set("orgaoId",s);const S=await fetch(`${C}/admin/cargos?${w.toString()}`,{headers:a});if(!S.ok){n([]),R("Erro ao carregar cargos.");return}const L=await S.json();n(Array.isArray(L)?L:[])}catch{R("Falha ao conectar. Tente novamente."),n([])}finally{k(!1)}}},[]);r.useEffect(()=>{y(),N()},[y,N]),r.useEffect(()=>{z(m,c)},[m,c,z]);const T=async t=>{t.preventDefault();const s=u.nome.trim(),a=u.orgaoId||null;if(!s){l.error("Informe o nome do cargo.");return}if(!a){l.error("Informe o órgão.");return}const w=F();if(!w.Authorization){l.error("Não autenticado.");return}P(!0);try{const S=await fetch(`${C}/admin/cargos`,{method:"POST",headers:{...w,"Content-Type":"application/json"},body:JSON.stringify({nome:s,orgaoId:a})});if(!S.ok){const L=await S.json().catch(()=>({}));l.error(L.message||"Erro ao criar cargo.");return}l.success("Cargo criado com sucesso."),O({nome:"",orgaoId:""}),await z(m,c)}catch{l.error("Falha ao conectar. Tente novamente.")}finally{P(!1)}},_=async t=>{if(!window.confirm("Tem certeza que deseja excluir este cargo?"))return;const s=F();if(!s.Authorization){l.error("Não autenticado.");return}try{const a=await fetch(`${C}/admin/cargos/${t}`,{method:"DELETE",headers:s});if(!a.ok){const S=await a.json().catch(()=>({}));l.error(S.message||"Erro ao excluir cargo.");return}const w=await a.json().catch(()=>({}));if(w&&w.deleted===!1){l.error("Não foi possÃvel excluir o cargo.");return}n(S=>S.filter(L=>L.id!==t)),l.success("Cargo excluÃdo com sucesso.")}catch{l.error("Falha ao conectar. Tente novamente.")}},M=o.filter(t=>c?t.orgaoId===c:!0);return e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-ds-text-primary ",children:"Cargos"}),e.jsx("p",{className:"mt-2 text-sm text-ds-text-secondary",children:"Gerencie os cargos vinculados aos editais e órgãos."}),e.jsxs("div",{className:"mt-6 grid gap-4 md:grid-cols-[minmax(0,2fr)_minmax(0,1.5fr)]",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"gestao-cargos-edital",className:"mb-1 block text-xs font-medium uppercase tracking-wide text-ds-text-tertiary",children:"Edital"}),e.jsxs("select",{id:"gestao-cargos-edital",value:m,onChange:t=>I(t.target.value),disabled:p,className:"mt-1 w-full rounded-[var(--ds-radius-control)] border border-ds-border bg-ds-surface px-3 py-2 text-sm text-ds-text-primary [font-family:var(--ds-font-metric)]",children:[e.jsx("option",{value:"",children:"Selecione um edital"}),$.map(t=>e.jsx("option",{value:t.id,children:t.nome},t.id))]}),p&&e.jsx("p",{className:"mt-1 text-xs text-ds-text-tertiary",children:"Carregando editais..."})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"gestao-cargos-orgao-filter",className:"mb-1 block text-xs font-medium uppercase tracking-wide text-ds-text-tertiary",children:"Filtrar por órgão"}),e.jsx(B,{id:"gestao-cargos-orgao-filter",apiUrl:C,value:c,onChange:E,placeholder:"Todos os órgãos",emptyOptionLabel:"Todos os órgãos"})]})]}),e.jsxs("form",{onSubmit:T,className:"mt-6 flex flex-wrap items-end gap-3",children:[e.jsxs("div",{className:"min-w-[200px] flex-1",children:[e.jsx("label",{htmlFor:"gestao-cargos-nome",className:"mb-1 block text-xs font-medium uppercase tracking-wide text-ds-text-tertiary",children:"Nome do cargo"}),e.jsx("input",{id:"gestao-cargos-nome",type:"text",value:u.nome,onChange:t=>O(s=>({...s,nome:t.target.value})),placeholder:"Ex.: Analista Judiciário",className:"w-full rounded-[var(--ds-radius-control)] border border-ds-border bg-ds-surface px-3 py-2 text-sm text-ds-text-primary placeholder:text-ds-text-tertiary [font-family:var(--ds-font-metric)]",disabled:A})]}),e.jsxs("div",{className:"min-w-[200px]",children:[e.jsx("label",{htmlFor:"gestao-cargos-orgao",className:"mb-1 block text-xs font-medium uppercase tracking-wide text-ds-text-tertiary",children:"Ãrgão"}),e.jsx(B,{id:"gestao-cargos-orgao",apiUrl:C,value:u.orgaoId,onChange:t=>O(s=>({...s,orgaoId:t})),placeholder:"Selecione um órgão",disabled:A,selectedOrgaoNome:u.orgaoId?(H=v.find(t=>
1t.id===u.orgaoId))==null?void 0:H.nome:void 0})]}),e.jsx("button",{type:"submit",disabled:A||!u.nome.trim()||!u.orgaoId,className:"rounded-[var(--ds-radius-control)] border border-ds-border-subtle bg-ds-panel px-4 py-2 text-sm font-medium text-ds-text-secondary hover:bg-ds-surface-hover hover:text-ds-text-primary disabled:cursor-not-allowed disabled:opacity-50",children:A?"Criando...":"Criar cargo"})]}),j&&e.jsxs("div",{className:"mt-8 flex flex-col items-center gap-3 py-8",children:[e.jsx(q,{size:"lg"}),e.jsx("p",{className:"text-sm text-ds-text-secondary",children:"Carregando cargos..."})]}),!j&&f&&e.jsx("div",{className:"mt-8 rounded-[var(--ds-radius-control)] border border-ds-border-subtle bg-ds-panel px-4 py-3 text-sm text-ds-text-secondary",children:f}),!j&&!f&&e.jsx("div",{className:"mt-8",children:M.length===0?e.jsx("p",{className:"mt-4 text-sm text-ds-text-secondary",children:m?"Nenhum cargo cadastrado para o edital selecionado.":"Nenhum cargo cadastrado. Crie um cargo usando o formulário acima."}):e.jsxs(e.Fragment,{children:[!m&&e.jsx("p",{className:"mb-2 text-sm text-ds-text-secondary",children:"Exibindo todos os cargos. Selecione um edital para filtrar por edital ou use o filtro por órgão."}),e.jsx("div",{className:"mt-2 overflow-x-auto rounded-[var(--ds-radius-control)] border border-ds-border-subtle bg-ds-panel",children:e.jsxs("table",{className:"w-full min-w-[480px] text-left text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-ds-border-subtle",children:[e.jsx("th",{className:"px-4 py-3 font-medium text-ds-text-primary",children:"Nome"}),e.jsx("th",{className:"px-4 py-3 font-medium text-ds-text-primary",children:"Ãrgão"}),e.jsx("th",{className:"px-4 py-3 font-medium text-ds-text-primary",children:"Ações"})]})}),e.jsx("tbody",{children:M.map(t=>{const s=v.find(a=>a.id===t.orgaoId);return e.jsxs("tr",{className:"border-b border-ds-border-subtle last:border-0 hover:bg-ds-surface-hover",children:[e.jsx("td",{className:"px-4 py-3 text-ds-text-secondary",children:t.nome}),e.jsx("td",{className:"px-4 py-3 text-ds-text-secondary",children:s?s.nome:t.orgaoNome??"â"}),e.jsx("td",{className:"px-4 py-3 text-ds-text-secondary",children:e.jsxs("div",{className:"flex gap-2",children:[e.jsx("button",{type:"button",onClick:()=>G(t),className:"text-xs font-medium text-ds-accent-text hover:underline",children:"Editar"}),e.jsx("button",{type:"button",onClick:()=>_(t.id),className:"text-xs font-medium text-ds-danger-text hover:underline",children:"Excluir"})]})})]},t.id)})})]})})]})}),U&&e.jsx(ee,{item:U,onClose:d,onSaved:t=>n(s=>s.map(a=>a.id===t.id?t:a))})]})}export{re as GestaoCargosPage};
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.