<?php
include "../../root.class.php";

$db = new db_safeguard();
var_dump($_POST);

$date = date("Y-m-d H:i", strtotime("+2 hours"));

$order_no = $_POST['order_no'];
$client_id = $_POST['client_id'];
$order_date = $_POST['order_date'];
$date_created = $date;

echo "INSERT INTO orders (order_no, client_id, order_date, date_created) VALUES ('$order_no', '$client_id', '$order_date', '$date_created')" . "\n";

$order_id = $db->query("orders", "INSERT INTO orders (order_no, client_id, order_date, date_created) VALUES ('$order_no', '$client_id', '$order_date', '$date_created')");

echo "Order ID: " . $order_id . "\n";

$index = 0;
$order_items_arry = [];
$counter = $_POST['counter'];

while ($index < $counter) {
    if (strlen($_POST['color_code_' . $index]) > 0 && strlen($_POST['quantity_' . $index]) > 0) {
        if (strlen($_POST['quantity_' . $index]) > 0 && $_POST['total_' . $index] > 0) {
            echo "INSERT INTO `order_items`(`record_id`, `color_code`, `discription`, `quantity`, `size`, `panels`, `amount`, `total`) VALUES ('$order_id', '{$_POST['color_code_' . $index]}', '{$_POST[discription_ . $index]}', '{$_POST[quantity_ . $index]}', '{$_POST[size_ . $index]}', '{$_POST[panels_ . $index]}', '{$_POST[amount_ . $index]}', '{$_POST[total_ . $index]}')" . "\n";

            $order_items_id = $db->query("order_items","INSERT INTO `order_items`(`record_id`, `color_code`, `discription`, `quantity`, `size`, `panels`, `amount`, `total`) VALUES ('$order_id', '{$_POST['color_code_' . $index]}', '{$_POST[discription_ . $index]}', '{$_POST[quantity_ . $index]}', '{$_POST[size_ . $index]}', '{$_POST[panels_ . $index]}', '{$_POST[amount_ . $index]}', '{$_POST[total_ . $index]}')");
        }
    }
    $index++;
}

echo "<script>location.href = 'home.php';</script>";