<?php
$errors = $errors ?? [];
$error = $error ?? null;
$success = $success ?? null;
$maxUploadFileBytes = (int) ($maxUploadFileBytes ?? \Core\RequestLimits::uploadMaxFileBytes());
$maxUploadFileLabel = (string) ($maxUploadFileLabel ?? \Core\RequestLimits::uploadMaxFileLabel());
$postMaxBytes = (int) ($postMaxBytes ?? \Core\RequestLimits::postMaxBytes());
$postMaxLabel = (string) ($postMaxLabel ?? \Core\RequestLimits::postMaxLabel());
$currentAttachment = $profilePhotoAttachment ?? null;
$profilePhotoAccessToken = (string) ($profilePhotoAccessToken ?? '');
?>
<div class="row justify-content-center">
    <div class="col-12 col-lg-8 col-xl-7">
        <div class="hero-card mb-4">
            <div class="hero-card__content">
                <div>
                    <span class="badge text-bg-success mb-3">Etapa 3 de 3</span>
                    <h1 class="display-6 fw-bold mb-2">Envie a foto de perfil</h1>
                    <p class="lead text-muted mb-0">A ficha e os documentos já foram enviados. Agora a foto vai em um envio separado para fechar o processo sem sobrecarregar o servidor.</p>
                </div>
                <div class="hero-card__aside">
                    <div class="hero-stat"><span>Colaborador</span><strong><?= e((string) ($admission['full_name'] ?? '-')) ?></strong></div>
                    <div class="hero-stat"><span>CPF</span><strong><?= e((string) ($admission['cpf'] ?? '-')) ?></strong></div>
                    <div class="hero-stat"><span>Limite</span><strong><?= e($maxUploadFileLabel) ?></strong></div>
                </div>
            </div>
        </div>

        <?php if (!empty($success)): ?>
            <div class="alert alert-success shadow-sm form-alert">
                <strong><?= e($success) ?></strong>
            </div>
        <?php endif; ?>

        <?php if (!empty($errors) || !empty($error)): ?>
            <div class="alert alert-danger shadow-sm form-alert">
                <strong><?= e($error ?? 'Revise a foto de perfil e tente novamente.') ?></strong>
                <ul class="form-alert__list mb-0 mt-2">
                    <?php foreach ($errors as $message): ?>
                        <li><?= e($message) ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>

        <div class="card shadow-sm border-0">
            <div class="card-body p-3 p-lg-4">
                <form
                    method="post"
                    action="<?= e(baseUrl('admissoes/foto-perfil')) ?>"
                    enctype="multipart/form-data"
                    novalidate
                    autocomplete="off"
                    data-upload-guard="true"
                    data-max-file-size-bytes="<?= e((string) $maxUploadFileBytes) ?>"
                    data-max-file-size-label="<?= e($maxUploadFileLabel) ?>"
                    data-post-max-bytes="<?= e((string) $postMaxBytes) ?>"
                    data-post-max-label="<?= e($postMaxLabel) ?>"
                    data-form-time-limit-seconds="<?= e((string) ($formTimeoutSeconds ?? \Core\RequestLimits::formTimeoutSeconds())) ?>"
                    data-timeout-scope="profile-photo-step"
                >
                    <?= csrf_field() ?>
                    <input type="hidden" name="MAX_FILE_SIZE" value="<?= e((string) $maxUploadFileBytes) ?>">
                    <input type="hidden" name="form_started_at" value="<?= e((string) time()) ?>">
                    <input type="hidden" name="admission_id" value="<?= e((string) ($admission['id'] ?? '')) ?>">
                    <input type="hidden" name="photo_access_token" value="<?= e($profilePhotoAccessToken) ?>">

                    <div class="form-section mb-0">
                        <div class="section-header">
                            <h2>Foto de perfil</h2>
                            <p>Envie uma imagem JPG ou PNG.</p>
                        </div>

                        <div class="row g-3 align-items-start">
                            <div class="col-12">
                                <label class="form-label">Foto de perfil</label>
                                <input
                                    type="file"
                                    class="form-control js-file-preview<?= !empty($errors['profile_photo']) ? ' is-invalid' : '' ?>"
                                    name="profile_photo"
                                    accept=".jpg,.jpeg,.png"
                                    data-preview-target="#preview-profile-photo"
                                    data-preview-image="#preview-image-profile-photo"
                                    data-preview-meta="#preview-meta-profile-photo"
                                >
                                <?php if (!empty($errors['profile_photo'])): ?>
                                    <div class="field-error"><?= e($errors['profile_photo']) ?></div>
                                <?php endif; ?>
                                <div class="form-text">Máximo <?= e($maxUploadFileLabel) ?> por arquivo.</div>
                            </div>

                            <div class="col-12">
                                <div id="preview-profile-photo" class="upload-preview<?= $currentAttachment ? '' : ' is-empty' ?>">
                                    <div class="upload-preview__media d-none">
                                        <img
                                            id="preview-image-profile-photo"
                                            class="upload-preview__image"
                                            alt="Foto de perfil"
                                            src=""
                                        >
                                    </div>

                                    <div id="preview-meta-profile-photo" class="upload-preview__meta">
                                        <?php if ($currentAttachment): ?>
                                            <div class="upload-preview__filename"><?= e((string) $currentAttachment['original_name']) ?></div>
                                            <div class="upload-preview__size">Tamanho: <?= e(formatBytes((int) ($currentAttachment['size_bytes'] ?? 0))) ?></div>
                                            <div class="upload-preview__hint">Arquivo atual salvo para esta ficha. Ao selecionar uma nova imagem, a prévia aparece aqui.</div>
                                        <?php else: ?>
                                            <div class="upload-preview__filename">Nenhum arquivo selecionado</div>
                                            <div class="upload-preview__hint">A miniatura aparece aqui quando você escolher uma imagem.</div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-tips form-tips--limits mb-4">
                        <div><strong>Limite por arquivo:</strong> <?= e($maxUploadFileLabel) ?></div>
                        <div><strong>Limite total do envio:</strong> <?= e($postMaxLabel) ?></div>
                    </div>

                    <div class="d-flex flex-column flex-md-row gap-3 justify-content-end mt-4">
                        <a href="<?= e(baseUrl('')) ?>" class="btn btn-outline-secondary px-4">Voltar ao início</a>
                        <button type="submit" class="btn btn-primary px-4">Enviar foto e concluir</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
