<?php
include($_SERVER['DOCUMENT_ROOT'] . "/API-Others/connection/conn.php");
// error_reporting(E_ALL);
// ini_set('display_errors', '1');
header('content-type:application/json');
$target_dir = $_SERVER['DOCUMENT_ROOT'] . "/API-Others/uploads/profile-photos/";
$target = basename($_FILES["profile_photo"]["name"]);
$target_file = $target_dir . basename($_FILES["profile_photo"]["name"]);
// $check = getimagesize($_FILES["profile_photo"]["tmp_name"]);
if (move_uploaded_file($_FILES["profile_photo"]["tmp_name"], $target_file)) {
    $msg =  $target;
}
$image = $target;

foreach ($_POST['itemDescription'] as $key => $itemDescription) {
    $vendor = isset($_POST['vendor']) ? $_POST['vendor'] : '';
    $invoice = isset($_POST['invoice']) ? $_POST['invoice'] : '';
    $category = isset($_POST['category']) ? $_POST['category'] : '';
    $itemDescription = isset($_POST['itemDescription']) ? $_POST['itemDescription'] : '';
    $specification = isset($_POST['specification']) ? $_POST['specification'] : '';
    $unitofmeasurement = isset($_POST['unitofmeasurement']) ? $_POST['unitofmeasurement'] : '';
    $quantity = isset($_POST['quantity']) ? $_POST['quantity'] : '';
    $date = DATE("Y-m-d");
    $time = DATE("H:i:s");

    $category = mysqli_real_escape_string($conn, $category[$key]);
    $itemDescription = mysqli_real_escape_string($conn, $itemDescription[$key]);
    $specification =mysqli_real_escape_string($conn, $specification[$key]);
    $unitofmeasurement = mysqli_real_escape_string($conn, $unitofmeasurement[$key]);
    $quantity = mysqli_real_escape_string($conn, $quantity[$key]);
    if (!empty($itemDescription)) {
        $sql = ("INSERT INTO stocklist (image,vendor, invoice_no, category, item, specification, unitofmeasurement, quantity, Status, date, time) VALUES ('$image','$vendor', '$invoice','$category', '$itemDescription', '$specification', '$unitofmeasurement', '$quantity', 'Pending', '$date', '$time')");
        if (mysqli_query($conn, $sql)) {
            $statusCode = 200;
            $statusMessage = "Saved Successfully";
        } else {
            $error = "Error: " . mysqli_error($conn);
        }
    }
}




// foreach ($_POST['itemDescription'] as $key => $itemDescription) {
//     $vendor = stripslashes(isset($_POST['vendor']) ? $_POST['vendor'] : '');
//     $invoice = stripslashes(isset($_POST['invoice']) ? $_POST['invoice'] : '');
//     $category = stripslashes(isset($_GET['category']) ? $_GET['category'] : '');
//     $itemDescription = stripslashes(isset($_POST['itemDescription']) ? $_POST['itemDescription'] : '');
//     $unitofmeasurement = isset($_POST['unitofmeasurement']) ? $_POST['unitofmeasurement'] : '';
//     $date = DATE("Y-m-d");
//     $time = DATE("H:i:s");

//     $category = mysqli_real_escape_string($conn, $category[$key]);
//     $itemDescription = mysqli_real_escape_string($conn, $itemDescription[$key]);
//     $unitofmeasurement = mysqli_real_escape_string($conn, $unitofmeasurement[$key]);
//     if (!empty($$category)) {
//         $sql = ("INSERT INTO stocklist (vendor, invoice_no, category, item, unitofmeasurement, quantity, Status, date, time) VALUES ('$vendor', '$invoice','$category', '$itemDescription', '$unitofmeasurement', '$quantity', 'Pending', '$date', '$time')");
//         if (mysqli_query($conn, $sql)) {
//             $statusCode = 200;
//             $statusMessage = "Saved Successfully";
//         } else {
//             $error = "Error: " . mysqli_error($conn);
//         }
//     }
// }

// $vendor = addslashes(isset($_POST['vendor']) ? $_POST['vendor'] : '');
// $invoice = addslashes(isset($_POST['invoice']) ? $_POST['invoice'] : '');
// $category = addslashes(isset($_GET['category']) ? $_GET['category'] : '');
// $itemDescription = addslashes(isset($_POST['itemDescription']) ? $_POST['itemDescription'] : '');
// $unitofmeasurement = addslashes(isset($_POST['unitofmeasurement']) ? $_POST['unitofmeasurement'] : '');
// $quantity = addslashes(isset($_POST['quantity']) ? $_POST['quantity'] : '');
// $date = DATE("Y-m-d");
// $time = DATE("H:i:s");

// $sql = ("INSERT INTO stocklist (vendor, invoice_no, category, item, unitofmeasurement, quantity, Status, date, time) VALUES ('$vendor', '$invoice','$category', '$itemDescription', '$unitofmeasurement', '$quantity', 'Pending', '$date', '$time')");
// if (mysqli_query($conn, $sql)) {
//     $statusCode = 200;
//     $statusMessage = "Saved Successfully";
// } else {
//     $error = "Error: " . mysqli_error($conn);
// }

// $query = "SELECT * FROM config WHERE config_type_id = '$config_type_id' AND name = '$name'";
// $result = mysqli_query($conn, $query);
// $rowCount = mysqli_num_rows($result);
// if ($rowCount > 0) {
//     $statusCode = 1062;
//     $statusMessage = "Entry was not saved. Duplicate entry!";
// } else {
//     $sql = ("INSERT INTO config (config_type_id, name, category, amount) VALUES ('$config_type_id', '$name','$category', '$amount')");
//     if (mysqli_query($conn, $sql)) {
//         $statusCode = 200;
//         $statusMessage = "Saved Successfully";
//     } else {
//         $error = "Error: " . mysqli_error($conn);
//     }
// }

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