<?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;
}

$Orders = new NewOrder();

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    // Check if all required fields are set and are arrays
    if(isset($_POST["category"], $_POST["product"], $_POST["variation"], $_POST["product_id"], $_POST["price"]) &&
       is_array($_POST["category"]) && is_array($_POST["product"]) && is_array($_POST["variation"]) &&
       is_array($_POST["product_id"]) && is_array($_POST["price"])) {

        // Get the values from the arrays
        $category = $_POST["category"];
        $product = $_POST['product'];
        $variation = $_POST['variation'];
        $product_id = $_POST['product_id'];
        $price = $_POST['price'];

        // Loop through each set of values and insert into the database
        for ($i = 0; $i < count($category); $i++) {
            // Escape values to prevent SQL injection
            $escaped_category = mysqli_real_escape_string($connection, $category[$i]);
            $escaped_product = mysqli_real_escape_string($connection, $product[$i]);
            $escaped_variation = mysqli_real_escape_string($connection, $variation[$i]);
            $escaped_product_id = mysqli_real_escape_string($connection, $product_id[$i]);
            $escaped_price = mysqli_real_escape_string($connection, $price[$i]);

            // Perform the insertion
            $query = "INSERT INTO procurement (category, product, variation, product_id, price) VALUES ('$escaped_category', '$escaped_product', '$escaped_variation', '$escaped_product_id', '$escaped_price')";
            $result = mysqli_query($connection, $query);

            if (!$result) {
                // If insertion fails, display error and exit loop
                echo "Error: " . mysqli_error($connection);
                break;
            }
        }

        // Check if all insertions were successful
        if ($i === count($category)) {
            echo "<script> alert('Added Successful'); </script>";
        } else {
            echo "<script> alert('Something went wrong'); </script>";
        }
    } else {
        echo "Error: One of the fields is not set or is not an array.";
    }
}
?>


<body>
    <div class="container_main">
    <button class="reset-button"><a href=""><i class="fa-solid fa-backward"></i></a></button>
        <form action="" method="POST">
            <div class="form-group">
            <h1>New Raw Material</h1>
 <div class="box_button" id="boxButton">
                   <button type="button" class="btn btn-outline-success" style="width:180px;">
                    <input type="text" class="form-control" placeholder="category" aria-label="Text input with segmented dropdown button" name="category[]" required>
                </button>
                <button type="button" class="btn btn-outline-success" style="width:180px;">
                    <input type="text" class="form-control" placeholder="product" aria-label="Text input with segmented dropdown button" name="product[]" required>
                </button>
                <button type="button" class="btn btn-outline-success" style="width:180px;">
                    <input type="text" class="form-control" placeholder="variation" aria-label="Text input with segmented dropdown button" name="variation[]" required>
                </button>
                <button type="button" class="btn btn-outline-success" style="width:180px;">
                    <input type="text" class="form-control" placeholder="product_id" aria-label="Text input with segmented dropdown button" name="product_id[]" required>
                </button>
                <button type="button" class="btn btn-outline-success" style="width:180px;">
                    <input type="text" class="form-control" placeholder="price" aria-label="Text input with segmented dropdown button" name="price[]" required>
                </button>
                    
</div>
<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">
    
      <button type="submit"name="submit"class="reset-button">Submit</button>
    </div> 


 </form>
 </div>
  </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>
