<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
session_start();
require_once "config.php";

if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
    header("location: index.php");
    exit;
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['branches'])) {
    $selected_branches = $_POST['branches'];

    $_SESSION["selected_branches"] = $selected_branches;

    header("location: dashboard.php");
    exit;
}
?>
<!DOCTYPE html>
<html>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

<head>
    <title>DCL - Select Branch</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="_token" content="hjNpe8ZTnCKGglKMW9sw81G1NiWyGTLydjvmiHIS">

    <link rel="shortcut icon" href="../favicon.ico">

    <!-- plugin css -->
    <link media="all" type="text/css" rel="stylesheet" href="assets/plugins/%40mdi/font/css/materialdesignicons.min.css">
    <link media="all" type="text/css" rel="stylesheet" href="assets/plugins/perfect-scrollbar/perfect-scrollbar.css">
    <!-- end plugin css -->

    <!-- common css -->
    <link media="all" type="text/css" rel="stylesheet" href="css/app.css">
    <!-- end common css -->

</head>

<body>
    <div class="container-scroller" id="app">
        <div class="container-fluid page-body-wrapper full-page-wrapper">

            <div class="content-wrapper d-flex align-items-center auth">
                <div class="row flex-grow">
                    <div class="col-lg-4 mx-auto">
                        <div class="auth-form-light text-left p-5">
                            <div class="brand-logo">
                                <img src="assets/images/logo.webp" class="w-100">
                            </div>

                            <div class="row text-center">
                                <div class="col-12">
                                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                                        <label for="product_type">Select Branch</label>
                                        <?php
                                            $sql = "SELECT branch_id, branch_name FROM branches";
                                            $result = $conn->query($sql);

                                            if ($result && $result->num_rows > 0) {
                                                echo '<div id="product_types" class="row p-3">';

                                                while ($row = $result->fetch_assoc()) {
                                                    $id = $row['branch_id'];
                                                    $name = $row['branch_name'];
                                                    ?>
                                                        <div class="form-group px-3 mb-0">
                                                            <input type="checkbox" id="branch_id<?php echo $id; ?>" name="branches[]"
                                                                value="<?php echo $id; ?>">
                                                            <label class="text-capitalize" for="branch_id<?php echo $id; ?>">
                                                                <?php echo $name ?></label>
                                                        </div>
                                                        <?php
                                                }
                                                echo '</div>';
                                            } else {
                                                echo '<p>No branches found.</p>';
                                            }
                                        ?>
                                        <div class="mt-3">
                                            <input class="btn btn-block btn-primary btn-lg font-weight-medium auth-form-btn" type="submit" value="Submit">
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>

    <script src="js/app.js"></script>

</body>

</html>
