<?php

// session_start();

require 'config.php';
$koneksi = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbname);


// Auth sync
$user_id = $_SESSION['id'] ?? null;
if ($user_id) {
    $result = mysqli_query($koneksi, "SELECT * FROM users_id WHERE id = '$user_id'");
    $whatsapp = mysqli_fetch_assoc($result);
    $logo = $whatsapp['logo'] ?? 'images/icon.png';
}

// Event Sync
if (isset($_GET['switch_event'])) {
    $switch_id = (int)$_GET['switch_event'];
    // Verify user has access
    $check_access = mysqli_query($koneksi, "SELECT 1 FROM user_events WHERE user_id = '$user_id' AND event_id = '$switch_id'");
    if (mysqli_num_rows($check_access) > 0) {
        $_SESSION['active_event_id'] = $switch_id;
        mysqli_query($koneksi, "UPDATE users_id SET active_event_id = '$switch_id' WHERE id = '$user_id'");
        header("Location: " . strtok($_SERVER["REQUEST_URI"], '?')); // Reload without query string
        exit;
    }
}

$active_event_id = get_active_event_id();
if ($active_event_id) {
    $event_query = mysqli_query($koneksi, "SELECT * FROM event_id WHERE id = '$active_event_id'");
    $checkevent = mysqli_fetch_assoc($event_query);
    $acara = $checkevent['acara'];
    
    // Override timezone with event-specific timezone
    if (isset($checkevent['timezone'])) {
        $timezone = $checkevent['timezone'];
        date_default_timezone_set($timezone);
    }
} else {
    $acara = "Buku Tamu";
}

// Get user's events for switcher
$user_events = [];
if ($user_id) {
    $user_events = query("SELECT e.* FROM event_id e JOIN user_events ue ON e.id = ue.event_id WHERE ue.user_id = '$user_id'");
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.0/font/bootstrap-icons.css">
    <!-- Data Tables -->
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.23/css/dataTables.bootstrap5.min.css">
    <!-- Font Google -->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Righteous&display=swap" rel="stylesheet">
    <!-- Own CSS -->
    <link rel="stylesheet" href="css/style.css">
       <!-- Sweet Alert -->
    <link type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/8.11.8/sweetalert2.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script type="text/javascript" src="js/app.js"></script>
    <script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.23/js/dataTables.bootstrap5.min.js"></script>

    <script src="library/bootstrap-datepicker.js"></script>
    <link rel="stylesheet" href="library/datepicker.css">
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
    
    <title><?= $acara;?></title>
    <link rel="icon" type="image/png" href="<?php echo linHome();?>/<?=$logo;?>">

    <style>
        .dataTables_length, .dataTables_info, .dataTables_filter {
            text-align: left;
            font-size: 13px;
        }
        .pagination {
            font-size: 12px;
        }
    </style>


    <!-- Modern CSS -->
    <link rel="stylesheet" href="css/modern.css">
    
    <!-- Title -->
    <title><?= $acara;?></title>
    <link rel="icon" type="image/png" href="<?=$logo;?>">
</head>



<body style="width:100%;" data-instant-allow-query-string="" data-instant-allow-external-links="">

    <nav class="navbar navbar-expand-lg border-bottom sticky-top">
    <div class="container">
        <a class="navbar-brand" href="<?= is_subscription_active($_SESSION['id']) ? 'dashboard' : 'profile' ?>">
            <img src="<?= $logo; ?>" alt="logo">
        </a>
        <button type="button" class="navbar-toggler collapsed" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-expanded="false">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarCollapse">
            <div class="navbar-nav">
                <?php $current_page = basename($_SERVER['PHP_SELF'], '.php'); ?>
                <?php if (isset($_SESSION['id']) && is_subscription_active($_SESSION['id'])): ?>
                    <a class="nav-item nav-link <?= ($current_page == 'dashboard') ? 'active' : '' ?>" href="dashboard">Dashboard</a>
                <?php endif; ?>
                <a class="nav-item nav-link <?= ($current_page == 'profile') ? 'active' : '' ?>" href="profile">Profile</a>
            <?php if (isset($_SESSION['role']))
            {
                if ($_SESSION['role'] == "admin")
                {
                    $active_user = ($current_page == 'user') ? 'active' : '';
                    echo '<a class="nav-item nav-link ' . $active_user . '" href="user">Users</a>';
                }
            }?>
            <?php if (isset($_SESSION['role']) && $_SESSION['role'] == "admin"): ?>
                <a class="nav-item nav-link <?= ($current_page == 'settings') ? 'active' : '' ?>" href="settings">Settings</a>
            <?php endif; ?>
                <?php if (is_subscription_active($_SESSION['id'])): ?>
                    <a class="nav-item nav-link <?= ($current_page == 'event') ? 'active' : '' ?>" href="event">Event</a>
                    <?php if (isset($_SESSION['role']) && $_SESSION['role'] == "admin"): ?>
                        <a class="nav-item nav-link <?= ($current_page == 'manage-events') ? 'active' : '' ?>" href="manage-events.php">Manage Events</a>
                    <?php endif; ?>
                <?php endif; ?>
                
                <?php if (count($user_events) > 1): ?>
                <div class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle d-flex align-items-center" href="#" id="eventDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="bi bi-calendar-event"></i>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-end shadow-lg border-0 radius-12 bg-white" aria-labelledby="eventDropdown" style="min-width: 200px;">
                        <li><h6 class="dropdown-header">Switch Event</h6></li>
                        <?php foreach ($user_events as $ue): ?>
                        <li><a class="dropdown-item py-2 <?= ($ue['id'] == $active_event_id) ? 'active' : '' ?>" href="?switch_event=<?= $ue['id'] ?>"><?= $ue['acara'] ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
                <?php endif; ?>

                <a class="nav-item nav-link" href="https://momenpedia.com/dash-panduan" target="_blank">Panduan</a>
            </div>
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="logout"><i class="bi bi-box-arrow-right"></i>&nbsp;Logout</a>
            </div>
        </div>
    </div>
</nav>