<?php
session_start();
include('includes/dbcon.php'); // Include your database connection

// Check if the cart is empty
if (!isset($_SESSION['cart']) || empty($_SESSION['cart'])) {
    die("Your cart is empty.");
}

// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Handle logged-in user
    if (isset($_SESSION['rm_id'])) {
        $cus_id = $_POST['cus_id'];
        $emp_id=$_SESSION['rm_emp_id'];
        // Validate customer ID
        if (empty($cus_id)) {
            die("Please select a customer.");
        }
    }
    // Handle guest user
    else {
        $cus_name = $_POST['cus_name'];
        $cus_email = $_POST['cus_email'];
        $cus_contact = $_POST['cus_contact'];
        $cus_address = $_POST['cus_address'];

        // Validate guest details
        if (empty($cus_name) || empty($cus_email) || empty($cus_contact) || empty($cus_address)) {
            die("Please fill in all customer details.");
        }

        // Set cus_id to 0 (or a guest customer ID)
        $cus_id = 0;

        // Send email to damvict@gmail.com
        $to = "damvict@gmail.com";
        $subject = "New Order from Guest Customer";
        $message = "Customer Name: $cus_name\n";
        $message .= "Email: $cus_email\n";
        $message .= "Contact: $cus_contact\n";
        $message .= "Address: $cus_address\n";
        $message .= "Order Details:\n";

        foreach ($_SESSION['cart'] as $item_id => $item) {
            $message .= "Product: {$item['item_name']}, Quantity: {$item['quantity']}, Price: {$item['sales_price']}\n";
        }

        $headers = "From: no-reply@yourwebsite.com";
        mail($to, $subject, $message, $headers);
    }

    // Save the cart to inv_temp_header
mysqli_begin_transaction($con);
    $querydoc=mysqli_query($con,"select * from sequence where seq_id=5 ")or die(mysqli_error());
while($row=mysqli_fetch_array($querydoc)){  
$inv_no=$row['cno'];
}

    $invd_date = date('Y-m-d H:i:s'); // Current date and time
    $inv_type = 1; // Assuming inv_type is always 1

    // Insert into inv_temp_header
    $query = "INSERT INTO inv_temp_header (invd_id,invd_date, cus_id, inv_type,loc_id,discount,emp_id) VALUES ($inv_no,'$invd_date', '$cus_id', '$inv_type',0,0,$emp_id)";
    mysqli_query($con, $query);
//echo $query;
    // Get the last inserted ID
    //$invd_id = mysqli_insert_id($con);

    // Save cart items to inv_temp_details (if you have such a table)
    foreach ($_SESSION['cart'] as $item_id => $item) {
        $item_name = $item['item_name'];
        $sales_price = $item['sales_price'];
        $quantity = $item['quantity'];

        $query = "INSERT INTO inv_temp_detail (invd_id, item_id, sales_price, qty,item_dis,qoh) VALUES ('$inv_no', '$item_id',  '$sales_price', '$quantity',0,0)";
        mysqli_query($con, $query);
        //echo $query;
    }

    // Clear the cart
    unset($_SESSION['cart']);
if ( mysqli_query($con,"update sequence set cno =cno + 1  where seq_id=5  ")or die(mysqli_error($con)))         
                        {   mysqli_commit($con); }
                         header("Location: cart.php");
    // Redirect to a success page
    ////header("Location: order_success.php");
    exit();
} else {
    // If the form is not submitted, redirect to the cart page
    header("Location: cart.php");
    exit();
}
?>