<?php 
include('include/session.php');
// session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('include/navigation.php');
include('connection.php');
include('class.php');

$dbConnection = new DbConnection();
$connection = $dbConnection->connection;

if (isset($_SESSION["id"])) {
    header("Location: _order_list.php");
    exit;
}
echo var_dump($_POST);
$Orders = new NewOrder();
if ($_SERVER["REQUEST_METHOD"] === "POST") {
 
    $c_uid = $_POST["cuid"];
    
    $category = $_POST["category"];
    $product = $_POST['product'];
    $variation = $_POST['variation'];
    $price = $_POST['price'];
    $traully_number = $_POST["traully_number"];

   // Serialize the array of "Amount Receive" values
$serialized_category = is_array($category) ? implode(', ', array_values($category)) : $category;
$serialized_product = is_array($product) ? implode(', ', array_values($product)) : $product;
$serialized_variation = is_array($variation) ? implode(', ', array_values($variation)) : $variation;
$serialized_price = is_array($price) ? implode(', ', array_values($price)) : $price;



    // Insert the serialized array into the database
  $result = $Orders->SalesList($c_uid, $serialized_category, $serialized_product, $serialized_variation, $serialized_price,$traully_number);

    if ($result == 1) {
echo "<script> alert('Added Successful'); window.location.href = '_order_list.php'; </script>";

    } elseif ($result == 10) {
        echo "<script> alert('Something is wrong'); </script>";
    }
}
?>







<html lang="en">
<head>
    <link rel="stylesheet" href="assets/css/style.css">
<?php
include('include/header_links.php');

?>
<body>
    <div class="container_main">
           <div id="warantee_button">
                    <div class="item-left"><button class="reset-button"><a href="_stock_list.php"><i class="fa-solid fa-backward"></i></a></button></div>
                     <div class="reset-button item-right"><a href="_product_order_list.php">Order List</a></div>
                </div>
   
        <form action="" method="POST">
            <div class="form-group">
            <h1>Order List</h1>
         
                <h3>Client UID: 
                        <select data-live-search="true" name="cuid" id="clientUid" required >
                        <option value="">Select Client UID</option>
                        
                                <?php
                                $option = mysqli_query($connection, "SELECT * FROM clients");
                                while ($row = mysqli_fetch_array($option)) {
                                    echo '<option value="' . $row['u_id'] . '">' . $row['u_id'] . ' - ' . $row['firm_name'] . ' - ' . $row['address'] . '</option>';
                                }
                                ?>
                    
                        </select></h3><br><br>

                        <h1>Product List</h1>
<div class="box_button" id="boxButton">
     <button type="button" class="btn btn-outline-success">

         <select class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split category"  id ="category" name="category[]" required>
                <option value=""> Select Category </option>
            <?php
                        $sql = "SELECT category FROM category 
            UNION 
            SELECT category FROM procurement";

                        $result = $connection->query($sql);

                        // Check if there are rows in the result
                        if ($result->num_rows > 0) {
                            while ($row = $result->fetch_assoc()) {
                                echo '<option value="' . $row["category"] . '">' . $row["category"] . '</option>';
                            }
                        }
                    ?>
        </select>
    </button>
            <button type="button" class="btn btn-outline-success">
                    <select class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split product" name="product[]" id="product" required>
                    <option value=""> Select product</option>
                    <optgroup label="Product">
                        <!-- Options will be dynamically populated using JavaScript -->
                    </optgroup>
                   </select>
           </button>
        <button type="button" class="btn btn-outline-success">
            <select class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split variation" name="variation[]" id="variation" required>
                <option value=""> Select variation</option>
                <optgroup label="variation">
                    <!-- Options will be dynamically populated using JavaScript -->
                </optgroup>
            </select>
        </button>

        <button type="button" class="btn btn-outline-success" style="width:200px">
        
            <input type="text" id="price" class="form-control price" placeholder="Price" aria-label="Price" name="price" value="" required>
            
        </button>

</div>
<div id="clonedContainer"></div>
       
   </div>
       <div class="button" id="add_button">
       <button type="button" class="btn btn-info" id="addMore">Add More</button>
      </div>
      <div class="button">
       <input type="text" class="form-control"  id="traullyNo" placeholder="Traully Number" aria-label="Text input with segmented dropdown button" name="traully_number"required  style="width:70%">
       <button type="submit"name="submit"class="reset-button">Send For Invoicing</button>
       
       </div>
  </div>

 </form>
 </div>
  <section class="footer">
     <?php include('include/footer_two.php');         ?>
     
 </section>
</body>
 
<?php 
 include("include/footer_link.php");
  ?>
<script src="assets/script/script.js"></script>

</html>
