<?php

require 'connect.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $product_information = array();
    $targetDirectory = "./UPLOADED_IMAGES/";

    foreach ($_POST as $key => $value) {
        //echo $key.' '.$value.'KEY: <br>';
        if(isset($_POST[$key]) && $_POST[$key] == 'on') {

            //echo $_POST[$key].": ".$key." ".$_POST[$key.'-value']."<br>";
            if(!empty($_POST[$key.'-value'])) {
                $product_information[$key] = $_POST[$key.'-value'];
            }
            else {
                $product_information[$key] = NULL;
            }
        }
        else if(isset($_POST[$key]) && (!empty($value) || $value == '0')) {
            $product_information[$key] = $value;
        }
    }

    foreach($_FILES['images']['name'] as $key => $name) {
        if(!empty($name)) {
            $tmpName = $_FILES['images']['tmp_name'][$key];
            $fileName = basename($name);

            $targetFilePath = $targetDirectory . $fileName;

            if (move_uploaded_file($tmpName, $targetFilePath)) {
                echo "File $fileName uploaded successfully.<br>";
                $product_information['image'.$key+1] = "'$targetFilePath'";
            } else {
                $product_information['image'.$key+1] = NULL;
                echo "Failed to upload $fileName.<br>";
            }
        }
    }

    $stmt = $conn->prepare("INSERT INTO products (
            name,
            price,
            discount,
            in_stock,
            category,
            description,
            image1,
            image2,
            image3,
            image4,
            brand,
            series,
            model_number,
            weight, 
            dimensions, 
            color, 
            manufactuer, 
            origin, 
            firstDate, 
            energy, 
            volts, 
            memorySpeed, 
            usb3, 
            usb2, 
            processorBrand, 
            numProcess, 
            typeMem, 
            HardDD, 
            HardPlat, 
            HardFlash, 
            HardInterface, 
            gpuRAM, 
            motherboardRam, 
            motherboardTYPE, 
            screenSize, 
            screenRes, 
            screenMax
        )

        VALUES (
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?, 
            ?
        );
        "
    );

    if($product_information['in_stock'] == "true") {
        $product_information['in_stock'] = 1;
    }
    else {
        $product_information['in_stock'] = 0;
    }

    $stmt->bind_param(
        "ssdissssssssssssssssiiiisisssssiissss", 
        $product_information["name"], $product_information["price"], $product_information["discount"], $product_information["in_stock"],
        $product_information["category"], $product_information["description"], $product_information["image1"], $product_information["image2"],
        $product_information["image3"], $product_information["image4"], $product_information["brand"], $product_information["series"], 
        $product_information["model_number"], $product_information["weight"], $product_information["dimensions"], $product_information["color"], 
        $product_information["manufactuer"], $product_information["origin"], $product_information["firstDate"], 
        $product_information["energy"], $product_information["volts"], $product_information["memorySpeed"], $product_information["usb3"], $product_information["usb2"], 
        $product_information["processorBrand"], $product_information["numProcess"], 
        $product_information["typeMem"], $product_information["HardDD"], $product_information["HardPlat"], $product_information["HardFlash"], 
        $product_information["HardInterface"], $product_information["gpuRAM"], 
        $product_information["motherboardRAM"], $product_information["motherboardTYPE"], $product_information["screenSize"], $product_information["screenRes"], 
        $product_information["screenMax"]
    );

    if ($stmt->execute()) {
        echo "<p><font color='green'>Data updated successfully!</font></p>";
    } else {
        echo "<p><font color='red'>Error updating data: " . $conn->error . "</font></p>";
    }

    $conn->close();
}
