<?php
//error_reporting(E_ALL); 
//ini_set('display_errors', 1);
session_start();
include('functions/userfunctions.php');
include('config/dbcon.php');

include('includes/header.php');
include('authenticate.php');
$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']);
$totalPrice = mysqli_real_escape_string($con, $_POST['totalprice']);
$payment_id = mysqli_real_escape_string($con, $_POST['payment_id']);
$backupuser_id = mysqli_real_escape_string($con, $_POST['suid']);



// Generate unique tracking number
$tracking_no = "EX" . rand(1111, 9999) . substr($phone, 2);
$user_id = $_SESSION['auth_user']['user_id'];
//$userIP = $_SERVER['REMOTE_ADDR'];
// Generate hash data for payment
$orderId = $tracking_no;
$track_no = $tracking_no;



$cartItems = getCartItems();
$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);
   }



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

   $deleteCartQueryRun = mysqli_query($con, $deleteCartQuery);
}else{
    echo "no insert query;";
}
    ?>


<div class="container py-5">
    <div class="card">
        <div class="card-body shadow">
            
                <div class="row">

                    <div class="col-12">
                            <h5 class="text-center text-success">Successful!</h5>
                            <hr>
                            <div class="row">
                                <div class="col-md-12 text-center">
                                   <h3>Your order is in process!</h3><br><br>
<?php
$updateOrder_query = "UPDATE orders SET status='2' WHERE tracking_no='$track_no' ";

$updateOrder_query_run = mysqli_query($con, $updateOrder_query);
                                            //print "Bankadan dönen cevap:" . json_encode($_POST);


                                            $orderData = checkTrackingNoValid($track_no);
                                            $data = mysqli_fetch_array($orderData);
                                            ?>
                                            <hr>
                                            <h4>Order Details</h4>

                            

                            <table class="table">

                                <thead>

                                    <tr>

                                        <th>Product</th>

                                       

                                        <th>Quantity</th>
                                        <th>Price</th>

                                    </tr>

                                </thead>

                                <tbody>

                                    <?php

                                        

                                        $order_query = "SELECT o.id as oid, o.tracking_no, o.user_id, oi.*,oi.qty as orderqty, p.* FROM orders o, order_items oi, products p WHERE oi.order_id=o.id AND p.id=oi.prod_id AND o.tracking_no='$track_no' ";

                                        $order_query_run = mysqli_query($con, $order_query);



                                        if(mysqli_num_rows($order_query_run)>0)

                                        {

                                            foreach($order_query_run as $item)

                                            {

                                                ?>

                                                    <tr>

                                                        <td class="align-middle">

                                                            <!-- ürün görseli

                                                                <img src="../uploads/<?= $item['image'] ?>" alt="<?= $item['name'] ?>" width="50px" height="50px">

                                                            -->

                                                            <!-- ürün ismi  -->

                                                            <?= $item['name'] ?>

                                                        </td>

                                                       

                                                        <td class="align-middle">

                                                            <?= $item['orderqty'] ?> x

                                                        </td>

                                                        <td class="align-middle" style="padding:0px;">

                                                            <?= $item['price'] ?> ₺

                                                        </td>

                                                    </tr>



                                                <?php

                                            }

                                        }

                                    ?>

                                </tbody>

                            </table>

                            

                            <h5>Total Price : <span class="fw-bold"><?= $data['total_price']; ?> ₺</span></h5>
<?php
    if($data['total_price']){
            $to = "info@exelsiorhotels.com";
            $subject = "Yeni Siparis Olusturuldu";
            $message = "<html><body>";
            $message = "<h3>Yeni Siparis Olusturuldu</h3>";
            $message .= "<p>İsim: $name</p>";
            $message .= "<p>Telefon: $phone</p>";
            $message .= "<p>Email: $email</p>";
            $message .= "<p>Oda/Masa: $pincode</p>";
            $message .= "<p>Not: $address</p>";
            $message .= "<p>Sipariş no: $track_no</p>";
            $message .= "<p>Tutar: $totalPrice ₺</p>";
            $message .= "<p>Ödeme Metodu: $payment_mode</p>";
                $ordermail_query = "SELECT o.id as oid, o.tracking_no, o.user_id, oi.*, oi.qty as orderqty, p.* FROM orders o, order_items oi, products p WHERE oi.order_id=o.id AND p.id=oi.prod_id AND o.tracking_no='$track_no'";
                $ordermail_query_run = mysqli_query($con, $ordermail_query);

            // Add the order details table to the email message
            
            $message .= "<table class='table'>";
            $message .= "<thead>";
            $message .= "<tr>";
            $message .= "<th>Product</th>";
            $message .= "<th>Quantity</th>";
            $message .= "<th>Unit Price</th>";
            $message .= "<th>Price</th>";
            $message .= "</tr>";
            $message .= "</thead>";
            $message .= "<tbody>";

            if (mysqli_num_rows($ordermail_query_run) > 0) {
                foreach ($ordermail_query_run as $item) {
                    $message .= "<tr>";
                    $message .= "<td class='align-middle'>" . $item['name'] . "</td>";
                    $message .= "<td class='align-middle'>" . $item['orderqty'] . "</td>";
                    $message .= "<td class='align-middle'>" . $item['price'] . " ₺</td>";
                    $message .= "<td class='align-middle'>" . ($item['price'] * $item['orderqty']) . " ₺</td>"; 
                    $message .= "</tr>";
                }
            }

            $message .= "</tbody>";
            $message .= "</table>";
            $message .= "</body></html>";
            $headers = "From: info@exelsiorhotels.com\r\n"; // Specify the sender's email address
            $headers .= "Cc: mustafabey_21@hotmail.com\r\n";
            //$headers .= "Cc: fb@exelsiorhotels.com, $email\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
        $mailSent = mail($to, $subject, $message, $headers);
    }else{error_log('empty form sent.');}
    ?>

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