<?php $attachmentsByKey = []; $dependentCertificateAttachments = []; foreach ($admission['attachments'] as $attachment) { $labelKey = $attachment['label_key']; if (preg_match('/^dependent_birth_certificate_(\d+)$/', (string) $labelKey, $matches)) { $dependentCertificateAttachments[(int) $matches[1]] = $attachment; continue; } $attachmentsByKey[$labelKey] = $attachment; } ksort($dependentCertificateAttachments); ?>
<?php $bankPaymentMethod = (($admission['bank_payment_method'] ?? null) === 'pix' || (strtoupper(trim((string) ($admission['bank_agency'] ?? ''))) === 'PIX' && !empty($admission['bank_account'] ?? ''))) ? 'pix' : 'bank'; $displayBankAgency = $bankPaymentMethod === 'bank' ? ($admission['bank_agency'] ?: '-') : '-'; $displayBankAccount = $bankPaymentMethod === 'bank' ? ($admission['bank_account'] ?: '-') : '-'; $displayPixKey = $bankPaymentMethod === 'pix' ? (($admission['pix_key'] ?? '') !== '' ? $admission['pix_key'] : ($admission['bank_account'] ?? '-')) : '-'; ?>
<div class="d-flex flex-column flex-lg-row justify-content-between align-items-lg-center gap-3 mb-4">
    <div>
        <h1 class="h3 fw-bold mb-1"><?= e($admission['full_name']) ?></h1>
        <p class="text-muted mb-0">Ficha #<?= e((string)$admission['id']) ?> • Status: <span class="badge status-badge status-<?= e($admission['status']) ?>"><?= e($config['statuses'][$admission['status']] ?? $admission['status']) ?></span></p>
    </div>
    <div class="d-flex flex-wrap gap-2">
        <a href="<?= e(baseUrl('admin/admissoes')) ?>" class="btn btn-outline-dark">Voltar</a>
        <a href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/editar')) ?>" class="btn btn-secondary">Editar</a>
        <a href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/imprimir')) ?>" target="_blank" class="btn btn-outline-primary">Imprimir</a>
        <a href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/pdf')) ?>" target="_blank" class="btn btn-primary">PDF</a>
        <a href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/excluir')) ?>" class="btn btn-outline-danger">Excluir resposta</a>
    </div>
</div>

<?php if (!empty($success)): ?>
    <div class="alert alert-success rounded-4"><?= e($success) ?></div>
<?php endif; ?>

<?php if (!empty($error)): ?>
    <div class="alert alert-danger rounded-4"><?= e($error) ?></div>
<?php endif; ?>

<div class="detail-section mb-4">
    <div class="d-flex flex-column flex-xl-row justify-content-between align-items-xl-center gap-3">
        <div>
            <h2 class="mb-1">Status da ficha</h2>
            <p class="text-muted mb-0">Atualize o andamento do colaborador sem precisar abrir a edição completa.</p>
        </div>

        <form method="post" action="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/status')) ?>" class="row g-2 align-items-end">
            <?= csrf_field() ?>
            <div class="col-sm-auto">
                <label for="status" class="form-label mb-1">Novo status</label>
                <select name="status" id="status" class="form-select">
                    <?php foreach ($config['statuses'] as $value => $label): ?>
                        <option value="<?= e($value) ?>" <?= selected($admission['status'], $value) ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-sm-auto">
                <button type="submit" class="btn btn-dark w-100">Salvar status</button>
            </div>
        </form>
    </div>
