<?php
include "../../root.class.php";
$db = new db_safeguard();
var_dump($_POST);
$date = date("Y-m-d H:i", strtotime("+2 Hours"));
$index = 1;
while ($index < count($_POST)) {

	$sub_index = 1;
	while ($sub_index <= count($_POST)) {

		if ($_POST["detial_notes_" . $index . "_" . $sub_index] == "") {
			$sub_index++;
			continue;
		}

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


$index = 1;
while ($index < count($_POST)) {

	if ($_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']},'{$_POST['notes_' . $index]}',{$_SESSION['user_id']},'$date')");
	}


	if ($_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']},'{$_POST['sticky_notes_' . $index]}',{$_SESSION['user_id']},'$date')");
	}
	$index++;
}

$index = 1;

while ($index < count($_POST)) {
	if ($_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']},'{$_POST['instruction_' . $index]}',{$_SESSION['user_id']},'$date')");
	}
	$index++;
}

if (isset($_POST['client_reported_issue'])) {
	$client_issue = ",`client_reported_issue`='{$_POST['client_reported_issue']}'";
}

$db->query("job_cards_tech", "UPDATE `job_cards_tech` SET `jc_number`='{$_POST['jc_number']}', `date_time_created`='{$_POST['date_time_created']}', `action_date_time`='" . str_replace("T", " ", $_POST['action_date_time']) . "',`company_name`='{$_POST['company_name']}', `company_address`='{$_POST['company_address']}', `contact_person`='{$_POST['contact_person']}', `contact_number`='{$_POST['contact_person_contact']}' $client_issue, `quote_number`='{$_POST['quote_number']}', `client_po_number`='{$_POST['client_po_number']}', `equipment_required`='{$_POST['equipment_required']}', `status`='{$_POST['status']}', `user_id`='{$_POST['user_id']}',`quote_pdf`='{$_POST['quote_pdf_1']}',`po_pdf`='{$_POST['po_pdf_1']}'  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']}')");
// get user id and its email
$call = new call_functions();
$email = new email();
$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 UPDATED");
$email->body("<h1>Job Card Updated</h1>
	<p>Job Card Number: {$_POST['jc_number']}</p>
	<p>Date and Time Created: {$_POST['date_time_created']}</p>
	<p>Action Date and Time: " . str_replace("T", " ", $_POST['action_date_time']) . "</p>
	<p>Company Name: {$_POST['company_name']}</p>
	<p>Company Address: {$_POST['company_address']}</p>
	<p>Contact Person: {$_POST['contact_person']}</p>
	<p>Contact Number: {$_POST['contact_person_contact']}</p>
		<p>Client Reported Issue:<br><hr> {$_POST['client_reported_issue']}<hr><br></p>
	<p>Quote Number: {$_POST['quote_number']}</p>
	<p>Client PO Number: {$_POST['client_po_number']}</p>
	<p>Equipment Required: {$_POST['equipment_required']}</p>
	<p>Status:" . ($_POST['status'] == 1) ? 'Active' : 'Inactive' . " ?></p>
	<p>User: " . $call->get_username($_POST['user_id']) . "</p>
	<p>TECHNICAN:" . $call->get_username($_POST['user_to_do']) . "</p>
");

// $email->send();
echo "<script>location.href = 'jobcard_tech_home.php';</script>";


