<?php

$pageTitle = 'Chi tiết người dùng - PET SPA';
$activeAdminPage = 'users';
$successMessage = $_SESSION['flash_success'] ?? null;
$errorMessage = $_SESSION['flash_error'] ?? null;
unset($_SESSION['flash_success'], $_SESSION['flash_error']);
$isCurrentAdmin = (int) $user['id'] === (int) ($_SESSION['user']['id'] ?? 0);

require BASE_PATH . '/app/views/layouts/header.php';
require BASE_PATH . '/app/views/layouts/navbar.php';
?>

<main class="admin-area">
    <div class="container-fluid">
        <div class="row g-4">
            <div class="col-lg-3 col-xl-2">
                <?php require BASE_PATH . '/app/views/layouts/admin-sidebar.php'; ?>
            </div>
            <div class="col-lg-9 col-xl-10">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <h1 class="h3 mb-0">Chi tiết người dùng #<?= (int) $user['id'] ?></h1>
                    <div class="d-flex gap-2"><?php if ($user['role'] !== 'admin'): ?><a class="btn btn-primary" href="<?= BASE_URL ?>/?url=admin/user/edit&id=<?= (int) $user['id'] ?>">Sửa</a><?php endif; ?><a class="btn btn-outline-secondary" href="<?= BASE_URL ?>/?url=admin/users">Quay lại danh sách</a></div>
                </div>

                <?php if ($successMessage): ?><div class="alert alert-success"><?= htmlspecialchars($successMessage) ?></div><?php endif; ?>
                <?php if ($errorMessage): ?><div class="alert alert-danger"><?= htmlspecialchars($errorMessage) ?></div><?php endif; ?>

                <div class="row g-4">
                    <section class="col-xl-7">
                        <div class="card h-100"><div class="card-body">
                            <div class="d-flex gap-3 align-items-center mb-4">
                                <?php if (!empty($user['avatar'])): ?>
                                    <img src="<?= BASE_URL ?>/<?= htmlspecialchars(ltrim($user['avatar'], '/')) ?>" alt="Avatar" width="64" height="64" class="rounded-circle object-fit-cover">
                                <?php else: ?>
                                    <div class="rounded-circle bg-light d-flex align-items-center justify-content-center" style="width:64px;height:64px"><i class="bi bi-person fs-3"></i></div>
                                <?php endif; ?>
                                <div><div class="h5 mb-1"><?= htmlspecialchars($user['full_name']) ?></div><div class="text-muted"><?= htmlspecialchars($user['email'] ?? 'Chưa có email') ?></div></div>
                            </div>
                            <dl class="row mb-0">
                                <dt class="col-sm-4">Số điện thoại</dt><dd class="col-sm-8"><?= htmlspecialchars($user['phone']) ?></dd>
                                <dt class="col-sm-4">Vai trò</dt><dd class="col-sm-8"><?= htmlspecialchars($user['role']) ?></dd>
                                <dt class="col-sm-4">Trạng thái</dt><dd class="col-sm-8"><?= htmlspecialchars($user['status']) ?></dd>
                                <dt class="col-sm-4">Đăng nhập gần nhất</dt><dd class="col-sm-8"><?= htmlspecialchars($user['last_login_at'] ?? 'Chưa đăng nhập') ?></dd>
                                <dt class="col-sm-4">Tạo lúc</dt><dd class="col-sm-8"><?= htmlspecialchars($user['created_at']) ?></dd>
                                <dt class="col-sm-4">Cập nhật lúc</dt><dd class="col-sm-8"><?= htmlspecialchars($user['updated_at']) ?></dd>
                            </dl>
                        </div></div>
                    </section>

                    <section class="col-xl-5">
                        <?php if ($user['role'] === 'customer'): ?>
                            <div class="card mb-4"><div class="card-body">
                                <h2 class="h6">Thông tin mua hàng</h2>
                                <div>Đơn hàng: <strong><?= number_format((int) $user['order_count']) ?></strong></div>
                                <div>Hoàn thành: <strong><?= number_format((int) $user['completed_order_count']) ?></strong></div>
                                <div>Đã chi: <strong><?= number_format((float) $user['total_spent'], 0, ',', '.') ?>đ</strong></div>
                            </div></div>
                        <?php endif; ?>

                        <div class="card mb-4"><div class="card-body">
                            <h2 class="h6">Trạng thái tài khoản</h2>
                            <form action="<?= BASE_URL ?>/?url=admin/user/status" method="POST" class="d-flex gap-2">
                                <input type="hidden" name="_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                                <input type="hidden" name="user_id" value="<?= (int) $user['id'] ?>">
                                <?php if ($user['status'] === 'active'): ?>
                                    <input type="hidden" name="status" value="locked">
                                    <button class="btn btn-outline-danger" <?= $isCurrentAdmin ? 'disabled' : '' ?>>Khóa tài khoản</button>
                                <?php else: ?>
                                    <input type="hidden" name="status" value="active">
                                    <button class="btn btn-outline-success">Mở khóa tài khoản</button>
                                <?php endif; ?>
                            </form>
                            <?php if ($isCurrentAdmin): ?><small class="text-muted d-block mt-2">Bạn không thể tự khóa chính mình.</small><?php endif; ?>
                        </div></div>

                        <?php if (in_array($user['role'], ['customer', 'owner'], true)): ?>
                            <div class="card"><div class="card-body">
                                <h2 class="h6">Đổi vai trò</h2>
                                <form action="<?= BASE_URL ?>/?url=admin/user/role" method="POST" class="row g-2">
                                    <input type="hidden" name="_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                                    <input type="hidden" name="user_id" value="<?= (int) $user['id'] ?>">
                                    <div class="col-8"><select name="role" class="form-select"><option value="customer" <?= $user['role'] === 'customer' ? 'selected' : '' ?>>Customer</option><option value="owner" <?= $user['role'] === 'owner' ? 'selected' : '' ?>>Owner</option></select></div>
                                    <div class="col-4"><button class="btn btn-primary w-100">Lưu</button></div>
                                </form>
                            </div></div>
                        <?php endif; ?>
                    </section>
                </div>
            </div>
        </div>
    </div>
</main>

<?php require BASE_PATH . '/app/views/layouts/footer.php'; ?>
