<?php
session_start();

// {"record_id":"22","asset_type_id":"10","asset_id":"B1-106","fleet_no":"B1-106","status":"ACTIVE","farm_id":"5","qr_code":"dc=6690","license_exp_date":"NONE","description":"Case 90 JXM #2","vin":"FR1425519 (AE90JXM90WDICY1014)","fuel_size":"80"}
// echo $_POST['asset_record_id'];

include "classes/html.class.php";
$date = date("Y-m-d H:i", strtotime("+ 2hours"));
$db = new db();
$table_data = '<tr></tr>';

$get_last_odo_res = $db->exec_query('asset_odo', ['*'], '', '', '', '', "asset_record_id = {$_POST['asset_record_id']}", "ORDER BY record_id DESC");
$get_last_odo = $get_last_odo_res->fetch_assoc();

$get_asset_info = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = '{$_POST['asset_record_id']}'");
$asset_info = $get_asset_info->fetch_assoc();

$get_type_res = $db->exec_query('asset_types', ['*'], '', '', '', '', "record_id = '{$asset_info['asset_type_id']}'");
$type_info =  $get_type_res->fetch_assoc();

$inspection_res = $db->exec_query('pre_inspection_content', ['*'], '', '', '', '', "record_id = {$type_info['pre_inspection_list_id']}");

$pre_inspection_info = $inspection_res->fetch_assoc();

$index = 0;
$questions_array_part_1 = explode(',', $pre_inspection_info['question']);
$questions_array_part_2 = explode(',', $pre_inspection_info['answer_type']);
$questions_array_part_3 = explode(',', $pre_inspection_info['warning']);
$questions_array_part_1[] = "ASSET FIT FOR USE";
$questions_array_part_2[] = "Y";
$questions_array_part_3[] = "0";

$array_count = count($questions_array_part_1);
$questions_array = [];
while ($index <= $array_count) {
    $questions_array[] = $questions_array_part_1[$index] . '||' . $questions_array_part_2[$index] . '||' . $questions_array_part_3[$index];
    $index++;
}


$html = new html("inspections");
?>


<table style='width:100%; text-align:center;'>
    <br>
    <tr>
        <td style="font-size:1.5vw;">
            DATE OPENED
        </td>
        <td style="font-size:1.5vw;">
            ASSET ID
        </td>


    </tr>
    <tr>
        <td>
            <input type='text' value='<?php echo date("Y-m-d H:i", strtotime("+2 hours")) ?>' class='form_input' readonly />

        </td>
        <td>
            <input type='text' value='<?php echo $asset_info['asset_id']; ?>' id='asset_id' name='asset_id' class='form_input' readonly />
            <input type='text' value='<?php echo $asset_info['record_id']; ?>' hidden id='asset_record_id' class='form_input' readonly />


        </td>
        <td>



        </td>
        <td>
            <!-- <input type='submit' value='CONTINUE' class='form_btn' /> -->
        </td>
    </tr>
</table>
<hr>

