<?php include('partials-front/menu.php'); ?>
<head>
<link rel="stylesheet" href="css/styles.css">
</head>

<?php 
    //CHeck whether food id is set or not
    if(isset($_GET['food_id']))
    {
        //Get the Food id and details of the selected food
        $food_id = $_GET['food_id'];

        //Get the DEtails of the SElected Food
        $sql = "SELECT * FROM tbl_food WHERE id=$food_id";
        //Execute the Query
        $res = mysqli_query($conn, $sql);
        //Count the rows
        $count = mysqli_num_rows($res);
        //CHeck whether the data is available or not
        if($count==1)
        {
            //WE Have DAta
            //GEt the Data from Database
            $row = mysqli_fetch_assoc($res);

            $title = $row['title'];
            $price = $row['price'];
            $image_name = $row['image_name'];
        }
        else
        {
            //Food not Availabe
            //REdirect to Home Page
            header('location:'.SITEURL);
        }
    }
    else
    {
        //Redirect to homepage
        header('location:'.SITEURL);
    }
?>

<!-- fOOD sEARCH Section Starts Here -->
<section class="food-search2" style="background-image: url(images/bg.jpg)">
    <div class="container">
        
        <h2 class="text-center text-white">املأ هذا النموذج لتاكيد طلبك.</h2>

        <form action="" method="POST" class="order">
            <fieldset>

                <div class="food-menu-img">
                    <?php 
                        //CHeck whether the image is available or not
                        if($image_name=="")
                        {
                            //Image not Availabe
                            echo "<div class='error'>االصورة غير متاحة.</div>";
                        }
                        else
                        {
                            //Image is Available
                            ?>
                            <img src="<?php echo SITEURL; ?>images/food/<?php echo $image_name; ?>" alt="Chicke Hawain Pizza" class="img-responsive img-curve">
                            <?php
                        }
                    ?>
                </div>

                <div class="food-menu-desc">
                    <h3><?php echo $title; ?></h3>
                    <input type="hidden" name="food" value="<?php echo $title; ?>">

                    <p class="food-price">$<?php echo $price; ?></p>
                    <input type="hidden" name="price" value="<?php echo $price; ?>">

                    <div class="order-label">اختار الكمية</div>
                    <input type="number" name="qty" class="input-responsive" value="1" required>
                    
                </div>

            </fieldset>
            
            <fieldset>
                <legend>تفاصيل الطلب</legend>
                
                <div class="order-label">رقم الطاولة</div>
                <select name="table_number" class="input-responsive" required>
                    <option value="" disabled selected>اختر رقم الطاولة</option>
                    <?php
                        for ($i = 1; $i <= 20; $i++) { // عدّل الرقم حسب عدد الطاولات عندك
                            echo "<option value='$i'>$i</option>";
                        }
                    ?>
                </select>

                <div class="order-label">ملاحظات</div>
                <textarea name="notes" rows="4" placeholder="أي ملاحظات إضافية..." class="input-responsive"></textarea>

                <input type="submit" name="submit" value="تاكيد الطلب" class="btn btn-primary" style="margin-left: 243px;">
            </fieldset>

        </form>

        <?php 
            //CHeck whether submit button is clicked or not
            if(isset($_POST['submit']))
            {
                // Get all the details from the form
                $food = $_POST['food'];
                $price = $_POST['price'];
                $qty = $_POST['qty'];
                $table_number = $_POST['table_number'];
                $notes = $_POST['notes'];

                $total = $price * $qty; // total = price x qty 
                $order_date = date("Y-m-d h:i:sa"); //Order DAte
                $status = "Ordered";  // Ordered, On Delivery, Delivered, Cancelled

                // حفظ رقم الطاولة في customer_name والملاحظات في customer_address
                $sql2 = "INSERT INTO tbl_order SET 
                    food = '$food',
                    price = $price,
                    qty = $qty,
                    total = $total,
                    order_date = '$order_date',
                    status = '$status',
                    customer_name = '$table_number',
                    customer_contact = '',
                    customer_email = '',
                    customer_address = '$notes'
                ";

                $res2 = mysqli_query($conn, $sql2);

                if($res2==true)
                {
                    // إضافة الطلب لجدول المطبخ
                    $order_id = mysqli_insert_id($conn);
                    $sql_kitchen = "INSERT INTO kitchen_orders (order_id) VALUES ('$order_id')";
                    mysqli_query($conn, $sql_kitchen);

                    $_SESSION['order'] = "<div class='success text-center'>تم الطلب بنجاح.</div>";
                    header('location:'.SITEURL);
                    exit;
                }
                else
                {
                    $_SESSION['order'] = "<div class='error text-center'>فشل في اتمام الطلب.</div>";
                    header('location:'.SITEURL);
                    exit;
                }
            }
        ?>

    </div>
</section>
<!-- fOOD sEARCH Section Ends Here -->

<?php include('partials-front/footer.php'); ?>
