<?php
include "db.class.php";
$db = new DBMain();


if (isset($_FILES["fileToUpload"]) && $_FILES["fileToUpload"]["error"] != 4) {
    $target_dir = "assets/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

    // Allow certain file formats
    if (
        $imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" && $imageFileType != "pdf" && $imageFileType != "excel"
    ) {
        echo "Sorry, only JPG, JPEG, PNG, GIF, pdf, excel files are allowed.";
        $uploadOk = 0;
    }

    $file_name = $_POST['record_id'] . "." . $imageFileType;


    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir . "/" . $file_name)) {
            echo "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded.";
        }
        echo "Sorry, there was an error uploading your file.";
    }

    $db->Update('jobcards', [
        'jobcard_date',
        'artist_name',
        'sculpture_name',
        'account_no',
        'quantity',
        'serial_nr',
        'inv_nr',
        'wax_paint_dep_painter_name',
        'wax_paint_dep_date_in',
        'wax_paint_dep_date_out',
        'wax_work_dep_name',
        'wax_work_dep_date_in',
        'wax_work_dep_date_out',
        'wax_work_dep_wax_kg',
        'wax_runner_dep_name',
        'wax_runner_dep_date_in',
        'wax_runner_dep_date_out',
        'ceramics_dep_name',
        'ceramics_dep_date_in',
        'ceramics_dep_date_out',
        'dewax_dep_name',
        'dewax_dep_date_in',
        'dewax_dep_date_out',
        'casting_dept_name',
        'casting_dep_date_in',
        'casting_dep_date_out',
        'sandblast_dep_name',
        'sandblast_dep_date_in',
        'sandblast_dep_date_out',
        'metal_dep_name',
        'metal_dep_date_in',
        'metal_dep_date_out',
        'metal_dep_bronze_kg',
        'drill_tap',
        'leveld',
        'patina_dept_name',
        'patina_dept_date_in',
        'patina_dept_date_out',
        'patina_type',
        'base',
        'height',
        'width',
        'length',
        'notes',
        'deadline',
        'date_out',
        'file_name'
    ], [
        $_POST['jobcard_date'],
        $_POST['artist_name'],
        $_POST['sculpture_name'],
        $_POST['account_no'],
        $_POST['quantity'],
        $_POST['serial_nr'],
        $_POST['inv_nr'],
        $_POST['wax_paint_dep_painter_name'],
        $_POST['wax_paint_dep_date_in'],
        $_POST['wax_paint_dep_date_out'],
        $_POST['wax_work_dep_name'],
        $_POST['wax_work_dep_date_in'],
        $_POST['wax_work_dep_date_out'],
        $_POST['wax_work_dep_wax_kg'],
        $_POST['wax_runner_dep_name'],
        $_POST['wax_runner_dep_date_in'],
        $_POST['wax_runner_dep_date_out'],
        $_POST['ceramics_dep_name'],
        $_POST['ceramics_dep_date_in'],
        $_POST['ceramics_dep_date_out'],
        $_POST['dewax_dep_name'],
        $_POST['dewax_dep_date_in'],
        $_POST['dewax_dep_date_out'],
        $_POST['casting_dept_name'],
        $_POST['casting_dep_date_in'],
        $_POST['casting_dep_date_out'],
        $_POST['sandblast_dep_name'],
        $_POST['sandblast_dep_date_in'],
        $_POST['sandblast_dep_date_out'],
        $_POST['metal_dep_name'],
        $_POST['metal_dep_date_in'],
        $_POST['metal_dep_date_out'],
        $_POST['metal_dep_bronze_kg'],
        $_POST['drill_tap'],
        $_POST['leveld'],
        $_POST['patina_dept_name'],
        $_POST['patina_dept_date_in'],
        $_POST['patina_dept_date_out'],
        $_POST['patina_type'],
        $_POST['base'],
        $_POST['height'],
        $_POST['width'],
        $_POST['length'],
        $_POST['notes'],
        $_POST['deadline'],
        $_POST['date_out'],
        $file_name
    ], "record_id = {$_POST['record_id']}");

    echo "<script>window.location.href='open_jobcards.php';</script>";
} else {
    $db->Update('jobcards', [
        'jobcard_date',
        'artist_name',
        'sculpture_name',
        'account_no',
        'quantity',
        'serial_nr',
        'inv_nr',
        'wax_paint_dep_painter_name',
        'wax_paint_dep_date_in',
        'wax_paint_dep_date_out',
        'wax_work_dep_name',
        'wax_work_dep_date_in',
        'wax_work_dep_date_out',
        'wax_work_dep_wax_kg',
        'wax_runner_dep_name',
        'wax_runner_dep_date_in',
        'wax_runner_dep_date_out',
        'ceramics_dep_name',
        'ceramics_dep_date_in',
        'ceramics_dep_date_out',
        'dewax_dep_name',
        'dewax_dep_date_in',
        'dewax_dep_date_out',
        'casting_dept_name',
        'casting_dep_date_in',
        'casting_dep_date_out',
        'sandblast_dep_name',
        'sandblast_dep_date_in',
        'sandblast_dep_date_out',
        'metal_dep_name',
        'metal_dep_date_in',
        'metal_dep_date_out',
        'metal_dep_bronze_kg',
        'drill_tap',
        'leveld',
        'patina_dept_name',
        'patina_dept_date_in',
        'patina_dept_date_out',
        'patina_type',
        'base',
        'height',
        'width',
        'length',
        'notes',
        'deadline',
        'date_out'
    ], [
        $_POST['jobcard_date'],
        $_POST['artist_name'],
        $_POST['sculpture_name'],
        $_POST['account_no'],
        $_POST['quantity'],
        $_POST['serial_nr'],
        $_POST['inv_nr'],
        $_POST['wax_paint_dep_painter_name'],
        $_POST['wax_paint_dep_date_in'],
        $_POST['wax_paint_dep_date_out'],
        $_POST['wax_work_dep_name'],
        $_POST['wax_work_dep_date_in'],
        $_POST['wax_work_dep_date_out'],
        $_POST['wax_work_dep_wax_kg'],
        $_POST['wax_runner_dep_name'],
        $_POST['wax_runner_dep_date_in'],
        $_POST['wax_runner_dep_date_out'],
        $_POST['ceramics_dep_name'],
        $_POST['ceramics_dep_date_in'],
        $_POST['ceramics_dep_date_out'],
        $_POST['dewax_dep_name'],
        $_POST['dewax_dep_date_in'],
        $_POST['dewax_dep_date_out'],
        $_POST['casting_dept_name'],
        $_POST['casting_dep_date_in'],
        $_POST['casting_dep_date_out'],
        $_POST['sandblast_dep_name'],
        $_POST['sandblast_dep_date_in'],
        $_POST['sandblast_dep_date_out'],
        $_POST['metal_dep_name'],
        $_POST['metal_dep_date_in'],
        $_POST['metal_dep_date_out'],
        $_POST['metal_dep_bronze_kg'],
        $_POST['drill_tap'],
        $_POST['leveld'],
        $_POST['patina_dept_name'],
        $_POST['patina_dept_date_in'],
        $_POST['patina_dept_date_out'],
        $_POST['patina_type'],
        $_POST['base'],
        $_POST['height'],
        $_POST['width'],
        $_POST['length'],
        $_POST['notes'],
        $_POST['deadline'],
        $_POST['date_out']
    ], "record_id = {$_POST['record_id']}");

    echo "<script>window.location.href='open_jobcards.php';</script>";
}
