<?php
$isEdit = isset($user);
$formAction = $isEdit
    ? baseUrl('admin/usuarios/' . $user['id'] . '/editar')
    : baseUrl('admin/usuarios');

$activeValue = old('is_active', $isEdit ? (string) $user['is_active'] : '1');
$currentRole = old('role', $user['role'] ?? 'operador');
?>

<?php if (!empty($error)): ?>
    <div class="alert alert-danger rounded-4 mb-4">
        <div class="fw-semibold mb-2"><?= e($error) ?></div>
        <?php if (!empty($errors)): ?>
            <ul class="mb-0 ps-3">
                <?php foreach ($errors as $message): ?>
                    <li><?= e($message) ?></li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>
<?php endif; ?>

<div class="card border-0 shadow-sm">
    <div class="card-body p-4 p-lg-5">
        <form method="post" action="<?= e($formAction) ?>" novalidate>
            <?= csrf_field() ?>

            <div class="row g-4">
                <div class="col-lg-6">
                    <label class="form-label">Nome completo</label>
                    <input
                        type="text"
                        name="name"
                        class="form-control <?= !empty($errors['name']) ? 'is-invalid' : '' ?>"
                        value="<?= e(old('name', $user['name'] ?? '')) ?>"
                        required
                    >
                    <?php if (!empty($errors['name'])): ?>
                        <div class="invalid-feedback d-block"><?= e($errors['name']) ?></div>
                    <?php endif; ?>
                </div>

                <div class="col-lg-6">
                    <label class="form-label">E-mail</label>
                    <input
                        type="email"
                        name="email"
                        class="form-control <?= !empty($errors['email']) ? 'is-invalid' : '' ?>"
                        value="<?= e(old('email', $user['email'] ?? '')) ?>"
                        required
                    >
                    <?php if (!empty($errors['email'])): ?>
                        <div class="invalid-feedback d-block"><?= e($errors['email']) ?></div>
                    <?php endif; ?>
                </div>

                <div class="col-lg-6">
                    <label class="form-label">Tipo de usuário</label>
                    <select
                        name="role"
                        class="form-select <?= !empty($errors['role']) ? 'is-invalid' : '' ?>"
                        required
                    >
                        <?php foreach (($roleOptions ?? []) as $value => $label): ?>
                            <option value="<?= e($value) ?>" <?= selected($currentRole, $value) ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <div class="form-text">
                        Apenas o perfil Administrador pode acessar Diagnóstico e Usuários do painel.
                    </div>
                    <?php if (!empty($errors['role'])): ?>
                        <div class="invalid-feedback d-block"><?= e($errors['role']) ?></div>
                    <?php endif; ?>
                </div>

                <div class="col-lg-6">
                    <label class="form-label"><?= $isEdit ? 'Nova senha' : 'Senha' ?></label>
                    <input
                        type="password"
                        name="password"
                        class="form-control <?= !empty($errors['password']) ? 'is-invalid' : '' ?>"
                        <?= $isEdit ? '' : 'required' ?>
                    >
                    <div class="form-text">
                        <?= $isEdit ? 'Preencha apenas se quiser trocar a senha.' : 'Use pelo menos 8 caracteres.' ?>
                    </div>
                    <?php if (!empty($errors['password'])): ?>
                        <div class="invalid-feedback d-block"><?= e($errors['password']) ?></div>
                    <?php endif; ?>
                </div>

                <div class="col-lg-6">
                    <label class="form-label">Confirmar senha</label>
                    <input
                        type="password"
                        name="password_confirmation"
                        class="form-control <?= !empty($errors['password_confirmation']) ? 'is-invalid' : '' ?>"
                        <?= $isEdit ? '' : 'required' ?>
                    >
                    <?php if (!empty($errors['password_confirmation'])): ?>
                        <div class="invalid-feedback d-block"><?= e($errors['password_confirmation']) ?></div>
                    <?php endif; ?>
                </div>

                <div class="col-12">
                    <input type="hidden" name="is_active" value="0">
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input <?= !empty($errors['is_active']) ? 'is-invalid' : '' ?>"
                            type="checkbox"
                            role="switch"
                            id="is_active"
                            name="is_active"
                            value="1"
                            <?= checked($activeValue, 1) ?>
                        >
                        <label class="form-check-label" for="is_active">Usuário ativo</label>
                    </div>
                    <?php if (!empty($errors['is_active'])): ?>
                        <div class="invalid-feedback d-block"><?= e($errors['is_active']) ?></div>
                    <?php endif; ?>
                </div>
            </div>

            <div class="d-flex flex-wrap justify-content-end gap-2 mt-4">
                <a href="<?= e(baseUrl('admin/usuarios')) ?>" class="btn btn-outline-secondary">Cancelar</a>
                <button type="submit" class="btn btn-dark"><?= $isEdit ? 'Salvar alterações' : 'Criar usuário' ?></button>
            </div>
        </form>
    </div>
</div>
