<?php include "../../root.class.php";
$html = new html();
$db = new db_safeguard();

// var_dump($_POST);

$date = date("Y-m-d H:i");

// $status = empty($_POST['status']) ? 0 : $_POST['status'];
// $additional_notes = empty($_POST['additional_notes']) ? 0 : $_POST['additional_notes'];
// $team_assigned_id = empty($_POST['team_assigned_id']) ? 0 : $_POST['team_assigned_id'];
// $slip_image = empty($_POST['slip_image']) ? 0 : $_POST['slip_image'];
// $payment_type = empty($_POST['payment_type']) ? 0 : $_POST['payment_type'];
// $payment_made = empty($_POST['payment_made']) ? 0 : $_POST['payment_made'];
// $user_created_id = empty($_POST['user_created_id']) ? 0 : $_POST['user_created_id'];
// $type = empty($_POST['type']) ? 0 : $_POST['type'];
// $area = empty($_POST['area']) ? 0 : $_POST['area'];
// $alternate_number = empty($_POST['alternate_number']) ? 0 : $_POST['alternate_number'];
// $other_number = empty($_POST['other_number']) ? 0 : $_POST['other_number'];
// $contact_number = empty($_POST['contact_number']) ? 0 : $_POST['contact_number'];
// $jc_no = empty($_POST['jc_no']) ? 0 : $_POST['jc_no'];

// if (empty($_POST['amount']) || $_POST['amount'] == null) {
//     $amount = 0;
// } else {
//     $amount = $_POST['amount'];
// }

// echo "status: " . $status;

echo "<br><br>";

