<?php
ob_start();
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');
// include('include/session.php');
$dbConnection = new DbConnection();
$connection = $dbConnection->connection;

$NewOrer = new NewOrder();
$randomNumber=$NewOrer->generateSequentialNumber();

// Fetch data from order_summary table
$orderSummaryQuery = "SELECT * FROM order_summery"; // Adjust query as per your table structure
$orderSummaryResult = mysqli_query($connection, $orderSummaryQuery);

// Check if query executed successfully
if ($orderSummaryResult) {
    $orderSummaryData = mysqli_fetch_assoc($orderSummaryResult);
} else {
    echo "Error fetching order summary data: " . mysqli_error($connection);
}
?>
<html lang="en">
<head>
    <link rel="stylesheet" href="assets/css/style.css">
<?php
include('include/header_links.php');

?>
</head>
<style>
    .pd_id{
        display:flex;
        
    }
</style>
<body>
    <div class="container_main">
    <div id="warantee_button">
                    <div> <button class="reset-button"><a href="_new_order_list.php"><i class="fa-solid fa-backward"></i></a></button></div>
                   
                </div>
        <form id="orderForm" action="new_order.php" method="POST">
            <div class="">
            <h1> Edit Order </h1>
            
              <input type="text" id="uid_generated" name="order_id" value="<?php echo $randomNumber;?>" hidden>
                 <h3>Client UID:
                <select data-live-search="true" name="cuid" id="clientUid" value="<?php echo $orderSummaryData['cuid']; ?>" required>
                <option  id= "option" value="">Select Client UID</option>
                 <option value="">Select Client UID</option>
                                <?php
                                    $result = mysqli_query($connection, "SELECT * FROM clients");
                            
                                    if ($result && mysqli_num_rows($result) > 0) {
                                        while ($row = mysqli_fetch_assoc($result)) {
                                            $selected = ($row['u_id'] == $orderSummaryData['cuid']) ? 'selected' : ''; // Changed to $orderSummaryData['cuid']
                                            echo '<option value="' . $row['u_id'] . '" ' . $selected . '>' . $row['u_id'] . '</option>';
                                        }
                                    }
                                ?>

            
                    </select>
             
</h3>


<div class="box_button" id="boxButton"  >
     <button type="button" class="btn btn-outline-success" style="width:300px">

        <select class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split category" value="<?php echo $orderSummaryData['category']; ?>" id ="category" name="category[]" style="width:280px">
               <option value=""> Select Category </option>
                <?php
                $sql = "SELECT category FROM category UNION SELECT category FROM procurement";
                $result = $connection->query($sql);
                
                if ($result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) {
                        $selected = ($row['category'] == $orderSummaryData['category']) ? 'selected' : '';
                        echo '<option value="' . $row['category'] . '" ' . $selected . '>' . $row['category'] . '</option>';
                    }
                }
                ?>
        </select>
    </button>
            <button type="button" class="btn btn-outline-success" style="width:300px">
                    <select class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split product" name="product[]" id="product" value="<?php echo $orderSummaryData['product']; ?>"style="width:280px">
                    <option value=""> Select product</option>
                     
                    <optgroup label="Product">
                        <?php
                        $sql = "SELECT product FROM product"; // corrected SQL query
                        $result = $connection->query($sql);
            
                        if ($result->num_rows > 0) {
                            while ($row = $result->fetch_assoc()) {
                                $selected = ($row['product'] == $orderSummaryData['product']) ? 'selected' : '';
                                echo '<option value="' . $row['product'] . '" ' . $selected . '>' . $row['product'] . '</option>';
                            }
                        }
                        ?>
                    </optgroup>
    </select>
                   
           </button>
        <button type="button" class="btn btn-outline-success"style="width:300px">
            <select class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split variation" name="variation[]" id="variation" value="<?php echo $orderSummaryData['variation']; ?>"style="width:280px">
                <option value=""> Select variation</option>
                <optgroup label="variation">
                    <?php
                        $sql = "SELECT variation FROM product_variation"; // corrected SQL query
                        $result = $connection->query($sql);
            
                        if ($result->num_rows > 0) {
                            while ($row = $result->fetch_assoc()) {
                                $selected = ($row['variation'] == $orderSummaryData['variation']) ? 'selected' : '';
                                echo '<option value="' . $row['variation'] . '" ' . $selected . '>' . $row['variation'] . '</option>';
                            }
                        }
                        ?>
                </optgroup>
            </select>
        </button><br><br>
               <center><input type="button" class="btn btn-info or" id="or" value="OR"></center><br><br>
             <div class="pd_id" id="">
                    <button type="button" class="btn btn-outline-success" style="width:100%">
                      <h2>Product Id</h2>
                            <input type="text" id="product_id" class="form-control  product_id" placeholder="Product Id" aria-label="Product_id" name="product_id[]" value="<?php echo $orderSummaryData['product_id']; ?>" value="" >
                           
                </button>
        </div><br><br>
        
        
        <div>
        <button type="button" class="btn btn-outline-success" style="width:480px">
        
            <input type="text" id="price" class="form-control price" placeholder="Price" aria-label="Price[]" name="price" value="<?php echo $orderSummaryData['price']; ?>" required>
            
        </button>
                   <div class="btn btn-outline-success" style="width:300px">
    <input type="text" id="Instock" class="form-control Instock" placeholder="Instock" aria-label="Stock" name="quantity[]" value="" required>
</div>


        <button type="button" class="btn btn-outline-success" style="width:480px">
                <input type="text" class="form-control quantity" placeholder="quantity" aria-label="Text input with segmented dropdown button" id="quantity" name="items[]" value="<?php echo $orderSummaryData['items']; ?>"required>
        </button>
</div></div><br>
<div id="clonedContainer"></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 total_amount"  id="totalAmount" placehold="total_amount" aria-label="Text input with segmented dropdown button" value="<?php echo $orderSummaryData['total_price']; ?>" name="total_price"required >
           <input type="text" class="form-control" placeholder="Payment Refrence" aria-label="Text input with segmented dropdown button" value="<?php echo $orderSummaryData['payment_reference']; ?>" name="payment_reference" required>
      </div>
        
        
        <div class="button">
        <button type="submit"  name="submit" class="reset-button">Submit</button>
        <button type="button" class="reset-button">Save</button>
            <button type="button" class="reset-button">Share</button>
        </div>
        </div>
        </div>
 </form>
 </div>
</body>
 
<?php 
 include("include/footer_link.php");
?>
<script src="assets/script/script.js"></script>
<script>
$(document).ready(function() {
    // Initialize Bootstrap Selectpicker
    $('#clientUid').selectpicker();
});

$(document).ready(function() {
    // Retrieve values of elements with class name "Instock"
    var quantityElements = document.getElementsByClassName("Instock");
    var quantityValues = [];
    for (var i = 0; i < quantityElements.length; i++) {
        quantityValues.push(quantityElements[i].value);
    }

    // Retrieve values of elements with class name "quantity"
    var itemsElements = document.getElementsByClassName("quantity");
    var itemsValues = [];
    for (var j = 0; j < itemsElements.length; j++) {
        itemsValues.push(itemsElements[j].value);
    }

    // Log the retrieved values
    console.log("Quantity values:", quantityValues);
    console.log("Items values:", itemsValues);
    
   var updatedValues=quantityValues-itemsValues;
   console.log(updatedValues)
});

</script>
</html>
<?php
ob_end_flush();
?>