<?php
include "../../root.class.php";
$db = new db_safeguard();
var_dump($_POST);

$issue = str_replace("'", " ", $_POST['client_reported_issue']);
$issue = str_replace('"', " ", $issue);

$company_name = str_replace("'", " ", $_POST['company_name']);
$company_name = str_replace('"', " ", $company_name);


$company_address = str_replace("'", " ", $_POST['company_address']);
$company_address = str_replace('"', " ", $company_address);

$contact_person = str_replace("'", " ", $_POST['contact_person']);
$contact_person = str_replace('"', " ", $contact_person);


$contact_person_contact = str_replace("'", " ", $_POST['contact_person_contact']);
$contact_person_contact = str_replace('"', " ", $contact_person_contact);

$record_id = $db->query("job_cards_tech", "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`,  `user_id`, `user_to_do`) VALUES ('{$_POST['jc_number']}','{$_POST['date_time_created']}','" . str_replace("T", " ", $_POST['action_date_time']) . "','$company_name','$company_address','$contact_person','$contact_person_contact','$issue','{$_POST['quote_number']}','{$_POST['client_po_number']}','{$_POST['equipment_required']}','{$_POST['status']}','{$_POST['user_id']}','{$_POST['user_to_do']}')");

$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']}','$record_id','{$_POST['status']}')");
$call = new call_functions();

$emails[] = $call->get_user_email($_POST['user_id']);
$emails[] = $call->get_user_email($_POST['user_to_do']);
$names[] = $call->get_username($_POST['user_id']);
$names[] = $call->get_username($_POST['user_to_do']);

$subject = "{$_POST['jc_number']}";
$body = "<h1>Job Card Created</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:" . $call->get_job_card_status($_POST['status']) . "</p>
	<p>User: " . $call->get_username($_POST['user_id']) . "</p>
	<p>TECHNICAN:" . $call->get_username($_POST['user_to_do']) . "</p>
";
// $email->send();

$call->send_for_email($emails, $names, $subject, $body);
echo "<script>location.href = 'jobcard_tech_home.php';</script>";