</div>
<div class="row g-4">
<div class="col-lg-8">
<div class="detail-section"><h2>Identificação pessoal</h2><div class="detail-grid">
<div><span>Nome</span><strong><?= e($admission['full_name']) ?></strong></div><div><span>Raça</span><strong><?= e($config['races'][$admission['race']] ?? $admission['race']) ?></strong></div>
<div><span>Unidade</span><strong><?= e($admission['unit_name']) ?></strong></div><div><span>Cargo</span><strong><?= e($admission['job_title']) ?></strong></div>
<div><span>Pai</span><strong><?= e($admission['father_name'] ?: '-') ?></strong></div><div><span>Mãe</span><strong><?= e($admission['mother_name']) ?></strong></div>
<div><span>Data de nascimento</span><strong><?= e(formatDate($admission['birth_date'])) ?></strong></div><div><span>Estado civil</span><strong><?= e($config['marital_statuses'][$admission['marital_status']] ?? $admission['marital_status']) ?></strong></div>
<div><span>Estudante</span><strong><?= e(yesNo((int)$admission['is_student'])) ?></strong></div><div><span>Escolaridade</span><strong><?= e($config['education_levels'][$admission['education_level']] ?? $admission['education_level']) ?></strong></div>
<div><span>Nacionalidade</span><strong><?= e($admission['nationality']) ?></strong></div><div><span>Naturalidade</span><strong><?= e($admission['place_of_birth'] ?: '-') ?></strong></div>
<div><span>E-mail</span><strong><?= e($admission['personal_email']) ?></strong></div><div><span>Celular</span><strong><?= e($admission['phone']) ?></strong></div>
<div><span>Contato de emergência</span><strong><?= e($admission['emergency_contact']) ?></strong></div>
</div></div>
<div class="detail-section"><h2>Endereço</h2><div class="detail-grid">
<div><span>Endereço</span><strong><?= e($admission['address_line']) ?></strong></div><div><span>Número</span><strong><?= e($admission['address_number']) ?></strong></div>
<div><span>Bairro</span><strong><?= e($admission['neighborhood']) ?></strong></div><div><span>CEP</span><strong><?= e($admission['zip_code']) ?></strong></div>
<div><span>Cidade</span><strong><?= e($admission['city']) ?></strong></div><div><span>Complemento</span><strong><?= e($admission['address_extra'] ?: '-') ?></strong></div><div><span>UF</span><strong><?= e($admission['state']) ?></strong></div>
</div></div>
<div class="detail-section"><h2>Documentos</h2><div class="detail-grid">
<div><span>RG</span><strong><?= e($admission['rg_number']) ?></strong></div><div><span>Data de expedição</span><strong><?= e(formatDate($admission['rg_issue_date'])) ?></strong></div>
<div><span>Órgão expedidor</span><strong><?= e($admission['rg_issuer']) ?></strong></div><div><span>Município de nascimento</span><strong><?= e($admission['birth_city']) ?></strong></div>
<div><span>Estado de nascimento</span><strong><?= e($admission['birth_state']) ?></strong></div><div><span>CPF</span><strong><?= e($admission['cpf']) ?></strong></div>
<div><span>PIS/PASEP</span><strong><?= e($admission['pis_number']) ?></strong></div><div><span>Data de emissão do PIS</span><strong><?= e(formatDate($admission['pis_issue_date'])) ?></strong></div>
</div></div>
<?php if (!empty($admission['cref_number']) || !empty($admission['professor_mei_cnpj']) || in_array((string) ($admission['job_title'] ?? ''), $config['professor_jobs'] ?? [], true)): ?><div class="detail-section"><h2>Professores de Educação Física</h2><div class="detail-grid"><div><span>Número do CREF</span><strong><?= e($admission['cref_number'] ?: '-') ?></strong></div><div><span>CNPJ (MEI)</span><strong><?= e($admission['professor_mei_cnpj'] ?: '-') ?></strong></div></div></div><?php endif; ?>
<div class="detail-section"><h2>Dependentes</h2><?php if (empty($admission['dependents'])): ?><p class="text-muted mb-0">Nenhum dependente informado.</p><?php else: foreach ($admission['dependents'] as $dependent): ?><div class="border rounded-4 p-3 mb-3"><div class="detail-grid"><div><span>Nome</span><strong><?= e($dependent['full_name']) ?></strong></div><div><span>Data de nascimento</span><strong><?= e(formatDate($dependent['birth_date'])) ?></strong></div><div><span>CPF</span><strong><?= e($dependent['cpf']) ?></strong></div><div><span>Cidade de nascimento</span><strong><?= e($dependent['birth_city']) ?></strong></div><div><span>Deficiência / invalidez</span><strong><?= e(yesNo((int)$dependent['has_disability'])) ?></strong></div></div></div><?php endforeach; endif; ?></div>
<div class="detail-section"><h2>Dados de admissão</h2><div class="detail-grid"><div><span>Data de admissão</span><strong><?= e(formatDate($admission['admission_date'] ?? null)) ?></strong></div><div><span>Data de demissão</span><strong><?= e(formatDate($admission['dismissal_date'] ?? null)) ?></strong></div><div><span>Carga horária</span><strong><?= e($admission['workload_hours'] ?: '-') ?></strong></div><div><span>Salário</span><strong><?= e(formatMoney($admission['salary_amount'] ?? null)) ?></strong></div><div><span>Tipo de salário</span><strong><?= e($config['salary_types'][$admission['salary_type']] ?? ($admission['salary_type'] ?: '-')) ?></strong></div><div><span>Função interna</span><strong><?= e($admission['employment_function'] ?: '-') ?></strong></div></div></div>
<div class="detail-section"><h2>Dados bancários e benefícios</h2><div class="detail-grid"><div><span>Vale transporte</span><strong><?= e(yesNo((int)$admission['uses_transport_voucher'])) ?></strong></div><div><span>Valor diário do VT</span><strong><?= e(formatMoney($admission['transport_voucher_daily_value'])) ?></strong></div><div><span>Uniforme</span><strong><?= e($admission['uniform_size'] ?: '-') ?></strong></div><div><span>Forma de recebimento</span><strong><?= e($bankPaymentMethod === 'pix' ? 'PIX' : 'Agência / Conta') ?></strong></div><div><span>Agência</span><strong><?= e($displayBankAgency) ?></strong></div><div><span>Conta</span><strong><?= e($displayBankAccount) ?></strong></div><div><span>Chave PIX</span><strong><?= e($displayPixKey) ?></strong></div><div><span>Observações internas</span><strong><?= e($admission['internal_notes'] ?: '-') ?></strong></div></div></div>
<div class="detail-section"><h2>Dados acadêmicos / estágio</h2><?php if (empty($admission['academic'])): ?><p class="text-muted mb-0">Não se aplica.</p><?php else: ?><div class="detail-grid"><div><span>Natureza do estágio</span><strong><?= e($admission['academic']['internship_nature']) ?></strong></div><div><span>Nível</span><strong><?= e($admission['academic']['academic_level']) ?></strong></div><div><span>Instituição</span><strong><?= e($admission['academic']['institution_name']) ?></strong></div><div><span>CNPJ</span><strong><?= e($admission['academic']['institution_cnpj']) ?></strong></div><div><span>Endereço</span><strong><?= e($admission['academic']['institution_address']) ?></strong></div><div><span>Bairro</span><strong><?= e($admission['academic']['institution_neighborhood']) ?></strong></div><div><span>Número</span><strong><?= e($admission['academic']['institution_number']) ?></strong></div><div><span>CEP</span><strong><?= e($admission['academic']['institution_zip']) ?></strong></div><div><span>Cidade</span><strong><?= e($admission['academic']['institution_city']) ?></strong></div><div><span>Estado</span><strong><?= e($admission['academic']['institution_state']) ?></strong></div><div><span>Coordenador / orientador</span><strong><?= e($admission['academic']['advisor_name']) ?></strong></div><div><span>Semestre</span><strong><?= e($admission['academic']['semester']) ?></strong></div><div><span>Turno que estuda</span><strong><?= e($admission['academic']['study_shift'] ?? '-') ?></strong></div><div><span>Curso</span><strong><?= e($admission['academic']['course_name']) ?></strong></div><div><span>RGM / Matrícula</span><strong><?= e($admission['academic']['registration_number']) ?></strong></div></div><?php endif; ?></div>
</div>
<div class="col-lg-4">
<div class="detail-section">
    <h2>Anexos</h2>

            <div class="d-grid gap-3">
                <?php foreach ($config['attachment_labels'] as $key => $label): ?>
                    <?php if ($key === 'dependent_birth_certificate') continue; ?>
                    <?php $attachment = $attachmentsByKey[$key] ?? null; ?>

                    <div class="border rounded-4 p-3">
                        <div class="fw-semibold mb-2"><?= e($label) ?></div>

                        <?php if ($attachment): ?>
                            <?php $isImage = str_starts_with((string) $attachment['mime_type'], 'image/'); ?>

                            <?php if ($isImage): ?>
                                <div class="attachment-thumb">
                                    <img
                                        src="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/anexos/' . $attachment['id'] . '/visualizar')) ?>"
                                        alt="<?= e($label) ?>"
                                    >
                                </div>
                            <?php endif; ?>

                            <div class="small text-muted mb-2"><?= e($attachment['original_name']) ?></div>

                            <div class="d-flex flex-wrap gap-2">
                                <?php if ($isImage): ?>
                                    <a
                                        class="btn btn-sm btn-outline-secondary"
                                        target="_blank"
                                        href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/anexos/' . $attachment['id'] . '/visualizar')) ?>"
                                    >
                                        Visualizar
                                    </a>
                                <?php endif; ?>

                                <a
                                    class="btn btn-sm btn-outline-primary"
                                    href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/anexos/' . $attachment['id'] . '/download')) ?>"
                                >
                                    Baixar anexo
                                </a>
                            </div>
                        <?php else: ?>
                            <div class="small text-muted">Não enviado.</div>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
                <?php if (!empty($dependentCertificateAttachments)): ?>
                    <?php foreach ($dependentCertificateAttachments as $index => $attachment): ?>
                        <?php $isImage = str_starts_with((string) $attachment['mime_type'], 'image/'); ?>
                        <div class="border rounded-4 p-3">
                            <div class="fw-semibold mb-2">Certidão de nascimento do dependente <?= (int) $index + 1 ?></div>
                            <?php if ($isImage): ?>
                                <div class="attachment-thumb">
                                    <img src="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/anexos/' . $attachment['id'] . '/visualizar')) ?>" alt="Certidão do dependente <?= (int) $index + 1 ?>">
                                </div>
                            <?php endif; ?>
                            <div class="small text-muted mb-2"><?= e($attachment['original_name']) ?></div>
                            <div class="d-flex flex-wrap gap-2">
                                <?php if ($isImage): ?>
                                    <a class="btn btn-sm btn-outline-secondary" target="_blank" href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/anexos/' . $attachment['id'] . '/visualizar')) ?>">Visualizar</a>
                                <?php endif; ?>
                                <a class="btn btn-sm btn-outline-primary" href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/anexos/' . $attachment['id'] . '/download')) ?>">Baixar anexo</a>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php elseif (!empty($attachmentsByKey['dependent_birth_certificate'])): ?>
                    <?php $attachment = $attachmentsByKey['dependent_birth_certificate']; $isImage = str_starts_with((string) $attachment['mime_type'], 'image/'); ?>
                    <div class="border rounded-4 p-3">
                        <div class="fw-semibold mb-2">Certidão de nascimento do dependente</div>
                        <?php if ($isImage): ?><div class="attachment-thumb"><img src="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/anexos/' . $attachment['id'] . '/visualizar')) ?>" alt="Certidão do dependente"></div><?php endif; ?>
                        <div class="small text-muted mb-2"><?= e($attachment['original_name']) ?></div>
                        <div class="d-flex flex-wrap gap-2">
                            <?php if ($isImage): ?><a class="btn btn-sm btn-outline-secondary" target="_blank" href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/anexos/' . $attachment['id'] . '/visualizar')) ?>">Visualizar</a><?php endif; ?>
                            <a class="btn btn-sm btn-outline-primary" href="<?= e(baseUrl('admin/admissoes/' . $admission['id'] . '/anexos/' . $attachment['id'] . '/download')) ?>">Baixar anexo</a>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    <div class="detail-section"><h2>Auditoria básica</h2><div class="detail-grid detail-grid-single"><div><span>Consentimento LGPD</span><strong><?= e($admission['lgpd_consent_at'] ? 'Registrado em ' . $admission['lgpd_consent_at'] : '-') ?></strong></div><div><span>IP de envio</span><strong><?= e($admission['submitted_ip'] ?: '-') ?></strong></div><div><span>Enviado em</span><strong><?= e($admission['created_at']) ?></strong></div><div><span>Última atualização</span><strong><?= e($admission['updated_at']) ?></strong></div></div></div>
</div></div>
