<?php
session_start();
ob_start(); // Start output buffering at the beginning
include("connection.php");
include("includes/header.php");
include("includes/sidebar.php");

$user_profile = $_SESSION['user_name'];
// if (!$user_profile) {
//     header('location:index.php');
//     exit();
// }

// Handle form submission
if (isset($_POST['submit'])) {
    $parent_id = $_POST['parent_id'] ?? null; // Allow null for top-level menus
    $name = $_POST['name'];
    $link = $_POST['link'];
    $order = $_POST['order'];

    // Check if name, link, and order are not empty
    if (!empty($name) && !empty($link) && !empty($order)) {
        // Ensure order is an integer
        if (!filter_var($order, FILTER_VALIDATE_INT)) {
            echo "<div class='alert alert-danger'>Order must be a valid integer.</div>";
            exit();
        }

        // Prepare the SQL statement
        $stmt = $conn->prepare("INSERT INTO menus (parent_id, name, link, `order`) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("issi", $parent_id, $name, $link, $order); // "i" for integer, "s" for string

        // Execute the query and handle the result
        if ($stmt->execute()) {
            // Redirect to the view_menu page after successful insertion
            header("Location:view_menu.php");
            exit(); // Stop further execution of the script
        } else {
            echo "<div class='alert alert-danger'>Error adding menu item: " . $stmt->error . "</div>";
        }

        // Close the prepared statement
        $stmt->close();
    } else {
        echo "<div class='alert alert-danger'>All fields are required.</div>";
    }
}
?>

<div class="app-main__outer">
    <div class="app-main__inner">

        <div class="app-page-title">
            <div class="page-title-wrapper">
                <div class="page-title-heading">
                    <div class="page-title-icon">
                        <i class="fas fa-tachometer-alt"></i>
                    </div>
                    <div>Admin Dashboard</div>
                </div>
                <div class="page-title-actions">
                    <button type="button" data-toggle="tooltip" title="Example Tooltip" data-placement="bottom" class="btn-shadow mr-3 btn btn-dark">
                        <i class="fa fa-star"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="card mb-3">
                    <div class="card-header">
                        <strong>Add Menu Item</strong>
                    </div>

                    <div class="card-body">
                        <form action="" method="post">

                            <div class="form-group">
                                <label for="parent_id">Parent Menu</label>
                                <select class="form-control" name="parent_id">
                                    <option value="">-- None (Top Level) --</option>
                                    <?php
                                    // Fetch existing menus for parent options
                                    $result = $conn->query("SELECT id, name FROM menus WHERE parent_id = 0");
                                    while ($row = $result->fetch_assoc()) {
                                        echo "<option value='{$row['id']}'>{$row['name']}</option>";
                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="form-group">
                                <label for="name">Menu Name</label>
                                <input type="text" class="form-control" name="name" placeholder="Enter Menu Name" required>
                            </div>

                            <div class="form-group">
                                <label for="link">Link</label>
                                <input type="text" class="form-control" name="link" placeholder="Enter Link" required>
                            </div>

                            <div class="form-group">
                                <label for="order">Order</label>
                                <input type="number" class="form-control" name="order" placeholder="Enter Display Order" required>
                            </div>

                            <input type="submit" class="btn btn-primary" value="Submit" name="submit">
                        </form>

                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<?php
include("includes/footer.php");
ob_end_flush(); // Send the buffered output to the client
?>
