<?php
session_start();
include "classes/html.class.php";

$db = new db();

$index = 0;
$date = date('Y-m-d H:i', strtotime("+ 2hours"));
$inplode_text = '';
$triggers = '';
$trigger = 0;

while ($index <= $_POST['total_questions'] - 1) {

    // echo $_POST['question_' . $index].'<br>';
    // echo $_POST['q_a_' . $index].'<br>';
    // echo $_POST['w_' . $index].'<br>';

    if (strlen($_POST['question_' . $index]) < 1) {
    } else {
        if ($_POST["w_" . $index] != '0') {
            if ($_POST["w_" . $index] == substr($_POST['q_a_' . $index], 0, 1)) {
                $questions = $questions . $_POST['question_' . $index] . "(WARNING)" . '||';
                $triggers = $triggers . "{$_POST['question_' .$index]} {$_POST['q_a_' .$index]} <br><br>";
                $trigger = 1;
                $answers = $answers . $_POST['q_a_' . $index] . '||';
            } else if ($_POST["w_" . $index] == 'T' && strlen($_POST['q_a_' . $index]) > 1) {
                $questions = $questions . $_POST['question_' . $index] . "(WARNING)" . '||';
                $triggers = $triggers . "{$_POST['question_' .$index]} {$_POST['q_a_' .$index]} <br><br>";
                $trigger = 1;
                $answers = $answers . $_POST['q_a_' . $index] . '||';
            } else {
                $questions = $questions . $_POST['question_' . $index] . '||';
                $triggers = $triggers . "{$_POST['question_' .$index]} {$_POST['q_a_' .$index]} <br><br>";
                $answers = $answers . $_POST['q_a_' . $index] . '||';
            }
        } else {
            $questions = $questions . $_POST['question_' . $index] . '||';
            $triggers = $triggers . "{$_POST['question_' .$index]} {$_POST['q_a_' .$index]} <br><br>";
            $answers = $answers . $_POST['q_a_' . $index] . '||';
        }
    }
    $index++;
}

$implode_text = $questions . ',' . $answers;

if ($trigger == 1) {
    $db->insert("INSERT INTO warning_events (`event_type`,`event_message`,`sent`,`asset_id`) VALUES ('WARNING','<h1>PLEASE NOTE! JOB CARD INSPECTION LIST WARNING(s) TRIGGERED :</h1><hr><h2> <br><br> ASSET: {$_POST['asset_id']} <br><br> DATE: $date :</h2><h2>JOB CARD NUMBER: {$_POST['jobcard_no']}</h2><h2>USER:{$_SESSION['username']}</h2><hr><br><h2>LIST DETAILS: <br><br> $triggers <br>Notes:</h2>{$_POST['notes']}',0,{$_POST['asset_record_id']})");
} else {
}

if ($_POST['WAIT']) {
    if ($db->Update('job_cards', ['warning_questions', 'job_card_status'], [$implode_text, 'AWAITING PARTS'], "record_id = {$_POST['jobcard_no']}")) {
        echo "<script>window.location.href = 'open_jobcards.php';</script>";
    } else {

        echo "<script>alert('something went wrong');window.location.href = 'jobcards.php';</script>";
    }
}

if ($_POST['COMPLETE']) {
    if ($db->Update('job_cards', ['warning_questions', 'job_card_status', 'date_time_closed', 'user_id_closed'], [$implode_text, 'CLOSED', $date, $_SESSION['user_id']], "record_id = {$_POST['jobcard_no']}")) {
        echo "<script>window.location.href = 'open_jobcards.php';</script>";
    } else {

        echo "<script>alert('something went wrong');window.location.href = 'jobcards.php';</script>";
    }
}
// echo $_POST['NOT_DONE_YET'];
if ($_POST['NOT_DONE_YET']) {
    echo "<script>window.location.href = 'open_jobcards.php';</script>";
}
