<?php

require_once BASE_PATH . '/app/helpers/Authorization.php';
require_once BASE_PATH . '/app/models/Notification.php';
$activeCustomerPage = $activeCustomerPage ?? '';
if (!isset($unreadNotificationCount)) {
    try {
        $unreadNotificationCount = (new Notification())->countUnreadByUser((int) $_SESSION['user']['id']);
    } catch (Throwable) {
        $unreadNotificationCount = 0;
    }
}
$customerMenuItems = [
    ['key' => 'dashboard', 'permission' => 'customer.dashboard', 'route' => 'customer', 'icon' => 'bi-grid', 'label' => 'Tổng quan'],
    ['key' => 'booking-create', 'permission' => 'customer.booking.create', 'route' => 'customer/booking/create', 'icon' => 'bi-plus-circle', 'label' => 'Đặt lịch'],
    ['key' => 'bookings', 'permission' => 'customer.booking.view', 'route' => 'customer/bookings', 'icon' => 'bi-calendar-check', 'label' => 'Lịch của tôi'],
    ['key' => 'notifications', 'permission' => 'customer.notification.view', 'route' => 'customer/notifications', 'icon' => 'bi-bell', 'label' => 'Thông báo'],
    ['key' => 'profile', 'permission' => 'customer.profile.manage', 'route' => 'customer/profile', 'icon' => 'bi-person-gear', 'label' => 'Hồ sơ'],
    ['key' => 'password', 'permission' => 'customer.profile.manage', 'route' => 'customer/profile#change-password', 'icon' => 'bi-key', 'label' => 'Đổi mật khẩu']
];
?>
<aside class="customer-sidebar">
    <div class="customer-sidebar-user"><div class="customer-avatar"><?php if (!empty($_SESSION['user']['avatar'])): ?><img src="<?= BASE_URL ?>/<?= htmlspecialchars(ltrim($_SESSION['user']['avatar'], '/')) ?>?v=<?= urlencode((string) ($_SESSION['user']['avatar_version'] ?? '1')) ?>" alt="Ảnh đại diện"><?php else: ?><i class="bi bi-person"></i><?php endif; ?></div><div><strong><?= htmlspecialchars($_SESSION['user']['full_name'] ?? '') ?></strong><small>Khách hàng Spa</small></div><button class="sidebar-toggle ms-auto" type="button" aria-label="Mở menu khách hàng" aria-expanded="false"><i class="bi bi-list"></i></button></div>
    <nav class="customer-menu" aria-label="Menu khách hàng">
        <?php foreach ($customerMenuItems as $item): ?>
            <?php if (can($item['permission'])): ?>
                <a class="<?= $activeCustomerPage === $item['key'] ? 'active' : '' ?>" href="<?= BASE_URL ?>/?url=<?= htmlspecialchars($item['route']) ?>"><i class="bi <?= htmlspecialchars($item['icon']) ?>"></i><span><?= htmlspecialchars($item['label']) ?></span><?php if ($item['key'] === 'notifications' && $unreadNotificationCount): ?><span class="badge text-bg-danger ms-auto"><?= (int) $unreadNotificationCount ?></span><?php endif; ?></a>
            <?php endif; ?>
        <?php endforeach; ?>
        <form
    method="POST"
    action="<?= BASE_URL ?>/?url=logout"
    class="customer-logout-form"
>
    <input
        type="hidden"
        name="_token"
        value="<?= htmlspecialchars(
            $_SESSION['csrf_token'] ?? ''
        ) ?>"
    >

    <button
        type="submit"
        class="customer-menu-logout"
    >
        <i class="bi bi-box-arrow-right"></i>

        <span>
            Đăng xuất
        </span>
    </button>
</form>
    </nav>
</aside>
