<?php
header('content-type:application/json');
include($_SERVER['DOCUMENT_ROOT'] . "/API-Others/connection/conn.php");
// $value = array_combine($_POST['rawMaterial'], $_POST['productionWarehouse'], $_POST['frameType'], $_POST['frameSize'], $_POST['productionLevel'], $_POST['quantityNeeded']);


// foreach ($_POST['rawMaterial'] as $index => $rawmaterial) {
//     $productionwarehouse = $_POST['productionWarehouse'][$index];
//     $frametype = $_POST['frameType'][$index];
//     $framesize = $_POST['frameSize'][$index];
//     $productionlevel = $_POST['productionLevel'][$index];
//     $quantityneeded = $_POST['quantityNeeded'][$index];
//     $date = DATE("Y-m-d");
//     $time = DATE("H:i:s");


//     $sql = ("INSERT INTO production_mix (productionwarehouse, frametype, framesize, productionlevel, rawmaterial, quantityneeded, date, time) VALUES ('$productionwarehouse','$frametype', '$framesize', '$productionlevel','$rawmaterial','$quantityneeded', '$date', '$time')");
//     if (mysqli_query($conn, $sql)) {
//         $statusCode = 200;
//         $statusMessage = "Saved Successfully";
//     } else {
//         $error = "Error: " . mysqli_error($conn);
//     }
// }




// foreach ($_POST['rawMaterial'] as $key => $rawmaterial) {
//     $productionwarehouse = isset($_POST['productionWarehouse']) ? $_POST['productionWarehouse'] : '';
//     $frametype = isset($_POST['frameType']) ? $_POST['frameType'] : '';
//     $framesize = isset($_POST['frameSize']) ? $_POST['frameSize'] : '';
//     $productionlevel = isset($_POST['productionLevel']) ? $_POST['productionLevel'] : '';
//     $quantityneeded = isset($_POST['quantityNeeded']) ? $_POST['quantityNeeded'] : '';
//     $date = DATE("Y-m-d");
//     $time = DATE("H:i:s");

//     $productionwarehouse = mysqli_real_escape_string($conn, $productionwarehouse[$key]);
//     $frametype = mysqli_real_escape_string($conn, $frametype[$key]);
//     $framesize = mysqli_real_escape_string($conn, $framesize[$key]);
//     $productionlevel = mysqli_real_escape_string($conn, $productionlevel[$key]);
//     $quantityneeded = mysqli_real_escape_string($conn, $quantityneeded[$key]);

//     $sql = ("INSERT INTO production_mix (productionwarehouse, frametype, framesize, productionlevel, rawmaterial, quantityneeded, date, time) VALUES ('$productionwarehouse','$frametype', '$framesize', '$productionlevel','$rawmaterial','$quantityneeded', '$date', '$time')");
//     if (mysqli_query($conn, $sql)) {
//         $statusCode = 200;
//         $statusMessage = "Saved Successfully";
//     } else {
//         $error = "Error: " . mysqli_error($conn);
//     }
// }




// foreach ($_POST['rawMaterial'] as $key => $rawmaterial) {
//     $productionwarehouse = isset($_POST['productionWarehouse']) ? $_POST['productionWarehouse'] : '';
//     $frametype = isset($_POST['frameType']) ? $_POST['frameType'] : '';
//     $framesize = isset($_POST['frameSize']) ? $_POST['frameSize'] : '';
//     $productionlevel = isset($_POST['productionLevel']) ? $_POST['productionLevel'] : '';
//     $quantityneeded = isset($_POST['quantityNeeded']) ? $_POST['quantityNeeded'] : '';
//     $date = DATE("Y-m-d");
//     $time = DATE("H:i:s");

//     $quantityneeded = mysqli_real_escape_string($conn, $quantityneeded[$key]);

//     $sql = ("INSERT INTO production_mix (productionwarehouse, frametype, framesize, productionlevel, rawmaterial, quantityneeded, date, time) VALUES ('$productionwarehouse','$frametype', '$framesize', '$productionlevel','$rawmaterial','$quantityneeded', '$date', '$time')");
//     if (mysqli_query($conn, $sql)) {
//         $statusCode = 200;
//         $statusMessage = "Saved Successfully";
//     } else {
//         $error = "Error: " . mysqli_error($conn);
//     }
// }

function entryId()
{
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 6; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}
$entryId = entryId();

foreach ($_POST['productionWarehouse'] as $key => $productionwarehouse) {
    $rawmaterial = isset($_POST['rawMaterial']) ? $_POST['rawMaterial'] : '';
    $frametype = isset($_POST['frameType']) ? $_POST['frameType'] : '';
    $framesize = isset($_POST['frameSize']) ? $_POST['frameSize'] : '';
    $productionlevel = isset($_POST['productionLevel']) ? $_POST['productionLevel'] : '';
    $quantityneeded = isset($_POST['quantityNeeded']) ? $_POST['quantityNeeded'] : '';
    $date = DATE("Y-m-d");
    $time = DATE("H:i:s");


    $rawmaterial = mysqli_real_escape_string($conn, $rawmaterial[$key]);
    $frametype = mysqli_real_escape_string($conn, $frametype[$key]);
    $framesize = mysqli_real_escape_string($conn, $framesize[$key]);
    $productionlevel = mysqli_real_escape_string($conn, $productionlevel[$key]);
    $quantityneeded = mysqli_real_escape_string($conn, $quantityneeded[$key]);

    $sql = ("INSERT INTO production_mix (entryKey, entryId, productionwarehouse, frametype, framesize, productionlevel, rawmaterial, quantityneeded, date, time) VALUES ('Production Warehouse','$entryId','$productionwarehouse','$frametype', '$framesize', '$productionlevel','$rawmaterial','$quantityneeded', '$date', '$time')");
    if (mysqli_query($conn, $sql)) {
        $statusCode = 200;
        $statusMessage = "Saved Successfully";
    } else {
        $error = "Error: " . mysqli_error($conn);
    }
}


$query = mysqli_query($conn, "SELECT * FROM production_mix WHERE entryKey = 'Production Warehouse' AND entryId = '$entryId' AND frameType != ''");
while ($spec = mysqli_fetch_assoc($query)) {

    $sql = ("UPDATE production_mix SET frameType = '".$spec["frameType"]."', framesize  = '".$spec["framesize"]."', productionlevel  = '".$spec["productionlevel"]."', rawmaterial  = '".$spec["rawmaterial"]."', quantityneeded = '".$spec["quantityneeded"]."' WHERE entryKey = 'Production Warehouse' AND entryId = '$entryId' AND frameType = ''");
    if (mysqli_query($conn, $sql)) {
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}




$resultData = array('code' => $statusCode, 'message' => $statusMessage);
echo json_encode($resultData);
