<?php
session_start();

include "classes/html.class.php";
$where_data = ' 1 ';
if ($_POST['date_from'] != '') {
    $where_data = $where_data . " AND date_time BETWEEN '{$_POST['date_from']} 00:00' AND '{$_POST['date_to']} 23:59'";
}

if ($_POST['type'] != 'ALL') {
    $where_data = $where_data . " AND inpection_id = '{$_POST['type']}'";
}


if ($_POST['username'] != 'ALL') {
    $where_data = $where_data . " AND user_id = '{$_POST['username']}'";
}


if ($_POST['asset_id'] != 'ALL') {
    $where_data = $where_data . " AND asset_record_id = '{$_POST['asset_id']}'";
}


if ($_POST['farm_id'] != '0') {
    $where_data = $where_data . " AND farm_id = {$_POST['farm_id']}";
}


$db = new db();
$table_data = '<tr></tr>';
$jobcards_res = $db->exec_query('inspections', ['*'], '', '', '', '', $where_data, 'ORDER BY record_id DESC LIMIT 100');

$where_data = str_replace("'", "|", $where_data);
$index = 0;
$total = 0;
$total_open = 0;
$total_closed = 0;
$total_waiting = 0;
$html = new html("REPORTS");

while ($jobcard_info = $jobcards_res->fetch_assoc()) {

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

    $user_info_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$jobcard_info['user_id']}");
    $user_info = $user_info_res->fetch_assoc();

    $pre_inspection_res = $db->exec_query('pre_inspection_content', ['*'], '', '', '', '', '', "record_id = {$jobcard_info['inpection_id']}");
    $pre_inspection_info = $pre_inspection_res->fetch_assoc();

    $farm_res = $db->exec_query('farms', ['*'], '', '', '', '', "record_id = {$jobcard_info['farm_id']}");
    $farm = $farm_res->fetch_assoc();

    // 


    $table_data = $table_data . "<tr>
    <td><input type='text' class='table_input' id='opened_date_$index' readonly value='{$jobcard_info['record_id']}'</td>
    <td><input type='text' class='table_input' value='{$jobcard_info['date_time']}' readonly /></td>
    <td><input type='text' class='table_input' id='status_$index' value='{$pre_inspection_info['name']}' readonly /></td>
    <td><input type='text' class='table_input' value='{$asset_info['fleet_no']}' readonly /></td>
    <td><input type='text' class='table_input' value='{$user_info['username']}' readonly /></td>
    <td><a href='inspection.pdf.php?inspection_id={$jobcard_info['record_id']}' target='_blank'><input type='submit' id='btn_{$jobcard_info['record_id']}' value='PDF' class='form_btn' onclick='LOADING_{$jobcard_info['record_id']}()' id='btn_{$jobcard_info['record_id']}' readonly /></a></td></tr>
    ";
    $index++;
    $total++;
}

?>

<style>
    body {
        background-image: url('');
        /* background-color: white; */
        background-repeat: repeat-y;

    }
</style>
<table style='width:100%; text-align:center; font-size:2vw;'>
    <tr>
        <td>
            <form action='inspections_report.pdf.php' target='_blank' style="margin-bottom: 1vw" method='POST'>
                <input type='text' hidden name='where_data' value='<?php echo $where_data; ?>' />
                <br>
                <input type='submit'  value='FULL REPORT' class='form_btn' id='full_report_id'  />
            </form>
            <form action='api/excel_esport_inspections.php'  target='_blank' method='POST'>
                <input type='text' hidden name='where_data' value='<?php echo $where_data; ?>' />
                <input type='text' hidden name='date_from' value='<?php echo $_POST['date_from']; ?>' />
                <input type='text' hidden name='date_to' value='<?php echo $_POST['date_to']; ?>' />
                <input type='submit' value='CSV REPORT' class='form_btn' id='report_btn'  style="margin-top:2vw;" />
            </form>
        </td>
</table>

<hr><br>
<table style='width:100%; text-align:center;'>
    <tr>
        <td>
            NO.
        </td>
        <td>
            DATE
        </td>
        <td>
            INSPECTION TYPE
        </td>
        <td>
            FLEET NO
        </td>
        <td>
            USERNAME
        </td>
    </tr>


    <?php echo $table_data; ?>
</table>


</body>

</html>