<?php
include "../../root.class.php";
var_dump($_POST);

$db = new db_safeguard();
$date = date("Y-m-d H:i", strtotime("+2 hours"));
$index = 1;
while ($index <= count($_POST)) {

    if (strlen($_POST['area_' . $index]) == 0) {
        $index++;
        continue;
    }
    $job_card_tech_details_id = $db->query("job_card_tech_details", "INSERT INTO `job_card_tech_details`(`job_card_tech_id`, `issue`, `part_number`, `area`,`image`) VALUES ({$_POST['record_id']},'{$_POST['issue_' . $index]}','{$_POST['part_number_' . $index]}','{$_POST['area_' . $index]}','{$_POST['file_name_' . $index]}')");
    $sub_index = 1;
    while ($sub_index <= count($_POST) + 5) {

        if ($_POST["detial_notes_" . $index . "_" . $sub_index] == "") {
            $sub_index++;
            echo "Skipped " . $_POST["detial_notes_" . $index . "_" . $sub_index];
            continue;

        }
        echo $job_card_tech_details_id . "---";
        if (strlen($job_card_tech_details_id) < 1) {
            $details = 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 ('$details','$date',{$_SESSION['user_id']},{$_POST['detail_record_id_index_' . $index]})"
            );
        } else {
            $details = 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 ('$details','$date',{$_SESSION['user_id']},$job_card_tech_details_id)"
            );
        }

        $sub_index++;
    }

    $index++;
}

$index = 1;

while ($index <= count($_POST)) {

    $sub_index = 1;
    while ($sub_index <= count($_POST) + 5) {
        if ($_POST["detial_notes_" . $index . "_" . $sub_index] != "" && $_POST['detail_record_id_index_' . $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 ('{$_POST['detial_notes_' . $index . '_' . $sub_index]}','$date',{$_SESSION['user_id']},{$_POST['detail_record_id_index_' . $index]})"
            );
        }
        $sub_index++;
    }

    $index++;
}


if ($_POST['new_jc'] == 1) {
    $record_res = $db->query("tech_job_cards", "SELECT * FROM `job_cards_tech` WHERE record_id = {$_POST['record_id']}");
    $record = $record_res->fetch_assoc();
    $db->query("tech_job_cards", "INSERT INTO `job_cards_tech` (`jc_number`, `date_time_created`, `action_date_time`, `company_name`, `company_address`, `contact_person`, `contact_number`, `client_reported_issue`, `quote_number`, `client_po_number`, `equipment_required`, `status`, `progress`, `user_id`, `user_to_do`, `singed_by_for_quote`, `signature_by_for_quote`, `singed_by_for_complete`, `signature_by_for_complete`, `quote_pdf`) VALUES ('', '{$record['date_time_created']}', '{$record['action_date_time']}', '{$record['company_name']}', '{$record['company_address']}', '{$record['contact_person']}', '{$record['contact_number']}', '{$record['client_reported_issue']}', '{$record['quote_number']}', '{$record['client_po_number']}', '{$record['equipment_required']}', '{$record['status']}',0, '{$record['user_id']}', '{$record['user_to_do']}', '{$record['singed_by_for_quote']}', '{$record['signature_by_for_quote']}', '{$record['singed_by_for_complete']}', '{$record['signature_by_for_complete']}', '{$record['quote_pdf']}')");
}

$db->query("job_cards_tech", "UPDATE job_cards_tech SET `status` = 2,signature_by_for_quote = '{$_POST['signature']}',singed_by_for_quote = '{$_POST['singed']}' 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']}','2')");
$date = date('Y-m-d H:i', strtotime("+2 Hours"));

$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']},'STOPED')");
$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']);

$email->body(
    "
<h1>JOB CARD : {$_POST['jc_number']}</h1>
<h2> STATUS: SENT FOR APPROVAL</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='tech_jobcard_home.php';</script>";
