<?php
$profile = is_array($profile ?? null) ? $profile : [];

$profileName = (string) old('perfil_nombre', (string) ($profile['nombre'] ?? ''));
$profileEmail = (string) old('perfil_email', (string) ($profile['email'] ?? ''));
$profilePhone = (string) old('perfil_telefono', (string) ($profile['telefono'] ?? ''));

$roleName = trim((string) ($profile['rol_nombre'] ?? 'Sin rol'));
$companyName = trim((string) ($profile['empresa_nombre'] ?? ''));
$branchName = trim((string) ($profile['sucursal_nombre'] ?? ''));
$isActive = (int) ($profile['activo'] ?? 0) === 1;
$updatedAt = trim((string) ($profile['updated_at'] ?? ''));
?>

<div class="row g-3">
    <div class="col-12 col-xl-8">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex align-items-center gap-2 mb-3">
                    <span class="vx-auth-icon"><i class="bx bx-user-circle"></i></span>
                    <div>
                        <h2 class="h5 mb-0">Datos del perfil</h2>
                        <small class="text-muted">Actualiza tus datos personales de acceso.</small>
                    </div>
                </div>

                <form action="<?= e(url('perfil')) ?>" method="post">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">Nombre</label>
                            <input
                                class="form-control"
                                name="perfil_nombre"
                                value="<?= e($profileName) ?>"
                                maxlength="150"
                                required
                            >
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Correo</label>
                            <input
                                type="email"
                                class="form-control"
                                name="perfil_email"
                                value="<?= e($profileEmail) ?>"
                                maxlength="180"
                                required
                            >
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Teléfono</label>
                            <input
                                class="form-control"
                                name="perfil_telefono"
                                value="<?= e($profilePhone) ?>"
                                maxlength="40"
                                placeholder="Opcional"
                            >
                        </div>
                    </div>

                    <button class="btn btn-primary mt-3" type="submit">
                        <i class="bx bx-save me-1"></i>Guardar cambios
                    </button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-12 col-xl-4">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <h3 class="h6 mb-3">Resumen de sesión</h3>
                <p class="mb-2"><strong>Rol:</strong> <?= e($roleName) ?></p>
                <p class="mb-2"><strong>Estado:</strong> <?= $isActive ? '<span class="badge text-bg-success">Activo</span>' : '<span class="badge text-bg-secondary">Inactivo</span>' ?></p>
                <p class="mb-2"><strong>Empresa:</strong> <?= e($companyName !== '' ? $companyName : 'Superadmin') ?></p>
                <p class="mb-2"><strong>Sucursal:</strong> <?= e($branchName !== '' ? $branchName : 'Sin asignar') ?></p>
                <?php if ($updatedAt !== ''): ?>
                    <p class="mb-0 text-muted"><small>Última actualización: <?= e($updatedAt) ?></small></p>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-12">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <div class="d-flex align-items-center gap-2 mb-3">
                    <span class="vx-auth-icon"><i class="bx bx-lock-alt"></i></span>
                    <div>
                        <h2 class="h5 mb-0">Seguridad</h2>
                        <small class="text-muted">Cambia tu contraseña de forma segura.</small>
                    </div>
                </div>

                <form action="<?= e(url('perfil/password')) ?>" method="post">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label">Contraseña actual</label>
                            <input type="password" class="form-control" name="password_actual" minlength="8" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Nueva contraseña</label>
                            <input type="password" class="form-control" name="password_nueva" minlength="8" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Confirmar contraseña</label>
                            <input type="password" class="form-control" name="password_confirmacion" minlength="8" required>
                        </div>
                    </div>

                    <button class="btn btn-outline-primary mt-3" type="submit">
                        <i class="bx bx-shield-quarter me-1"></i>Actualizar contraseña
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
