Files
sgse-app/apps/web/src/routes/(dashboard)/recursos-humanos/funcionarios/+page.svelte
killer-cf 6128c20da0 feat: implement sub-steps management in workflow editor
- Added functionality for creating, updating, and deleting sub-steps within the workflow editor.
- Introduced a modal for adding new sub-steps, including fields for name and description.
- Enhanced the UI to display sub-steps with status indicators and options for updating their status.
- Updated navigation links to reflect changes in the workflow structure, ensuring consistency across the application.
- Refactored related components to accommodate the new sub-steps feature, improving overall workflow management.
2025-11-25 14:14:43 -03:00

520 lines
17 KiB
Svelte

<script lang="ts">
import { useConvexClient, useQuery } from 'convex-svelte';
import { api } from '@sgse-app/backend/convex/_generated/api';
import { goto } from '$app/navigation';
import { resolve } from '$app/paths';
import type { SimboloTipo } from '@sgse-app/backend/convex/schema';
import type { Id } from '@sgse-app/backend/convex/_generated/dataModel';
import PrintModal from '$lib/components/PrintModal.svelte';
const client = useConvexClient();
// Estado reativo
let list = $state<Array<{
_id: Id<'funcionarios'>;
nome: string;
matricula?: string;
cpf: string;
cidade?: string;
uf?: string;
simboloTipo?: SimboloTipo;
}>>([]);
let filtered = $state<typeof list>([]);
let openMenuId = $state<string | null>(null);
let funcionarioParaImprimir = $state<unknown>(null);
// Estado do modal de setores
let showSetoresModal = $state(false);
let funcionarioParaSetores = $state<{ _id: Id<'funcionarios'>; nome: string } | null>(null);
let setoresSelecionados = $state<Id<'setores'>[]>([]);
let isSavingSetores = $state(false);
let setoresError = $state<string | null>(null);
// Queries
const todosSetoresQuery = useQuery(api.setores.list, {});
let filtroNome = $state('');
let filtroCPF = $state('');
let filtroMatricula = $state('');
let filtroTipo = $state<SimboloTipo | ''>('');
function applyFilters() {
const nome = filtroNome.toLowerCase();
const cpf = filtroCPF.replace(/\D/g, '');
const mat = filtroMatricula.toLowerCase();
filtered = list.filter((f) => {
const okNome = !nome || (f.nome || '').toLowerCase().includes(nome);
const okCPF = !cpf || (f.cpf || '').includes(cpf);
const okMat = !mat || (f.matricula || '').toLowerCase().includes(mat);
const okTipo = !filtroTipo || f.simboloTipo === filtroTipo;
return okNome && okCPF && okMat && okTipo;
});
}
async function load() {
const data = await client.query(api.funcionarios.getAll, {});
list = data ?? [];
applyFilters();
}
async function openPrintModal(funcionarioId: string) {
try {
const data = await client.query(api.funcionarios.getFichaCompleta, {
id: funcionarioId as Id<'funcionarios'>
});
funcionarioParaImprimir = data;
} catch (err) {
console.error('Erro ao carregar funcionário:', err);
alert('Erro ao carregar dados para impressão');
}
}
function navCadastro() {
goto(resolve('/recursos-humanos/funcionarios/cadastro'));
}
load();
function toggleMenu(id: string) {
openMenuId = openMenuId === id ? null : id;
}
async function openSetoresModal(funcionarioId: Id<'funcionarios'>, nome: string) {
funcionarioParaSetores = { _id: funcionarioId, nome };
setoresSelecionados = [];
setoresError = null;
showSetoresModal = true;
openMenuId = null;
// Carregar setores do funcionário
try {
const setores = await client.query(api.setores.getSetoresByFuncionario, {
funcionarioId
});
setoresSelecionados = setores.map((s) => s._id);
} catch (err) {
console.error('Erro ao carregar setores do funcionário:', err);
setoresError = 'Erro ao carregar setores do funcionário';
}
}
function closeSetoresModal() {
showSetoresModal = false;
funcionarioParaSetores = null;
setoresSelecionados = [];
setoresError = null;
}
function toggleSetor(setorId: Id<'setores'>) {
if (setoresSelecionados.includes(setorId)) {
setoresSelecionados = setoresSelecionados.filter((id) => id !== setorId);
} else {
setoresSelecionados = [...setoresSelecionados, setorId];
}
}
async function salvarSetores() {
if (!funcionarioParaSetores) return;
isSavingSetores = true;
setoresError = null;
try {
await client.mutation(api.setores.atualizarSetoresFuncionario, {
funcionarioId: funcionarioParaSetores._id,
setorIds: setoresSelecionados
});
closeSetoresModal();
} catch (err) {
setoresError = err instanceof Error ? err.message : 'Erro ao salvar setores';
} finally {
isSavingSetores = false;
}
}
</script>
<main class="container mx-auto px-4 py-4 max-w-7xl flex flex-col" style="height: calc(100vh - 8rem); min-height: 600px;">
<!-- Breadcrumb -->
<div class="breadcrumbs mb-4 text-sm shrink-0">
<ul>
<li><a href={resolve('/recursos-humanos')} class="text-primary hover:underline">Recursos Humanos</a></li>
<li>Funcionários</li>
</ul>
</div>
<!-- Cabeçalho -->
<div class="mb-6 shrink-0">
<div class="flex flex-col items-start justify-between gap-4 sm:flex-row sm:items-center">
<div class="flex items-center gap-4">
<div class="rounded-xl bg-blue-500/20 p-3">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-8 w-8 text-blue-600"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"
/>
</svg>
</div>
<div>
<h1 class="text-primary text-3xl font-bold">Funcionários Cadastrados</h1>
<p class="text-base-content/70">Gerencie os funcionários da secretaria</p>
</div>
</div>
<button class="btn btn-primary btn-lg gap-2 shadow-md hover:shadow-lg transition-all" onclick={navCadastro}>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z"
clip-rule="evenodd"
/>
</svg>
Novo Funcionário
</button>
</div>
</div>
<!-- Filtros -->
<div class="card bg-base-100/90 backdrop-blur-sm border border-base-300 mb-4 shadow-xl shrink-0">
<div class="card-body">
<h2 class="card-title mb-4 text-lg">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M3 4a1 1 0 011-1h16a1 1 0 011 1v2.586a1 1 0 01-.293.707l-6.414 6.414a1 1 0 00-.293.707V17l-4 4v-6.586a1 1 0 00-.293-.707L3.293 7.293A1 1 0 013 6.586V4z"
/>
</svg>
Filtros de Pesquisa
</h2>
<div class="grid grid-cols-1 items-end gap-4 md:grid-cols-4">
<div class="form-control w-full">
<label class="label" for="func_nome">
<span class="label-text font-semibold">Nome</span>
</label>
<input
id="func_nome"
class="input input-bordered focus:input-primary w-full"
placeholder="Buscar por nome..."
bind:value={filtroNome}
oninput={applyFilters}
/>
</div>
<div class="form-control w-full">
<label class="label" for="func_cpf">
<span class="label-text font-semibold">CPF</span>
</label>
<input
id="func_cpf"
class="input input-bordered focus:input-primary w-full"
placeholder="000.000.000-00"
bind:value={filtroCPF}
oninput={applyFilters}
/>
</div>
<div class="form-control w-full">
<label class="label" for="func_matricula">
<span class="label-text font-semibold">Matrícula</span>
</label>
<input
id="func_matricula"
class="input input-bordered focus:input-primary w-full"
placeholder="Buscar por matrícula..."
bind:value={filtroMatricula}
oninput={applyFilters}
/>
</div>
<div class="form-control w-full">
<label class="label" for="func_tipo">
<span class="label-text font-semibold">Símbolo Tipo</span>
</label>
<select
id="func_tipo"
class="select select-bordered focus:select-primary w-full"
bind:value={filtroTipo}
oninput={applyFilters}
>
<option value="">Todos os tipos</option>
<option value="cargo_comissionado">Cargo Comissionado</option>
<option value="funcao_gratificada">Função Gratificada</option>
</select>
</div>
</div>
{#if filtroNome || filtroCPF || filtroMatricula || filtroTipo}
<div class="mt-4">
<button
class="btn btn-sm gap-2"
onclick={() => {
filtroNome = '';
filtroCPF = '';
filtroMatricula = '';
filtroTipo = '';
applyFilters();
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
Limpar Filtros
</button>
</div>
{/if}
</div>
</div>
<!-- Container da Tabela com altura responsiva -->
<div class="flex-1 flex flex-col min-h-0">
<!-- Tabela de Funcionários -->
<div class="card bg-base-100/90 backdrop-blur-sm border border-base-300 shadow-xl flex-1 flex flex-col min-h-0">
<div class="card-body p-0 flex-1 flex flex-col min-h-0">
<!-- Container com scroll -->
<div class="flex-1 overflow-hidden flex flex-col">
<div class="overflow-x-auto flex-1 overflow-y-auto">
<table class="table table-zebra w-full">
<thead class="sticky top-0 z-10 shadow-md bg-linear-to-r from-base-300 to-base-200">
<tr>
<th class="whitespace-nowrap font-bold text-base-content border-b border-base-400">Nome</th>
<th class="whitespace-nowrap font-bold text-base-content border-b border-base-400">CPF</th>
<th class="whitespace-nowrap font-bold text-base-content border-b border-base-400">Matrícula</th>
<th class="whitespace-nowrap font-bold text-base-content border-b border-base-400">Tipo</th>
<th class="whitespace-nowrap font-bold text-base-content border-b border-base-400">Cidade</th>
<th class="whitespace-nowrap font-bold text-base-content border-b border-base-400">UF</th>
<th class="text-right whitespace-nowrap font-bold text-base-content border-b border-base-400">Ações</th>
</tr>
</thead>
<tbody>
{#if filtered.length === 0}
<tr>
<td colspan="7" class="text-center py-12">
<div class="flex flex-col items-center justify-center gap-4">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-16 w-16 text-base-content/30"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="1.5"
d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"
/>
</svg>
<div class="text-base-content/60 text-center">
<p class="font-semibold text-lg mb-1">Nenhum funcionário encontrado</p>
<p class="text-sm">
{#if filtroNome || filtroCPF || filtroMatricula || filtroTipo}
Tente ajustar os filtros ou
{/if}
<button class="btn btn-link btn-sm p-0 h-auto min-h-0" onclick={navCadastro}>
cadastre um novo funcionário
</button>
</p>
</div>
</div>
</td>
</tr>
{:else}
{#each filtered as f (f._id)}
<tr class="hover:bg-base-200/50 transition-colors">
<td class="whitespace-nowrap font-medium">{f.nome}</td>
<td class="whitespace-nowrap">{f.cpf}</td>
<td class="whitespace-nowrap">{f.matricula}</td>
<td class="whitespace-nowrap">
<span class="badge badge-outline badge-sm">
{f.simboloTipo === 'cargo_comissionado' ? 'Cargo Comissionado' :
f.simboloTipo === 'funcao_gratificada' ? 'Função Gratificada' :
f.simboloTipo || '-'}
</span>
</td>
<td class="whitespace-nowrap">{f.cidade || '-'}</td>
<td class="whitespace-nowrap">{f.uf || '-'}</td>
<td class="text-right whitespace-nowrap">
<div class="dropdown dropdown-end" class:dropdown-open={openMenuId === f._id}>
<button
type="button"
aria-label="Abrir menu"
class="btn btn-sm btn-ghost hover:btn-primary transition-all"
onclick={() => toggleMenu(f._id)}
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
d="M10 6a2 2 0 110-4 2 2 0 010 4zM10 12a2 2 0 110-4 2 2 0 010 4zM10 18a2 2 0 110-4 2 2 0 010 4z"
/>
</svg>
</button>
<ul
class="dropdown-content menu bg-base-100 rounded-box border-base-300 z-20 w-52 border p-2 shadow-xl"
>
<li>
<a href={resolve(`/recursos-humanos/funcionarios/${f._id}`)} class="hover:bg-primary/10">
Ver Detalhes
</a>
</li>
<li>
<a href={resolve(`/recursos-humanos/funcionarios/${f._id}/editar`)} class="hover:bg-primary/10">
Editar
</a>
</li>
<li>
<a href={resolve(`/recursos-humanos/funcionarios/${f._id}/documentos`)} class="hover:bg-primary/10">
Ver Documentos
</a>
</li>
<li>
<button
onclick={() => openSetoresModal(f._id, f.nome)}
class="hover:bg-primary/10"
>
Atribuir Setores
</button>
</li>
<li>
<button onclick={() => openPrintModal(f._id)} class="hover:bg-primary/10">
Imprimir Ficha
</button>
</li>
</ul>
</div>
</td>
</tr>
{/each}
{/if}
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Informação sobre resultados -->
<div class="text-base-content/70 mt-3 text-center text-sm shrink-0 border-t border-base-300 pt-3 bg-base-100/50">
Exibindo <span class="font-semibold">{filtered.length}</span> de <span class="font-semibold">{list.length}</span> funcionário(s)
</div>
</div>
<!-- Modal de Impressão -->
{#if funcionarioParaImprimir}
<PrintModal
funcionario={funcionarioParaImprimir}
onClose={() => (funcionarioParaImprimir = null)}
/>
{/if}
<!-- Modal de Atribuição de Setores -->
{#if showSetoresModal && funcionarioParaSetores}
<div class="modal modal-open">
<div class="modal-box max-w-2xl">
<h3 class="text-lg font-bold">Atribuir Setores</h3>
<p class="text-base-content/60 mt-2">
Selecione os setores para <strong>{funcionarioParaSetores.nome}</strong>
</p>
{#if setoresError}
<div class="alert alert-error mt-4">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-6 w-6 shrink-0 stroke-current"
fill="none"
viewBox="0 0 24 24"
aria-hidden="true"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
<span>{setoresError}</span>
</div>
{/if}
<div class="mt-4 max-h-96 overflow-y-auto">
{#if todosSetoresQuery.isLoading}
<div class="flex items-center justify-center py-8">
<span class="loading loading-spinner loading-lg"></span>
</div>
{:else if todosSetoresQuery.data && todosSetoresQuery.data.length > 0}
<div class="space-y-2">
{#each todosSetoresQuery.data as setor (setor._id)}
{@const isSelected = setoresSelecionados.includes(setor._id)}
<label class="flex cursor-pointer items-center gap-3 rounded-lg border p-3 hover:bg-base-200 {isSelected ? 'border-primary bg-primary/5' : 'border-base-300'}">
<input
type="checkbox"
class="checkbox checkbox-primary"
checked={isSelected}
onchange={() => toggleSetor(setor._id)}
aria-label="Selecionar setor {setor.nome}"
/>
<div class="flex-1">
<div class="font-medium">{setor.nome}</div>
<div class="text-base-content/60 text-sm">Sigla: {setor.sigla}</div>
</div>
</label>
{/each}
</div>
{:else}
<div class="text-base-content/60 py-8 text-center">
<p>Nenhum setor cadastrado</p>
</div>
{/if}
</div>
<div class="modal-action">
<button class="btn" onclick={closeSetoresModal} disabled={isSavingSetores}>
Cancelar
</button>
<button class="btn btn-primary" onclick={salvarSetores} disabled={isSavingSetores}>
{#if isSavingSetores}
<span class="loading loading-spinner loading-sm"></span>
{/if}
Salvar
</button>
</div>
</div>
<button
type="button"
class="modal-backdrop"
onclick={closeSetoresModal}
aria-label="Fechar modal"
></button>
</div>
{/if}
</main>