<?php



require 'userfunctions.php';

include('../config/dbcon.php');




error_reporting(E_ALL);
ini_set('display_errors', 1);


if(isset($_SESSION['auth']))

{



    if(isset($_POST['placeOrderBtn']))

    {

        $name = mysqli_real_escape_string($con, $_POST['name']);

        $email = mysqli_real_escape_string($con, $_POST['email']);

        $phone = mysqli_real_escape_string($con, $_POST['phone']);

        $pincode = mysqli_real_escape_string($con, $_POST['pincode']);

        $address = mysqli_real_escape_string($con, $_POST['address']);

        $payment_mode = mysqli_real_escape_string($con, $_POST['payment_mode']);

        $payment_id = mysqli_real_escape_string($con, $_POST['payment_id']);



        if($name == "" || $email == "" || $phone == "" || $pincode == "" || $address == "")

        {

            $_SESSION['message'] = "All fields are required";

            header('Location: ../checkout.php');

            exit(0);

        }



        $cartItems = getCartItems();

        $totalPrice = 0;

                                foreach($cartItems as $citem)

                                {

                                    $totalPrice += $citem['selling_price'] * $citem['prod_qty'];

                                }

        $tracking_no = "yaka".rand(1111,9999).substr($phone,2);

        $user_id = $_SESSION['auth_user']['user_id'];

        $insert_query = "INSERT INTO `orders`(tracking_no, user_id, name, email, phone, address, pincode, total_price, payment_mode, payment_id) VALUES ('$tracking_no', '$user_id', '$name', '$email', '$phone', '$address', '$pincode', '$totalPrice', '$payment_mode', '$payment_id' )";

        $insert_query_run = mysqli_query($con, $insert_query);



        if($insert_query_run)

        {

            $order_id = mysqli_insert_id($con);

            foreach($cartItems as $citem)

            {

                

                $prod_id = $citem['prod_id'];

                $prod_qty = $citem['prod_qty'];

                $price = $citem['selling_price'];

            $insert_items_query = "INSERT INTO order_items (order_id, prod_id, qty, price) VALUES ('$order_id', '$prod_id', '$prod_qty', '$price')";

            $insert_items_query_run = mysqli_query($con, $insert_items_query);





            //ürünlere qty olarak stok girildiyse sipariş verildikçe stoktan düşmesi için güncelleme

            //$product_query = "SELECT * FROM products WHERE id = '$prod_id' LIMIT 1 ";

            //$product_query_run = mysqli_query($con, $product_query);



            //$productData = mysqli_fetch_array($product_query_run);

            //$current_qty = $productData['qty'];

            //$new_qty = $current_qty - $prod_qty;



            //$updateQty_query = "UPDATE products SET qty = '$new_qty' WHERE id = '$prod_id' ";

            //$updateQty_query_run = mysqli_query($con, $updateQty_query);

            //stok güncelleme sonu

            }



            $deleteCartQuery = "DELETE FROM carts WHERE user_id = '$user_id'";

            $deleteCartQueryRun = mysqli_query($con, $deleteCartQuery);



            $_SESSION['message'] = "Order Placed Successfully";
            header('Location: ../my-orders.php');
            exit();

        }

    }



}

else

{

    header('Location: ../index.php');

}



?>