<?php include "../../root.class.php";
$html = new html();
$db = new db_safeguard();

var_dump($_POST);


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;
}

$date = date("Y-m-d H:i", strtotime("+2 Hours"));

echo "<br><br>";

echo "<br> jobcard type: " . $_POST['jobcard_type'] . "<br><br>";
if (empty($_POST['amount'])) {
    $_POST['amount'] = 0;
}
if (empty($_POST['amount1'])) {
    $_POST['amount1'] = 0;
}
if (empty($_POST['team_assigned_id']) || $_POST['team_assigned_id'] == "") {
    $_POST['team_assigned_id'] = 0;
}
if ($_POST['jobcard_type'] == "LEAD") {


    $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>";
    $record_id = $insert_leads;

} else if ($_POST['jobcard_type'] == "JOBCARD") {


    $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_number1']}','{$_POST['other_number1']}','{$_POST['alternate_number1']}','{$_POST['area1']}','{$_POST['jobcard_type']}','{$_SESSION['user_id']}','{$_POST['payment_made1']}','{$_POST['payment_type1']}','$slip_path','{$_POST['amount1']}','{$_POST['team_assigned_id']}','{$_POST['additional_notes1']}','{$_POST['jobcard_status']}')");

    $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['area1']}','{$_POST['contact_number1']}','{$_POST['other_number1']}','{$_POST['alternate_number1']}','{$_POST['payment_type1']}','{$_POST['amount1']}')");

    $record_id = $insert_leads;

} else if ($_POST['jobcard_type'] == "PUMP INSTALL") {

    echo "<br><br>";

    // echo "INSERT INTO `pump_installation`(`jobcard_no`,`drilling_invoice`, `drilling_team_id`) VALUES ('{$_POST['drilling_jobcard_id']}','{$_POST['drilling_invoice']}','{$_POST['drilling_team_id']}')";

    $db->query("pump_installation", "INSERT INTO `pump_installation`(`jobcard_no`,`drilling_invoice`, `drilling_team_id`) VALUES ('{$_POST['drilling_jobcard_id']}','{$_POST['drilling_invoice']}','{$_POST['drilling_team_id']}')");

    // echo "<script>window.location.href = '../jobcards/home.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/home.php';</script>";

}

echo "<script>window.location.href = '../jobcards/edit_lead.php?record_id=$record_id&created';</script>";

?>