<?php
session_start();

include "classes/html.class.php";
$where_data = ' 1 ';
if (strlen($_POST['date_from']) > 6) {
    $where_data = $where_data . " AND license_exp_date BETWEEN '{$_POST['date_from']}' AND '{$_POST['date_to']}' ";
}

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

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


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


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


$db = new db();
$table_data = '<tr></tr>';
$asset_res = $db->exec_query('assets', ['*'], '', '', '', '', $where_data, 'ORDER BY asset_id ASC');

$where_data = str_replace("'", "|", $where_data);
$index = 0;
$total = 0;
$total_open = 0;
$total_SUSPENDED = 0;

while ($asset_info = $asset_res->fetch_assoc()) {

    if ($asset_info['status'] == 'ACTIVE') {
        $total_active = $total_active + 1;
        // $table_data = $table_data . "<tr><td>
        // <input type='text' class='table_input' id='asset_status_$index' readonly value='{$jobcard_info['date_time_opened']}'</td>";
    }

    if ($jobcard_info['status'] == 'SUSPENDED') {
        $total_SUSPENDED = $total_SUSPENDED + 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>";
    }

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

    $table_data = $table_data . "
    <td><input type='text' class='table_input' value='{$asset_info['asset_id']}' readonly /></td>
    <td><input type='text' class='table_input' id='status_$index' value='{$asset_info['status']}' readonly /></td>

    <script> if(document.getElementById('status_$index').value == 'SUSPENDED'){
        document.getElementById('status_$index').style.backgroundColor = 'orange';
    } else if(document.getElementById('status_$index').value == 'ACTIVE')
    {document.getElementById('status_$index').style.backgroundColor = 'green';}
     </script>
    <td><input type='text' class='table_input' value='{$farm['farm_name']}' readonly /></td>
    <td><input type='text' class='table_input' value='{$asset_info['license_exp_date']}' readonly /></td>
    <td><a href='asset.pdf.php?asset_record_id={$asset_info['record_id']}' target='_blank' ><input type='submit' value='PDF' class='form_btn' readonly /></a></td></tr>
    ";
    $index++;
    $total++;
}

$html = new html("ASSET REPORT");
?>
<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>
            ACTIVE: <?php echo $total_active; ?>
        </td>
        <td>
            SUSPENDED: <?php echo $total_SUSPENDED; ?>
        </td>
        <td>
            <!-- WAITING PARTS: <?php echo $total_waiting; ?> -->
        </td>
        <td>
            TOTAL: <?php echo $total; ?>
        </td>
        <td>
            <form action='asset_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_from' value='<?php echo $_POST['date_from']; ?>' />
                <input type='text' hidden name='date_to' value='<?php echo $_POST['date_to']; ?>' />
                <input type='submit' value='FULL REPORT' class='form_btn' style="margin-top:2vw;" />
            </form>
            <form action='api/excel_esport_assets.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>
            ID:
        </td>
        <td>
            STATUS
        </td>
        <td>
            FARM
        </td>
        <td>
            LICENSE EXP.
        </td>
        <td>
            <!-- USERNAME -->
        </td>
    </tr>


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

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

</body>

</html>