<?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"));
$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']);


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

 // // 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]}')");
            }
        }
    }


// 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++;
    }
}

// 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");