<?php
include "../../root.class.php";
$db = new db_safeguard();
// var_dump($_POST);


if ($_POST['quote_pdf_1'] != '') {
    $quote = ", `quote_pdf`='{$_POST['quote_file_1']}'";
}

if ($_POST['po_pdf_1'] != '') {
    $po = ", `po_pdf`='{$_POST['po_file_1']}'";
}
if (isset($_POST['client_po_number'])) {
    $po_number = ",`client_po_number` = '{$_POST['client_po_number']}'";
}

if (isset($_POST['quote_number'])) {
    $quote_number = ",`quote_number` = '{$_POST['quote_number']}'";
}


$db->query("job_cards_tech", "UPDATE `job_cards_tech` SET `status`='{$_POST['status']}',`jc_number` = '{$_POST['jc_number']}',`company_name` = '{$_POST['company_name']}',`user_to_do` = '{$_POST['user_to_do']}' $po_number $quote_number $quote $po WHERE `record_id`={$_POST['record_id']}");
$date = date('Y-m-d H:i', strtotime("+2 Hours"));
$db->query("job_card_tech_status_timeline", "INSERT INTO `job_card_tech_status_timeline`(`date_time`, `user_id`, `job_card_tech_id`, `status`) VALUES ('$date','{$_SESSION['user_id']}','{$_POST['record_id']}','{$_POST['status']}')");
$index = 1;
$date = date('Y-m-d H:i', strtotime("+2 Hours"));
while ($index < count($_POST)) {

    if ($_POST['notes_' . $index] != '') {
        $notes = str_replace("'", "\'", $_POST['notes_' . $index]);
        $db->query("job_card_tech_notes", "INSERT INTO `job_card_tech_notes`( `job_card_tech_id`, `notes`, `user_id`, `date_time`) VALUES ({$_POST['record_id']},' $notes',{$_SESSION['user_id']},'$date')");
    }

    if ($_POST['sticky_notes_' . $index] != '') {
        $sticky_notes = str_replace("'", "\'", $_POST['sticky_notes_' . $index]);

        $db->query("job_card_tech_sticky_notes", "INSERT INTO `job_card_tech_sticky_notes`( `job_card_tech_id`, `note`, `user_id`, `date_time`) VALUES ({$_POST['record_id']},'$sticky_notes',{$_SESSION['user_id']},'$date')");
    }
    $index++;
}
$index = 1;
while ($index < count($_POST)) {

    $sub_index = 1;
    while ($sub_index <= count($_POST)) {

        if ($_POST["detial_notes_" . $index . "_" . $sub_index] == "") {
            $sub_index++;
            continue;
        }
        $detail_notes_notes = str_replace("'", "\'", $_POST['detial_notes_' . $index . '_' . $sub_index]);
        $db->query(
            "job_card_tech_detail_notes",
            "INSERT INTO `job_card_tech_detail_notes` (`note`, `date_time`, `user_id`, `job_card_tech_detail_id`) VALUES ('$detail_notes_notes','$date',{$_SESSION['user_id']},{$_POST['detail_record_id_index_' . $index]})"
        );
        $sub_index++;
    }

    $index++;
}
$index = 1;

while ($index < count($_POST)) {

    if ($_POST['instruction_' . $index] != '') {
        $instructions = str_replace("'", "\'", $_POST['instruction_' . $index]);
        $db->query("job_card_tech_instructions", "INSERT INTO `job_card_tech_instructions`( `job_card_tech_id`, `instruction`, `user_id`, `date_time`) VALUES ({$_POST['record_id']},'$instructions',{$_SESSION['user_id']},'$date')");
    }
    $index++;
}

// get user id and its email
$email = new email();
$call = new call_functions();

$email->add_address($call->get_user_email($_POST['user_id']), $call->get_username($_POST['user_id']));
$email->add_address($call->get_user_email($_POST['user_to_do']), $call->get_username($_POST['user_to_do']));
$email->subject("Job Card {$_POST['jc_number']} Update");

$status = $call->get_job_card_status($_POST['status']);

$email->body(
    "
<h1>JOB CARD : {$_POST['jc_number']}</h1>
<h2> STATUS: $status</h2>
<h3>Technician : " . $call->get_username($_POST['user_to_do']) . "</h3>"

);


if ($_POST['pdf'] == 1) {
    echo "<script>location.href = 'client_jc_pdf.php?record_id={$_GET['record_id']}';</script>";

} else {

    echo "<script>location.href = 'jobcard_tech_home.php';</script>";
}

