<?php include('partials/menu.php'); ?>

<div class="main-content">
    <div class="wrapper">
        <h1>تحديث الطلب</h1>
        <br><br>

        <?php 
            // تحقق من وجود id في الرابط
            if(isset($_GET['id']))
            {
                $id = $_GET['id'];
                $sql = "SELECT * FROM tbl_order WHERE id=$id";
                $res = mysqli_query($conn, $sql);
                $count = mysqli_num_rows($res);

                if($count==1)
                {
                    $row = mysqli_fetch_assoc($res);

                    $food = $row['food'];
                    $price = $row['price'];
                    $qty = $row['qty'];
                    $status = $row['status'];
                    $customer_name = $row['customer_name'];
                    $customer_address = $row['customer_address'];
                }
                else
                {
                    header('location:'.SITEURL.'admin/manage-order.php');
                }
            }
            else
            {
                header('location:'.SITEURL.'admin/manage-order.php');
            }
        ?>

        <form action="" method="POST">
            <table class="tbl-30">
                <tr>
                    <td>اسم الوجبة</td>
                    <td><b><?php echo $food; ?></b></td>
                </tr>

                <tr>
                    <td>السعر</td>
                    <td>
                        <b>$<?php echo $price; ?></b>
                    </td>
                </tr>

                <tr>
                    <td>الكمية</td>
                    <td>
                        <input type="number" name="qty" value="<?php echo $qty; ?>">
                    </td>
                </tr>

                <tr>
    <td>حالة الطلب</td>
    <td>
        <select name="status" class="big-select">
            <option <?php if($status=="Ordered"){echo "selected";} ?> value="Ordered">جاري التجهيز</option>
            <option <?php if($status=="Delivered"){echo "selected";} ?> value="Delivered">تم التسليم</option>
            <option <?php if($status=="Cancelled"){echo "selected";} ?> value="Cancelled">الغاء الطلب</option>
        </select>
    </td>
</tr>

                <tr>
                    <td>رقم الطاولة:</td>
                    <td>
                        <input type="text" name="customer_name" value="<?php echo htmlspecialchars($customer_name); ?>">
                    </td>
                </tr>

                <tr>
                    <td>ملاحظات:</td>
                    <td>
                        <textarea name="customer_address" cols="30" rows="5"><?php echo htmlspecialchars($customer_address); ?></textarea>
                    </td>
                </tr>

                <tr>
                    <td colspan="2">
                        <input type="hidden" name="id" value="<?php echo $id; ?>">
                        <input type="hidden" name="price" value="<?php echo $price; ?>">
                        <input type="submit" name="submit" value="تحديث الطلب" class="btn-secondary">
                    </td>
                </tr>
            </table>
        </form>

        <?php 
            // عند الضغط على زر التحديث
            if(isset($_POST['submit']))
            {
                $id = $_POST['id'];
                $price = $_POST['price'];
                $qty = $_POST['qty'];
                $total = $price * $qty;
                $status = $_POST['status'];
                $customer_name = $_POST['customer_name'];
                $customer_address = $_POST['customer_address'];

                // تحديث الطلب في قاعدة البيانات
                $sql2 = "UPDATE tbl_order SET 
                    qty = $qty,
                    total = $total,
                    status = '$status',
                    customer_name = '$customer_name',
                    customer_address = '$customer_address'
                    WHERE id=$id
                ";

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

                if($res2==true)
                {
                    $_SESSION['update'] = "<div class='success'>تم تحديث الطلب بنجاح.</div>";
                    header('location:'.SITEURL.'admin/manage-order.php');
                }
                else
                {
                    $_SESSION['update'] = "<div class='error'>فشل في تحديث الطلب.</div>";
                    header('location:'.SITEURL.'admin/manage-order.php');
                }
            }
        ?>
    </div>
</div>

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