<?php include "../../root.class.php";
$html = new html();
$db = new db_safeguard();

// var_dump($_POST);

$date = date("Y-m-d H:i");

if (!empty($_FILES['slip_image']['name'])) {
    $slip_image_path = "images/" . date("Y-m-d_H-i-s") . "_" . basename($_FILES['slip_image']['name']);
    move_uploaded_file($_FILES['slip_image']['tmp_name'], $slip_image_path);
    $_POST['slip_file'] = $slip_image_path;
    $slip_path = $slip_image_path;

} else if (!empty($_FILES['slip_image1']['name'])) {
    $slip_image_path = "images/" . date("Y-m-d_H-i-s") . "_" . basename($_FILES['slip_image1']['name']);
    move_uploaded_file($_FILES['slip_image1']['tmp_name'], $slip_image_path);
    $_POST['slip_file'] = $slip_image_path;
    $slip_path = $slip_image_path;
}

$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 ($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']}'");


} 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`='$status' WHERE `record_id` = '{$_POST['record_id']}'");

}

echo "<script>window.location.href = '../jobcards/lead_home.php';</script>";
