<?php
include "../../classes/autoload.php";
$db = new db();

// var_dump($_POST);
// var_dump($_FILES); // check uploaded files


$date = date("Y-m-d H:i", strtotime("+2 hours"));

if(isset($_POST['record_id'])){
    $job_card_id = $_POST['record_id'];
}

if (isset($_GET['attachments'])) {

    // // loop through uploaded files
    if (!empty($_FILES['attachment_file']['name'])) {
        foreach ($_FILES['attachment_file']['name'] as $index => $name) {
            if ($_FILES['attachment_file']['error'][$index] === UPLOAD_ERR_OK) {
                $tmpName = $_FILES['attachment_file']['tmp_name'][$index];
                $safeName = time() . "_" . basename($name);
                $uploadDir = "attachments/";  // make sure this folder exists and is writable
                move_uploaded_file($tmpName, $uploadDir . $safeName);
                // echo $index;
                // var_dump($_POST['attachment_description'][$index]);
                // Optionally: insert into DB
                $db->query("job_card_attachments", "INSERT INTO job_card_attachments (job_card_id, `description`, file_name,`date_time`,`user_id`,`file_category_id`,`file_sub_category_id`) VALUES ($job_card_id, '{$_POST['attachment_description'][$index]}', '$safeName','$date','{$_SESSION['user_id']}','{$_POST['file_category'][$index]}','{$_POST['file_subcategory'][$index]}')");
                // echo "INSERT INTO job_card_attachments (job_card_id, `description`, file_name,`date_time`,`user_id`,`file_category_id`,`file_sub_category_id`) VALUES ($job_card_id, '{$_POST['attachment_description'][$index]}', '$safeName','$date','{$_SESSION['user_id']}','{$_POST['file_category'][$index]}','{$_POST['file_subcategory'][$index]}')";
            }
        }
    }

    header("Location: job_card_attachments.php?record_id=$job_card_id");

} elseif (isset($_GET['attachment_notes'])) {

    $notes = $_POST['notes'];
    $db->query("attachment_notes", "INSERT INTO `attachment_notes` (`job_card_attachment_id`, `note`,  `user_id`) VALUES ({$_POST['job_card_attachment_id']}, '$notes',  '{$_SESSION['user_id']}');");
    header("Location: job_card_attachment_notes.php?record_id={$_POST['job_card_attachment_id']}");
} else {
    $costing_amount = str_replace(array('R', ','), '', $_POST["costing_amount"]);
    $invoice_amount = str_replace(array('R', ','), '', $_POST["invoice_amount"]);
    $quote_amount = str_replace(array('R', ','), '', $_POST["quote_amount"]);


    if ($_POST['type_enclosure'] == 'OTHER') {
        $type_enclosure = $_POST['other_enclosure'];
    } else {
        $type_enclosure = $_POST['type_enclosure'];
    }

    if ($_POST['lock_types'] == 'OTHER') {
        $lock_types = $_POST['other_lock_types'];
    } else {
        $lock_types = $_POST['lock_types'];
    }

    if ($_POST['colour'] == 'OTHER') {
        $colour = $_POST['other_colour'];
    } else {
        $colour = $_POST['colour'];
    }

    if ($_POST['control_voltage'] == 'OTHER') {
        $control_voltage = $_POST['other_control_voltage'];
    } else {
        $control_voltage = $_POST['control_voltage'];
    }

    if ($_POST['starter_type'] == 'OTHER') {
        $starter_type = $_POST['other_starter_type'];
    } else {
        $starter_type = $_POST['starter_type'];
    }

    if ($_POST['panel_base_size'] == 'OTHER') {
        $panel_base_size = $_POST['other_panel_base_size'];
    } else {
        $panel_base_size = $_POST['panel_base_size'];
    }

    $_POST['job_card_no'] = str_replace('-', '', $_POST['job_card_no']);

    $db->query("job_cards", "UPDATE `job_cards` SET `user_created` = '{$_SESSION['user_id']}', `date_time_created` = '$date', `date_required` = '{$_POST['date_required']}', `company_name` = '{$_POST['company_name']}', `account_no` = '{$_POST['account_no']}', `vat_no` = '{$_POST['vat_no']}', `job_card_no` = '{$_POST['job_card_no']}', `customer_order_no` = '{$_POST['customer_order_no']}', `project_name` = '{$_POST['project_name']}', `installation` = '{$_POST['installation']}', `coj_jobcard_no` = '{$_POST['coj_jobcard_no']}', `project_manager` = '{$_POST['project_manager']}', `date_opened` = '{$_POST['date_opened']}', `rep_originator` = '{$_POST['rep_originator']}', `quote_number` = '{$_POST['quote_number']}', `quote_amount` = '$quote_amount', `invoice_number` = '{$_POST['invoice_number']}', `invoice_amount` = '$invoice_amount', `notes` = '{$_POST['notes']}', `costing` = '$costing_amount', `steel_type` = '{$_POST['steel_type']}', `steel_thickness` = '{$_POST['steel_thickness']}', `ip_rating` = '{$_POST['ip_rating']}', `type_of_enclosure` = '{$_POST['type_of_enclosure']}', `cable_entry` = '{$_POST['cable_entry']}', `loose_gland_plates` = '{$_POST['loose_gland_plates']}', `main_voltage` = '{$_POST['main_voltage']}', `controls_form` = '{$_POST['controls_form']}', `incomer` = '{$_POST['incomer']}', `ammeters` = '{$_POST['ammeters']}', `voltmeter` = '{$_POST['voltmeter']}', `motor_kw_rating` = '{$_POST['motor_kw_rating']}', `motor_type` = '{$_POST['motor_type']}', `GEARBOX` = '{$_POST['gearbox']}', `earth_leak` = '{$_POST['earth_leak']}', `ma_rating` = '{$_POST['ma_rating']}', `panel_base_size` = '$panel_base_size', `starter_type` = '$starter_type', `control_voltage` = '$control_voltage', `colour` = '$colour', `lock_types` = '$lock_types', `type_enclosure` = '$type_enclosure', `make_of_enclosure` = '{$_POST['make_of_enclosure']}' WHERE `record_id` = $job_card_id");

    // // loop through uploaded files
    if (!empty($_FILES['attachment_file']['name'])) {
        foreach ($_FILES['attachment_file']['name'] as $index => $name) {
            if ($_FILES['attachment_file']['error'][$index] === UPLOAD_ERR_OK) {
                $tmpName = $_FILES['attachment_file']['tmp_name'][$index];
                $safeName = time() . "_" . basename($name);
                $uploadDir = "attachments/";  // make sure this folder exists and is writable
                move_uploaded_file($tmpName, $uploadDir . $safeName);

                $db->query("job_card_attachments", "INSERT INTO job_card_attachments (job_card_id, `description`, file_name,`date_time`,`user_id`,`file_category_id`,`file_sub_category_id`) VALUES ($job_card_id, '{$_POST['attachment_description'][$index]}', '$safeName','$date','{$_SESSION['user_id']}','{$_POST['file_category'][$index]}','{$_POST['file_subcategory'][$index]}')");
            }
        }
    }

    // delete all contact persons and add new ones
    $db->query("job_card_contact_persons", "DELETE FROM `job_card_contact_persons` WHERE `job_card_id` = $job_card_id");
    // loop through contact persons
    if (!empty($_POST['contact_person'])) {
        $index = 0;
        foreach ($_POST['contact_person'] as $index => $name) {

            $person_name = $name;
            $email = $_POST['email'][$index];
            $contact_number = $_POST['cell_no'][$index];
            $description = $_POST['description'][$index];


            // Optionally: insert into DB
            $db->query("job_card_contact_persons", "INSERT INTO `job_card_contact_persons`(`name`, `contact_number`, `email`, `job_card_id`, `description`) VALUES ('$person_name','$contact_number','$email','$job_card_id','$description')");
            $index++;
        }
    }

    // delete all sections and add new ones
    $db->query("job_card_sections", "DELETE FROM `job_card_sections` WHERE `job_card_id` = $job_card_id");

    // loop through sections
    if (!empty($_POST['section'])) {
        $index = 0;
        foreach ($_POST['section'] as $index => $name) {

            $section_name = $name;
            $section_description = $_POST['section_description'][$index];

            // Optionally: insert into DB
            $db->query("job_card_sections", "INSERT INTO `job_card_sections`(`section_name`, `section_description`, `date_time`,`job_card_id`,`user_id`) VALUES ('$section_name',' $section_description','$date','$job_card_id','{$_SESSION['user_id']}')");
            $index++;
        }
    }

    header("location:home.php");
}