<div style='width:100%;text-align:center; height:fit-content;'>
    <div class="form">
        <h2 style="font-size:1.5vw;">ODO</h2>

        <input type='number' placeholder="<?php echo $get_last_odo['odo']; ?>" name='odo' id='odo' onchange="odo_change()" class='form_input' /><br><br>
        <input type='text' id='old_odo' hidden value='<?php echo $get_last_odo['odo']; ?>' />
        <script>
            function odo_change() {
                odo = document.getElementById('odo').value + 1 - 1;
                old_odo = document.getElementById('old_odo').value + 1 - 1;
                if (odo >= old_odo) {
                    // alert("Test");
                    odo = document.getElementById('odo').value;
                    asset_record_id = document.getElementById('asset_record_id').value;


                    // Create an XMLHttpRequest object
                    const xhttp = new XMLHttpRequest();
                    // Define a callback function
                    xhttp.onload = function() {
                        // Here you can use the Data
                        if (this.responseText != 1) {

                            // alert(this.responseText);

                        } else {
                            alert("Something Went Wrong");

                        }
                    }

                    xhttp.open("GET", "ajax/update_jobcard.ajax.php?odo=" + odo + "&asset_record_id=" + asset_record_id);
                    xhttp.send();

                } else {
                    document.getElementById('odo').value = '';
                    alert("ODO cant be less than last reported");
                }
            }
        </script>
        <form action='inspection_save.php' method='POST'>
            <input type='text' value='<?php echo $asset_info['record_id']; ?>' hidden id='asset_record_id' class='form_input' readonly />
            <input type='text' value='<?php echo $asset_info['asset_id']; ?>' id='asset_id' name='asset_id' class='form_input' readonly />
            <input type='text' name='inspection_id' value='<?php echo $type_info['pre_inspection_list_id']; ?>' hidden />
            <input type='text' name='asset_record_id' value='<?php echo $asset_info['record_id']; ?>' hidden />
            <?php
            $w_index = 0;
            foreach ($questions_array as $value) {

                $q_a = explode('||', $value);

                echo "<input type='text' value='{$q_a[0]}' name='question_$w_index' hidden/>";

                if ($q_a[1] == 'B') {
            ?>


                    <h2 style="font-size:1.5vw;"><?php echo $q_a[0]; ?>:</h2>
                    <input id='q_a_<?php echo $w_index; ?>' name='q_a_<?php echo $w_index; ?>' hidden value='NO' />
                    <button class='form_input' onclick='select_<?php echo $w_index; ?>(1)' id='btn_1_<?php echo $w_index; ?>'>NO PROBLEM</button>
                    <button class='form_input' id='btn_2_<?php echo $w_index; ?>' onclick='select_<?php echo $w_index; ?>(2)'>PROBLEM</button><br><br>
                    <h2 id='q_w_n_<?php echo $w_index; ?>' style="font-size:1.5vw;" hidden>REASON?</h2><br>
                    <textarea id='note_<?php echo $w_index; ?>' name='note_<?php echo $w_index; ?>' class='text_input' hidden style='width:80%;height:10vw;'></textarea>
                    <input type='text' hidden id='w_<?php echo $w_index; ?>' name='w_<?php echo $w_index; ?>' value='<? echo $q_a[2]; ?>' />

                    <script>
                        document.getElementById("btn_1_<?php echo $w_index; ?>").addEventListener("click", function(event) {
                            event.preventDefault();
                        });
                        document.getElementById("btn_2_<?php echo $w_index; ?>").addEventListener("click", function(event) {
                            event.preventDefault();
                        });
                        document.getElementById('btn_1_<?php echo $w_index; ?>').style.backgroundColor = 'grey';
                        document.getElementById('btn_2_<?php echo $w_index; ?>').style.backgroundColor = 'grey';

                        function select_<?php echo $w_index; ?>(value) {
                            if (value == 1) {

                                document.getElementById('q_a_<?php echo $w_index; ?>').value = 'YES';
                                document.getElementById('btn_1_<?php echo $w_index; ?>').classList.add('inspec_btn');
                                document.getElementById('btn_2_<?php echo $w_index; ?>').classList.remove('inspec_btn');
                                document.getElementById('btn_2_<?php echo $w_index; ?>').style.backgroundColor = 'grey';
                                document.getElementById('note_<?php echo $w_index; ?>').hidden = true;
                                document.getElementById('q_w_n_<?php echo $w_index; ?>').hidden = true;

                            } else {
                                document.getElementById('q_a_<?php echo $w_index; ?>').value = 'NO';
                                document.getElementById('btn_1_<?php echo $w_index; ?>').classList.remove('inspec_btn');
                                document.getElementById('btn_1_<?php echo $w_index; ?>').style.backgroundColor = 'grey';
                                document.getElementById('btn_2_<?php echo $w_index; ?>').classList.add('inspec_btn');
                                document.getElementById('note_<?php echo $w_index; ?>').hidden = false;
                                document.getElementById('q_w_n_<?php echo $w_index; ?>').hidden = false;


                            }

                        }
                    </script>
                    <br><br>
                <?php
                } else if ($q_a[1] == 'Y') {
                ?>


                    <h2 style="font-size:1.5vw;"><?php echo $q_a[0]; ?>:</h2>
                    <input id='q_a_<?php echo $w_index; ?>' name='q_a_<?php echo $w_index; ?>' hidden value='NO' />
                    <button class='form_input' onclick='select_<?php echo $w_index; ?>(1)' id='btn_1_<?php echo $w_index; ?>'>YES</button>
                    <button class='form_input' id='btn_2_<?php echo $w_index; ?>' onclick='select_<?php echo $w_index; ?>(2)'>NO</button><br><br>
                    <h2 id='q_w_n_<?php echo $w_index; ?>' style="font-size:1.5vw;" hidden>REASON?</h2><br>
                    <textarea id='note_<?php echo $w_index; ?>' name='note_<?php echo $w_index; ?>' class='text_input' hidden style='width:80%;height:10vw;'></textarea>
                    <input type='text' hidden id='w_<?php echo $w_index; ?>' name='w_<?php echo $w_index; ?>' value='<? echo $q_a[2]; ?>' />

                    <script>
                        document.getElementById("btn_1_<?php echo $w_index; ?>").addEventListener("click", function(event) {
                            event.preventDefault();
                        });
                        document.getElementById("btn_2_<?php echo $w_index; ?>").addEventListener("click", function(event) {
                            event.preventDefault();
                        });
                        document.getElementById('btn_1_<?php echo $w_index; ?>').style.backgroundColor = 'grey';
                        document.getElementById('btn_2_<?php echo $w_index; ?>').style.backgroundColor = 'grey';

                        function select_<?php echo $w_index; ?>(value) {
                            if (value == 1) {

                                document.getElementById('q_a_<?php echo $w_index; ?>').value = 'YES';
                                document.getElementById('btn_1_<?php echo $w_index; ?>').classList.add('inspec_btn');
                                document.getElementById('btn_2_<?php echo $w_index; ?>').classList.remove('inspec_btn');
                                document.getElementById('btn_2_<?php echo $w_index; ?>').style.backgroundColor = 'grey';
                                document.getElementById('note_<?php echo $w_index; ?>').hidden = true;
                                document.getElementById('q_w_n_<?php echo $w_index; ?>').hidden = true;

                            } else {
                                document.getElementById('q_a_<?php echo $w_index; ?>').value = 'NO';
                                document.getElementById('btn_1_<?php echo $w_index; ?>').classList.remove('inspec_btn');
                                document.getElementById('btn_1_<?php echo $w_index; ?>').style.backgroundColor = 'grey';
                                document.getElementById('btn_2_<?php echo $w_index; ?>').classList.add('inspec_btn');
                                document.getElementById('note_<?php echo $w_index; ?>').hidden = false;
                                document.getElementById('q_w_n_<?php echo $w_index; ?>').hidden = false;


                            }

                        }
                    </script>
                    <br><br>
                <?php
                } else if ($q_a[1] == 'T') {
                ?>
                    <h2 style="font-size:1.5vw;"><?php echo $q_a[0]; ?>:</h2> <input name='q_a_<?php echo $w_index; ?>' class='form_input' />
                    <input type='text' hidden id='w_<?php echo $w_index; ?>' name='w_<?php echo $w_index; ?>' value='<? echo $q_a[2]; ?>' />
                    <br><br>

            <?php
                }
                $w_index++;
            }
            echo "<input type='number' hidden value='$w_index' name='total_questions' />"
            ?>
            <br>
            <h2 style="font-size:1.5vw;">NOTES</h2><br>
            <textarea type='text' name='notes' id='notes' class="text_input" style='width:80%; height:10vw;'></textarea><br><br>
            <hr>
            <br>

            <input type='submit' name='COMPLETE' value='COMPLETE' class='form_btn' />

        </form>
    </div>
</div>
<br>
<br>
</body>

</html>