<?php

require_once __DIR__ . '/../library/school_session.php';

/*
|--------------------------------------------------------------------------
| PAGE TITLE
|--------------------------------------------------------------------------
*/

$page_title = $page_title ?? 'School Management System';


/*
|--------------------------------------------------------------------------
| CURRENT SESSION
|--------------------------------------------------------------------------
*/

$current_session = get_current_session_name($pdo);

$all_sessions = get_all_sessions($pdo);


/*
|--------------------------------------------------------------------------
| UPDATE ACTIVE SESSION
|--------------------------------------------------------------------------
*/

if (
    isset($_POST['change_session'])
    && isset($_POST['session_id'])
) {

    $session_id = (int) $_POST['session_id'];

    $school_id = $_SESSION['school_id'];


    // ======================================================
    // REMOVE OLD ACTIVE
    // ======================================================

    $stmt = $pdo->prepare("

        UPDATE academic_sessions

        SET status = 'Inactive'

        WHERE school_id = ?

    ");

    $stmt->execute([$school_id]);


    // ======================================================
    // SET NEW ACTIVE
    // ======================================================

    $stmt = $pdo->prepare("

        UPDATE academic_sessions

        SET status = 'Active'

        WHERE id = ?
        AND school_id = ?

    ");

    $stmt->execute([

        $session_id,
        $school_id

    ]);


    header("Location: " . $_SERVER['REQUEST_URI']);

    exit;
}

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <title>

        <?= htmlspecialchars($page_title) ?>

        | Web Devil

    </title>


    <!-- ADMIN LTE -->

    <link
        rel="stylesheet"
        href="https://cdnjs.cloudflare.com/ajax/libs/admin-lte/3.2.0/css/adminlte.min.css"
    >


    <!-- FONT AWESOME -->

    <link
        rel="stylesheet"
        href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css"
    >


    <!-- GOOGLE FONT -->

    <link
        href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap"
        rel="stylesheet"
    >


    <style>

        body {

            font-family: 'Poppins', sans-serif;

            background: #f4f6f9;
        }

        .main-header {

            border-bottom: 1px solid #e5e7eb;

            background: #ffffff;
        }

        .navbar-nav .nav-link {

            color: #1f2937 !important;

            font-weight: 500;
        }

        .navbar-nav .nav-link:hover {

            color: #2563eb !important;
        }

        .search-box {

            width: 320px;
        }

        .search-box .form-control {

            border-radius: 30px 0 0 30px;

            border: 1px solid #d1d5db;

            height: 40px;
        }

        .search-box .btn {

            border-radius: 0 30px 30px 0;

            padding: 0 18px;
        }

        .session-btn {

            border-radius: 30px;

            padding: 8px 14px;

            font-size: 14px;

            font-weight: 600;
        }

        .user-chip {

            background: #f3f4f6;

            border-radius: 30px;

            padding: 8px 14px;

            display: flex;

            align-items: center;

            gap: 8px;

            font-size: 14px;

            font-weight: 500;
        }

        .user-chip i {

            color: #2563eb;
        }

        .logout-btn {

            border-radius: 30px;

            padding: 8px 16px;

            font-weight: 600;
        }

        .dropdown-menu {

            border-radius: 14px;

            border: none;

            box-shadow: 0 10px 25px rgba(0,0,0,0.08);
        }

        .alert {

            border-radius: 10px;

            margin: 15px;
        }
		.content-wrapper {

    margin-top: 10px !important;
}

        @media (max-width: 991px) {

            .main-header {

                padding: 8px 10px;
            }

            .search-box {

                width: 100%;
            }

            .navbar-nav.ml-auto {

                width: 100%;

                margin-top: 10px;

                align-items: stretch !important;
            }

            .navbar-nav.ml-auto .nav-item {

                width: 100%;

                margin-bottom: 10px;
            }

            .user-chip {

                justify-content: center;
            }

            .logout-btn {

                width: 100%;
            }

            .session-btn {

                width: 100%;
            }

            .dropdown-menu {

                width: 100%;
            }
        }

    </style>

</head>

<body class="hold-transition sidebar-mini layout-fixed">

<div class="wrapper">


    <!-- ====================================================== -->
    <!-- NAVBAR -->
    <!-- ====================================================== -->

    <nav class="main-header navbar navbar-expand-lg navbar-light">

        <!-- LEFT -->

        <ul class="navbar-nav align-items-center">

            <li class="nav-item">

                <a
                    class="nav-link"
                    data-widget="pushmenu"
                    href="#"
                    role="button"
                >

                    <i class="fas fa-bars"></i>

                </a>

            </li>

        </ul>


        <!-- MOBILE TOGGLE -->

        <button
            class="navbar-toggler"
            type="button"
            data-toggle="collapse"
            data-target="#topNavbar"
        >

            <span class="navbar-toggler-icon"></span>

        </button>


        <!-- RIGHT -->

        <div class="collapse navbar-collapse" id="topNavbar">

            <ul class="navbar-nav ml-auto align-items-center">


                <!-- SEARCH -->

                <li class="nav-item mr-lg-3">

                    <form
                        action="<?php echo WEB_ROOT; ?>search/search_results.php"
                        method="GET"
                        class="form-inline search-box"
                    >

                        <div class="input-group w-100">

                            <input
                                type="text"
                                name="q"
                                class="form-control"
                                placeholder="Search student, fees, staff..."
                                autocomplete="off"
                                required
                            >

                            <div class="input-group-append">

                                <button
                                    class="btn btn-primary"
                                    type="submit"
                                >

                                    <i class="fas fa-search"></i>

                                </button>

                            </div>

                        </div>

                    </form>

                </li>


                <!-- SESSION -->

                <li class="nav-item dropdown mr-lg-3">

                    <a
                        class="nav-link"
                        data-toggle="dropdown"
                        href="#"
                    >

                        <span class="badge badge-primary session-btn">

                            <i class="fas fa-calendar-alt mr-1"></i>

                            <?= htmlspecialchars(
                                $current_session ?: 'Session Not Set'
                            ) ?>

                            <i class="fas fa-chevron-down ml-2"></i>

                        </span>

                    </a>


                    <!-- DROPDOWN -->

                    <div class="dropdown-menu dropdown-menu-right p-3">

                        <form method="POST">

                            <div class="form-group">

                                <label>

                                    Academic Session

                                </label>

                                <select
                                    name="session_id"
                                    class="form-control"
                                    required
                                >

                                    <?php foreach ($all_sessions as $session): ?>

                                        <option
                                            value="<?= $session['id']; ?>"

                                            <?= ($session['status'] == 'Active')
                                                ? 'selected'
                                                : ''; ?>
                                        >

                                            <?= htmlspecialchars(
                                                $session['session_name']
                                            ) ?>

                                            <?= ($session['status'] == 'Active')
                                                ? ' (Active)'
                                                : ''; ?>

                                        </option>

                                    <?php endforeach; ?>

                                </select>

                            </div>

                            <button
                                type="submit"
                                name="change_session"
                                class="btn btn-primary btn-block"
                            >

                                <i class="fas fa-save mr-1"></i>

                                Update Session

                            </button>

                        </form>

                    </div>

                </li>


                <!-- USER -->

                <li class="nav-item mr-lg-3">

                    <div class="user-chip">

                        <i class="fas fa-user-circle fa-lg"></i>

                        <span>

                            <?= htmlspecialchars(
                                $_SESSION['name'] ?? 'User'
                            ) ?>

                        </span>

                    </div>

                </li>


                <!-- LOGOUT -->

                <li class="nav-item">

                    <a
                        href="<?php echo WEB_ROOT; ?>logout.php"
                        class="btn btn-outline-danger logout-btn"
                    >

                        <i class="fas fa-sign-out-alt mr-1"></i>

                        Logout

                    </a>

                </li>

            </ul>

        </div>

    </nav>


    <!-- ====================================================== -->
    <!-- FLASH MESSAGES -->
    <!-- ====================================================== -->

    <?php if (isset($_SESSION['success'])): ?>

        <div class="alert alert-success alert-dismissible fade show">

            <button
                type="button"
                class="close"
                data-dismiss="alert"
            >

                &times;

            </button>

            <i class="fas fa-check-circle mr-2"></i>

            <?php

            echo htmlspecialchars($_SESSION['success']);

            unset($_SESSION['success']);

            ?>

        </div>

    <?php endif; ?>


    <?php if (isset($_SESSION['error'])): ?>

    <div class="alert alert-danger alert-dismissible fade show">

        <button
            type="button"
            class="close"
            data-dismiss="alert"
        >

            &times;

        </button>

        <i class="fas fa-times-circle mr-2"></i>

        <?php

        echo htmlspecialchars($_SESSION['error']);

        unset($_SESSION['error']);

        ?>

    </div>

<?php endif; ?>