if ($_POST['status'] == "ASSIGN TEAM") {

    // echo "UPDATE `leads` SET `jc_no`='$jc_no',`contact_number`='$contact_number',`other_number`='$other_number',`alternate_number`='$alternate_number',`area`='$area',`type`='$type',`user_created_id`='$user_created_id',`payment_made`='$payment_made',`payment_type`='$payment_type',`slip_image`='$slip_path',`amount`=$amount,`team_assigned_id`='$team_assigned_id',`additional_notes`='$additional_notes',`status`='$status' WHERE `record_id` = '{$_POST['record_id']}'";

    // echo "<br><br>";

    // echo "INSERT INTO `jobcards`(`jc_no`,`date_created`,`user_id`, `team_assigned_id`) VALUES('{$_POST['jc_no']}','{$date}','{$_SESSION['user_id']}','{$_POST['team_assigned_id']}')";

    // echo "<br><br>";

    // echo "UPDATE `jobcards` SET `contact_number` = '{$_POST['contact_number']}', `other_number` = '{$_POST['other_number']}', `alternate_number` = '{$_POST['alternate_number']}', `address` = '{$_POST['area']}', `slip` = '{$_POST['slip']}' WHERE `jc_no` = '{$_POST['jc_no']}'";

    // $update_leads = $db->query("leads", "UPDATE `leads` SET `jc_no`='$jc_no',`contact_number`='$contact_number',`other_number`='$other_number',`alternate_number`='$alternate_number',`area`='$area',`type`='$type',`user_created_id`='$user_created_id',`payment_made`='$payment_made',`payment_type`='$payment_type',`slip_image`='$slip_path',`amount`=$amount,`team_assigned_id`='$team_assigned_id',`additional_notes`='$additional_notes',`status`='$status' WHERE `record_id` = '{$_POST['record_id']}'");

    // $jobcard_id = $db->query("jobcards", "INSERT INTO `jobcards`(`jc_no`,`date_created`,`user_id`, `team_assigned_id`) VALUES('{$_POST['jc_no']}','{$date}','{$_SESSION['user_id']}','{$_POST['team_assigned_id']}')");

    // $update_jobcard = $db->query("jobcards", "UPDATE `jobcards` SET `contact_number` = '{$_POST['contact_number']}', `other_number` = '{$_POST['other_number']}', `alternate_number` = '{$_POST['alternate_number']}', `address` = '{$_POST['area']}', `slip` = '{$_POST['slip']}' WHERE `jc_no` = '{$_POST['jc_no']}'");

    // echo "UPDATE `work_requests` SET `jc_no`='{$_POST['jc_no']}',`type`='{$_POST['type']}',`contact_number`='{$_POST['contact_number']}',`other_number`='{$_POST['other_number']}',`alternate_number`='{$_POST['alternate_number']}',`address`='{$_POST['area']}',`user_id`='{$_SESSION['user_id']}', `team_id`='{$_POST['team_id']}',`additional_notes`='{$_POST['additional_notes']}',`status`='1' WHERE record_id = '{$_POST['record_id']}'";

    // echo "<br><br>";

    $assign = $db->query("work_requests", "UPDATE `work_requests` SET `jc_no`='{$_POST['jc_no']}',`type`='{$_POST['type']}',`contact_number`='{$_POST['contact_number']}',`other_number`='{$_POST['other_number']}',`alternate_number`='{$_POST['alternate_number']}',`address`='{$_POST['area']}',`user_id`='{$_SESSION['user_id']}', `team_id`='{$_POST['team_id']}',`additional_notes`='{$_POST['additional_notes']}',`status`='1' WHERE record_id = '{$_POST['record_id']}'");

    // echo "INSERT INTO `jobcards`(`jc_no`,`date_created`,`user_id`, `team_assigned_id`, `contact_number`, `other_number`, `alternate_number`, `address`) VALUES('{$_POST['jc_no']}','{$date}','{$_SESSION['user_id']}','{$_POST['team_id']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}','{$_POST['area']}')";

    $insert_jobcards = $db->query("jobcards", "INSERT INTO `jobcards`(`jc_no`,`date_created`,`user_id`, `team_assigned_id`, `contact_number`, `other_number`, `alternate_number`, `address`) VALUES('{$_POST['jc_no']}','{$date}','{$_SESSION['user_id']}','{$_POST['team_id']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}','{$_POST['area']}')");

} else {

    // echo "UPDATE `leads` SET `jc_no`='$jc_no',`contact_number`='$contact_number',`other_number`='$other_number',`alternate_number`='$alternate_number',`area`='$area',`type`='$type',`user_created_id`='$user_created_id',`payment_made`='$payment_made',`payment_type`='$payment_type',`slip_image`='$slip_path',`amount`='$amount',`team_assigned_id`='$team_assigned_id',`additional_notes`='$additional_notes',`status`='$status' WHERE `record_id` = '{$_POST['record_id']}'";

    // $update_leads = $db->query("leads", "UPDATE `leads` SET `jc_no`='$jc_no',`contact_number`='$contact_number',`other_number`='$other_number',`alternate_number`='$alternate_number',`area`='$area',`type`='$type',`user_created_id`='$user_created_id',`payment_made`='$payment_made',`payment_type`='$payment_type',`slip_image`='$slip_path',`amount`='$amount',`team_assigned_id`='$team_assigned_id',`additional_notes`='$additional_notes',`status`='0' WHERE `record_id` = '{$_POST['record_id']}'");

    // echo "UPDATE `work_requests` SET `jc_no`='{$_POST['jc_no']}',`type`='{$_POST['type']}',`contact_number`='{$_POST['contact_number']}',`other_number`='{$_POST['other_number']}',`alternate_number`='{$_POST['alternate_number']}',`address`='{$_POST['area']}',`user_id`='{$_SESSION['user_id']}',`additional_notes`='{$_POST['additional_notes']}',`status`='0' WHERE record_id = '{$_POST['record_id']}'";

    $update_lead = $db->query("work_requests", "UPDATE `work_requests` SET `jc_no`='{$_POST['jc_no']}',`type`='{$_POST['type']}',`contact_number`='{$_POST['contact_number']}',`other_number`='{$_POST['other_number']}',`alternate_number`='{$_POST['alternate_number']}',`address`='{$_POST['area']}',`user_id`='{$_SESSION['user_id']}',`additional_notes`='{$_POST['additional_notes']}',`status`='0' WHERE record_id = '{$_POST['record_id']}'");

}
?>

<script>alert("UPDATED SUCCESSFULLY");</script>

<?php
echo "<script>window.location.href = '../jobcards/lead_home.php';</script>";
