<?php include "../../root.class.php";
$html = new html();
$db = new db_safeguard();

var_dump($_POST);

$date = date("Y-m-d H:i", strtotime("+2 Hours"));

echo "<br><br>";

if ($_POST['jobcard_type'] == "LEAD") {

    // echo "<br>jc_no: " . $_POST['jc_no'];
    // echo "<br>date: " . $date;
    // echo "<br>contact_number: " . $_POST['contact_number'];
    // echo "<br>other_number: " . $_POST['other_number'];
    // echo "<br>alternate_number: " . $_POST['alternate_number'];
    // echo "<br>area: " . $_POST['area'];
    // echo "<br>jobcard_type: " . $_POST['jobcard_type'];
    // echo "<br>user_id: " . $_SESSION['user_id'];
    // echo "<br>team_assigned_id: " . $_POST['team_assigned_id'];
    // echo "<br>additional_notes: " . $_POST['additional_notes'];
    // echo "<br>status: " . $_POST['status'];

    // $insert_leads = $db->query("leads", "INSERT INTO `leads`(`jc_no`, `date_created`, `contact_number`, `other_number`, `alternate_number`, `area`, `type`, `user_created_id`, `payment_made`, `payment_type`, `slip_image`, `amount`, `team_assigned_id`, `additional_notes`, `status`) VALUES ('{$_POST['jc_no']}','{$date}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}','{$_POST['area']}','{$_POST['jobcard_type']}','{$_SESSION['user_id']}','{$_POST['payment_made']}','{$_POST['payment_type']}','$slip_path','{$_POST['amount']}','{$_POST['team_assigned_id']}','{$_POST['additional_notes']}','{$_POST['status']}')");

    // echo "<br><br>";

    // echo "INSERT INTO `work_requests`(`jc_no`, `type`, `contact_number`, `other_number`, `alternate_number`, `address`, `user_id`, `additional_notes`, `status`) VALUES ('{$_POST['jc_no']}','{$_POST['jobcard_type']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}','{$_POST['area']}','{$_SESSION['user_id']}','{$_POST['additional_notes']}','{$_POST['status']}')";

    $insert_leads = $db->query("work_requests", "INSERT INTO `work_requests`(`jc_no`, `type`, `contact_number`, `other_number`, `alternate_number`, `address`, `user_id`, `additional_notes`, `status`) VALUES ('{$_POST['jc_no']}','{$_POST['jobcard_type']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}','{$_POST['area']}','{$_SESSION['user_id']}','{$_POST['additional_notes']}','{$_POST['status']}')");

    $record_id = $insert_leads;

    echo "<script>window.location.href = '../jobcards/edit_lead.php?record_id=$record_id&created';</script>";

} else if ($_POST['jobcard_type'] == "JOBCARD") {

    // echo "INSERT INTO `jobcards`(`jc_no`,`date_created`,`user_id`, `team_assigned_id`, `slip`, `address`,`contact_number`, `other_number`, `alternate_number`,`payment_method`,`cash_amount`) VALUES('{$_POST['jc_no']}','{$date}','{$_SESSION['user_id']}','{$_POST['team_assigned_id']}', '$slip_path','{$_POST['area']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}','{$_POST['payment_type']}','{$_POST['amount']}')";

    // $jobcard_id = $db->query("jobcards", "INSERT INTO `jobcards`(`jc_no`,`date_created`,`user_id`, `team_assigned_id`, `slip`, `address`,`contact_number`, `other_number`, `alternate_number`,`payment_method`,`cash_amount`) VALUES('{$_POST['jc_no']}','{$date}','{$_SESSION['user_id']}','{$_POST['team_assigned_id']}', '$slip_path','{$_POST['area']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}','{$_POST['payment_type']}','{$_POST['amount']}')");

    // echo "<br>jc_no: " . $_POST['jc_no'];
    // echo "<br>date: " . $date;
    // echo "<br>contact_number: " . $_POST['contact_number'];
    // echo "<br>other_number: " . $_POST['other_number'];
    // echo "<br>alternate_number: " . $_POST['alternate_number'];
    // echo "<br>area: " . $_POST['area'];
    // echo "<br>jobcard_type: " . $_POST['jobcard_type'];
    // echo "<br>user_id: " . $_SESSION['user_id'];
    // echo "<br>team_assigned_id: " . $_POST['team_assigned_id'];
    // echo "<br>additional_notes: " . $_POST['additional_notes'];

    if ($_POST['team_assigned_id'] == "") {
        $_POST['team_assigned_id'] = 0;
    }
    // echo "<br><br>";

    // echo "INSERT INTO `work_requests`(`jc_no`, `type`, `contact_number`, `other_number`, `alternate_number`, `address`, `user_id`, `team_id`, `additional_notes`, `status`) VALUES ('{$_POST['jc_no']}','{$_POST['jobcard_type']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}','{$_POST['area']}','{$_SESSION['user_id']}','{$_POST['team_assigned_id']}','{$_POST['additional_notes']}','{$_POST['status']}')";

    $insert_requests = $db->query("work_requests", "INSERT INTO `work_requests`(`jc_no`, `type`, `contact_number`, `other_number`, `alternate_number`, `address`, `user_id`, `team_id`, `additional_notes`, `status`) VALUES ('{$_POST['jc_no']}','{$_POST['jobcard_type']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}','{$_POST['area']}','{$_SESSION['user_id']}','{$_POST['team_assigned_id']}','{$_POST['additional_notes']}','1')");

    // echo "<br><br>";

    // echo "INSERT INTO `jobcards`(`jc_no`,`date_created`,`user_id`, `team_assigned_id`,`address`,`contact_number`, `other_number`, `alternate_number`) VALUES('{$_POST['jc_no']}','$date','{$_SESSION['user_id']}','{$_POST['team_assigned_id']}','{$_POST['area']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}')";

    $insert_jobcards = $db->query("jobcards", "INSERT INTO `jobcards`(`jc_no`,`date_created`,`user_assigned`, `team_assigned_id`,`address`,`contact_number`, `other_number`, `alternate_number`) VALUES('{$_POST['jc_no']}','$date','{$_SESSION['user_id']}','{$_POST['team_assigned_id']}','{$_POST['area']}','{$_POST['contact_number']}','{$_POST['other_number']}','{$_POST['alternate_number']}')");

    $record_id = $insert_jobcards;

    echo "<script>window.location.href = '../jobcards/edit_jobcard.php?jc_no={$_POST['jc_no']}';</script>";


} else if ($_POST['jobcard_type'] == "PUMP INSTALL") {

    // echo "<br><br>";

    // echo "INSERT INTO `pump_installation`(`jobcard_no`,`drilling_invoice`, `pump_slip`, `drilling_team_id`) VALUES ('{$_POST['drilling_jobcard_id']}','{$_POST['drilling_invoice']}','{$_POST['pump_slip']}','{$_POST['install_team_id']}')";

    $db->query("pump_installation", "INSERT INTO `pump_installation`(`jobcard_no`,`drilling_invoice`, `pump_slip`, `drilling_team_id`) VALUES ('{$_POST['drilling_jobcard_id']}','{$_POST['drilling_invoice']}','{$_POST['pump_slip']}','{$_POST['install_team_id']}')");

    echo "<script>window.location.href = '../jobcards/create_lead.php';</script>";

} else if ($_POST['jobcard_type'] == "PUMP REPAIR") {

    // echo "<br><br>";

    // echo "INSERT INTO `pump_repair`(`jobcard_no`, `installation_id`,`drilling_invoice`, `drilling_team_id`) VALUES ('{$_POST['pump_repair_id']}','{$_POST['installation_id']}','{$_POST['drilling_repair_invoice']}','{$_POST['repair_team_id']}}')";

    $db->query("pump_repair", "INSERT INTO `pump_repair`(`jobcard_no`, `installation_id`,`drilling_invoice`, `drilling_team_id`) VALUES ('{$_POST['pump_repair_id']}','{$_POST['installation_id']}','{$_POST['drilling_repair_invoice']}','{$_POST['repair_team_id']}}')");

    echo "<script>window.location.href = '../jobcards/create_lead.php';</script>";

}

?>