<?php
include "../../root.class.php";
$db = new db_safeguard();

$date = date("Y-m-d H:i", strtotime("+2 Hours"));
var_dump($_POST);

$_POST['date_time_created'] = str_replace("T", " ", $_POST['date_time_created']);
$_POST['deadline_date_time'] = str_replace("T", " ", $_POST['deadline_date_time']);
$_POST['action_date_time'] = str_replace("T", " ", $_POST['action_date_time']);
$description = "";

$word_order_id = $db->query("work_orders", "INSERT INTO `work_orders`(`project_id`, `date_time_created`, `description`, `work_order_type_id`, `deadline_date_time`, `user_id`, `user_id_assigned`, `name`, `notes`, `completed`, `inprogress`, `in_review`, `sent_for_review`, `user_id_review`, `singed_off`, `pictures`, `action_date_time`, `public_holiday`) VALUES ('{$_POST['project_id']}','{$_POST['date_time_created']}','$description','{$_POST['work_order_type_id']}','{$_POST['deadline_date_time']}','{$_SESSION['user_id']}','{$_POST['user_id_assigned']}','{$_POST['name']}','{$_POST['notes']}',0,0,0,0,'{$_POST['user_id_review']}',0,'','{$_POST['action_date_time']}',0)");

$index = 1;
while ($index < count($_POST)) {

    if ($_POST['instruction_' . $index] != '') {
        $db->query("work_orders_instructions", "INSERT INTO `work_orders_instructions`(`work_order_id`, `instruction`, `user_id`, `marked`, `file_name`,`date_time_created`) VALUES ($word_order_id,'{$_POST['instruction_' . $index . '']}',{$_SESSION['user_id']},0,'','$date')");
    }
    $index++;
}
$index = 1;
while ($index <= count($_POST) + 2) {

    if ($_POST['resource_' . $index] != '') {
        echo $_POST['resource_' . $index];
        $db->query("work_order_resources", "INSERT INTO `work_order_resources`(`type`, `link`, `user_id`, `date_time`, `work_order_id`, `name`) VALUES ('FILE','{$_POST['file_' . $index]}',{$_SESSION['user_id']},'$date',$word_order_id,'{$_POST['resource_' . $index]}')");

    }
    $index++;
}
$index = 1;
while ($index <= count($_POST) + 2) {
    if ($_POST['log_' . $index] != '') {
        echo $_POST['log_' . $index];
        $db->query("work_order_logs", "INSERT INTO `work_order_logs`(`work_order_id`, `log`, `user_id`,`date_time`) VALUES ($word_order_id,'{$_POST['log_' . $index . '']}',{$_SESSION['user_id']},'$date')");
    }
    $index++;
}
echo "
<script>
window.location.href = 'edit_work_order.php?record_id=$word_order_id';
</script>
";