<?php
session_start();

include "classes/html.class.php";
$html = new html("JOB CARDS");
$where_data = ' 1 ';
if ($_POST['date_type'] == "DATE CREATED") {
    $where_data = $where_data . " AND date_time_created BETWEEN '{$_POST['date_from']} 00:00' AND '{$_POST['date_to']} 23:59'";
} elseif ($_POST['date_type'] == "DATE OPENED") {
    $where_data = $where_data . " AND date_time_opened BETWEEN '{$_POST['date_from']} 00:00' AND '{$_POST['date_to']} 23:59'";
} elseif ($_POST['date_type'] == "DATE CLOSED") {
    $where_data = $where_data . " AND date_time_closed BETWEEN '{$_POST['date_from']} 00:00' AND '{$_POST['date_to']} 23:59'";
}

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


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


if ($_POST['asset_id'] != 'ALL') {
    $where_data = $where_data . " AND asset_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('job_cards', ['*'], '', '', '', '', $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;

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

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

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

    if ($jobcard_info['job_card_status'] == 'OPEN') {
        $total_open = $total_open + 1;
        $table_data = $table_data . "<tr><td>
        <input type='text' class='table_input' id='opened_date_$index' readonly value='{$jobcard_info['date_time_opened']}'</td>";
    }

    if ($jobcard_info['job_card_status'] == 'CLOSED') {
        $total_closed = $total_closed + 1;
        $table_data = $table_data . "<tr><td>
        <input type='text' class='table_input' id='opened_date_$index' readonly value='{$jobcard_info['date_time_closed']}'</td>";
    }

    if ($jobcard_info['job_card_status'] == 'AWAITING PARTS') {
        $total_waiting = $total_waiting + 1;
        $table_data = $table_data . "<tr><td>
        <input type='text' class='table_input' id='opened_date_$index' readonly value='{$jobcard_info['date_time_opened']}'</td>";
    }




    $table_data = $table_data . "
    <td><input type='text' class='table_input' value='{$asset_info['description']}' readonly /></td>
    <td><input type='text' class='table_input' value='{$asset_info['fleet_no']}' readonly /></td>

    <td><input type='text' class='table_input' value='{$jobcard_info['job_card_type']}' readonly /></td>
    
    <td><input type='text' class='table_input' id='status_$index'  style='width:8vw;' value='{$jobcard_info['job_card_status']}' readonly /></td>

    <script> if(document.getElementById('status_$index').value == 'AWAITING PARTS'){
        document.getElementById('status_$index').style.backgroundColor = 'orange';
    } else if(document.getElementById('status_$index').value == 'OPEN')
    {document.getElementById('status_$index').style.backgroundColor = 'green';}
     </script>
    <td><input type='text' class='table_input'  style='width:8vw;'  value='{$jobcard_info['record_id']}' readonly /></td>
    <td><a href='jobcard.pdf.php?job_card_no={$jobcard_info['record_id']}' target='_blank'><input type='submit' class='form_btn' style='width:8vw;' value='PDF' id='btn_{$jobcard_info['record_id']}' class='form_btn_2' 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>
            OPEN: <?php echo $total_open; ?>
        </td>
        <td>
            CLOSED: <?php echo $total_closed; ?>
        </td>
        <td>
            WAITING PARTS: <?php echo $total_waiting; ?>
        </td>
        <td>
            TOTAL: <?php echo $total; ?>
        </td>
        <td>
            <form action='jobcards_report.pdf.php' target='_blank' method='POST'>
                <input type='text' hidden name='where_data' value='<?php echo $where_data; ?>' />
                <input type='text' hidden name='date_type' value='<?php echo $_POST['date_type']; ?>' />
                <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='PDF REPORT' class='form_btn' id='report_btn' style="margin-top:2vw;" />
            </form>
            <form action='api/excel_esport_job_cards.php' target='_blank' method='POST'>
                <input type='text' hidden name='where_data' value='<?php echo $where_data; ?>' />
                <input type='text' hidden name='date_type' value='<?php echo $_POST['date_type']; ?>' />
                <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>
            DATE
        </td>
        <td>
            DESCRIPTION
        </td>
        <td>
            FLEET NO
        </td>
        <td>
            TYPE
        </td>
        <td style='width:8vw;' >
            STATUS
        </td>
        <td style='width:8vw;'>
            J.C NUM.
        </td>
    </tr>


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

<br>
<br>
<br>
<br>
<br>
<br>

</body>

</html>