<?php
include "../../root.class.php";
$db = new db_safeguard();
var_dump($_POST);
$date = date("Y-m-d H:i", strtotime("+2 Hours"));

$db->query("job_cards_tech", "UPDATE `job_cards_tech` SET `status`='{$_POST['status']}', `singed_by_for_complete` = '{$_POST['singed']}', `signature_by_for_complete` = '{$_POST['signature']}' WHERE `record_id`={$_POST['record_id']}");

$index = 1;

while ($index <= count($_POST)) {

    if ($_POST['returned_parts_' . $index] != '') {
        $db->query("job_card_tech_returns", "INSERT INTO `job_card_tech_returns`( `returned_part`, `date_time`, `user_id`, `job_card_tech_id`) VALUES ('{$_POST['returned_parts_' . $index]}','$date',{$_SESSION['user_id']},{$_POST['record_id']})");
    }

    $sub_index = 1;
    while ($sub_index <= count($_POST) + 5) {
        if ($_POST["detial_notes_" . $index . "_" . $sub_index] != "") {
            $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++;
}

// get user id and its email
$email = new email();
$db->query("job_cards_tech_timeline", "INSERT INTO `job_cards_tech_timeline`( `date_time`, `user_id`, `job_card_tech_id`, `type`) VALUES ('$date',{$_SESSION['user_id']},{$_POST['record_id']},'COMPLETED')");
$date = date('Y-m-d H:i', strtotime("+ 2 Hours"));
$call = new call_functions();
$email->add_address($call->get_user_email($_SESSION['user_id']), $call->get_username($_SESSION['user_id']));
$email->subject($_POST['jc_number']);
$image_data = file_get_contents("signatures/" . $_POST['signature']);
$status = $call->get_job_card_status($_POST['status']);
$email->body(
    "
<h1>JOB CARD : {$_POST['jc_number']} </h1>
<h2> STATUS: $status </h2>
<h3>Singed By : {$_POST['singed']}</h3>
<img src='data:image/png;base64," . base64_encode($image_data) . "' width='200' height='100' />
<h3>Technician : " . $call->get_username($_SESSION['user_id']) . "</h3>"
);

// $email->send();

echo "<script>location.href = 'jobcard_action_home.php';</script>";