<?php
session_start();

include "classes/html.class.php";
$html = new html("JOB CARDS");

$db = new db();

?>
<style>
    body {
        background-image: url('');
        /* background-color: white; */
        background-repeat: repeat-y;

    }
</style>

<table style='width:100%; text-align:center; font-size:2vw;'>
    <td>
        <form action='job_card_age_analysis.pdf.php' target='_blank' method='POST'>
            <input type='submit' value='PDF REPORT' class='form_btn' id='report_btn' style="margin-top:2vw;" />
        </form>
    </td>
</table>

<?php
$table_data = '<tr></tr>';
// $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', 1, 'ORDER BY record_id DESC LIMIT 100');

$jobcards_res = $db->exec_query("job_cards", ["*"], '', '', '', '', "date_time_closed = '' AND UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 15 DAY)) <= UNIX_TIMESTAMP(date_time_created) AND UNIX_TIMESTAMP(date_time_created) <= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 0 DAY))");

$index = 0;
$total = 0;
$total_open = 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++;
}

?>
<br>
<hr><br>

<table style='width:100%; text-align:center; font-size:2vw;'>
    <td>
        <h1> 0 - 15 Days</h1>
    </td>
</table>

<table style='width:100%; text-align:center; font-size:2vw;'>
    <tr>
        <td>
            OPEN: <?php echo $total_open; ?>
        </td>
        <td>
            WAITING PARTS: <?php echo $total_waiting; ?>
        </td>
        <td>
            TOTAL: <?php echo $total; ?>
        </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>

<?php
$table_data = '<tr></tr>';
// $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', 1, 'ORDER BY record_id DESC LIMIT 100');

$jobcards_res = $db->exec_query("job_cards", ["*"], '', '', '', '', "date_time_closed = '' AND UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) <= UNIX_TIMESTAMP(date_time_created) AND UNIX_TIMESTAMP(date_time_created) <= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 16 DAY))");

$index = 0;
$total = 0;
$total_open = 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++;
}

?>
<br>
<hr>
<br>
<table style='width:100%; text-align:center; font-size:2vw;'>
    <td>
        <h1> 16 - 30 Days</h1>
    </td>
</table>

<table style='width:100%; text-align:center; font-size:2vw;'>
    <tr>
        <td>
            OPEN: <?php echo $total_open; ?>
        </td>
        <td>
            WAITING PARTS: <?php echo $total_waiting; ?>
        </td>
        <td>
            TOTAL: <?php echo $total; ?>
        </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>



<?php
$table_data = '<tr></tr>';
// $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', 1, 'ORDER BY record_id DESC LIMIT 100');

$jobcards_res = $db->exec_query("job_cards", ["*"], '', '', '', '', "date_time_closed = '' AND UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 60 DAY)) <= UNIX_TIMESTAMP(date_time_created) AND UNIX_TIMESTAMP(date_time_created) <= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 31 DAY))");

$index = 0;
$total = 0;
$total_open = 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++;
}

?>
<br>
<hr>
<br>
<table style='width:100%; text-align:center; font-size:2vw;'>
    <td>
        <h1> 31 - 60 Days</h1>
    </td>
</table>

<table style='width:100%; text-align:center; font-size:2vw;'>
    <tr>
        <td>
            OPEN: <?php echo $total_open; ?>
        </td>
        <td>
            WAITING PARTS: <?php echo $total_waiting; ?>
        </td>
        <td>
            TOTAL: <?php echo $total; ?>
        </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>




<?php
$table_data = '<tr></tr>';
// $jobcards_res = $db->exec_query('job_cards', ['*'], '', '', '', '', 1, 'ORDER BY record_id DESC LIMIT 100');

$jobcards_res = $db->exec_query("job_cards", ["*"], '', '', '', '', "date_time_closed = '' AND UNIX_TIMESTAMP(CURDATE()) - UNIX_TIMESTAMP(date_time_created) > 60*24*60*60");

$index = 0;
$total = 0;
$total_open = 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++;
}

?>
<br>
<hr>
<br>
<table style='width:100%; text-align:center; font-size:2vw;'>
    <td>
        <h1> 60+ Days</h1>
    </td>
</table>

<table style='width:100%; text-align:center; font-size:2vw;'>
    <tr>
        <td>
            OPEN: <?php echo $total_open; ?>
        </td>
        <td>
            WAITING PARTS: <?php echo $total_waiting; ?>
        </td>
        <td>
            TOTAL: <?php echo $total; ?>
        </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>