<?php

include "../root.class.php";
$db = new db_safeguard();
$authentication = new authentication();

$data_decrypted = $authentication->decrypt_password($_GET['data']);
$data = explode("&", $data_decrypted);
$_SESSION['restorant_id'] = explode("=", $data[0])[1];
$_SESSION['table_id'] = explode("=", $data[1])[1];

$resto_res = $db->query("restorants", "SELECT * FROM restorants WHERE record_id = {$_SESSION['restorant_id']} LIMIT 1");
$restorant_data = $resto_res->fetch_assoc();

// check if there is a open order for this table:
$table_history_check_res = $db->query("temp_table_orders", "SELECT * FROM temp_table_orders WHERE table_id = {$_SESSION['table_id']}  AND restorant_id = {$_SESSION['restorant_id']} LIMIT 1");

if ($table_history_check_res->num_rows > 0) {
    $table_data = $table_history_check_res->fetch_assoc();
    $_SESSION['temp_table_orders_id'] = $table_data['record_id'];
} else {
    $_SESSION['temp_table_orders_id'] = $db->query("temp_table_orders", "INSERT INTO temp_table_orders (table_id, restorant_id) VALUES ({$_SESSION['table_id']}, {$_SESSION['restorant_id']})");
}

?>
<style>
    .menu-item {
        display: inline-block;
        margin: 10px;
        padding: 5vw;
        border: 2px solid #333;
        border-radius: 5px;
        background-color: #f2f2f2;
        width: 86%;
        height: auto;
        text-align: center;
        font-size: 8vw;
    }

    .menu-item a {
        text-decoration: none;
        color: #333;
        font-size: 10vw;
    }

    .menu-item img {
        width: 50%;
    }

    .item-link {
        display: block;
        margin: 5px 0;
        padding: 5px;
        background-color: #e0e0e0;
        border-radius: 3px;
        transition: background-color 0.3s ease;
        font-size: 6vw;
    }

    .item-link:hover {
        background-color: #ccc;
    }

    .item-amount {
        display: inline-block;
        margin-left: 10px;
        vertical-align: middle;
    }

    .item-amount-btn {
        font-size: 4vw;
        width: 10vw;
    }

    .item-amount-input {
        font-size: 6vw;
        width: 10vw;
    }
</style>
<h1 style="text-align: center; font-size: 48px">WELCOME TO
    <?php echo $restorant_data['name'] . " - ORDER ID: " . $_SESSION['temp_table_orders_id']; ?>
</h1>

<hr style="border: 2px solid #333">

<h1 style="text-align: center; font-size: 48px">PLEASE SELECT ONE OF THE OPTIONS</h1>

<?php

$item_categories_res = $db->query("item_categories", "SELECT * FROM item_categories WHERE restorant_id = {$_SESSION['restorant_id']} ORDER BY name ASC");
while ($category = $item_categories_res->fetch_assoc()) {
    echo "<div class='menu-item'>
            <img src='icons/{$category['icon']}'>
            <a href='item_sub_category.php?item_category_id={$category['record_id']}'>{$category['name']}</a>
          </div>";
}


?>