<?php
include "classes/class.loader.php";
$html = new html();
$html->bacground_emoji();
$DashboardWidget = new DashboardWidget();
$calls = new functions();
$db = new db();
$html->header();
echo DashboardWidget::includeAssets();

?>
<style>
    body {
        background-color: #bebebee7;
        color: white;
    }
</style>

<div class="home_container">
    <div class="container_square">
        <h2>BOOKINGS FOR THE MONTH</h2>
        <div class="line"></div>
        <h1><?php echo $calls->get_total_bookings_this_month(); ?></h1>
        <br>
    </div>
    <div class="container_square">
        <h2>BOOKINGS FOR LAST MONTH</h2>
        <div class="line"></div>
        <h1><?php echo $calls->get_total_bookings_last_month(); ?></h1>
        <br>
    </div>
    <div class="container_square">
        <h2>TOTAL BOOKINGS</h2>
        <div class="line"></div>
        <h1><?php echo $calls->get_total_bookings(); ?></h1>
    </div>

    <div class="container_square">
        <br>
        <input type="text" id="search" placeholder="Search">
        <br>
        <button onclick="search()">SEARCH</button>
        <script>
            function search() {
                var search = document.getElementById("search").value;
                var search_str = new RegExp(search, 'i');
                var rows = document.querySelectorAll("tr");
                rows.forEach(function (row) {
                    var match = false;
                    row.querySelectorAll("td").forEach(function (cell) {
                        if (cell.innerHTML.match(search_str)) {
                            match = true;
                        }
                    });
                    if (match) {
                        row.style.backgroundColor = "#46878bff";
                        row.scrollIntoView();
                        row.focus();
                    } else {
                        row.style.backgroundColor = "";
                    }
                });
            }
        </script>
    </div>
    <?php

    $table_data = [];

    $bookings_res = $db->query("SELECT * FROM `bookings` WHERE `status` = 1 ORDER BY record_id DESC LIMIT 15");
    while ($booking = $bookings_res->fetch_assoc()) {
        if ($booking['status'] == 0) {
            $status = 'Pending Payment';
        } else if ($booking['status'] == 1) {
            $status = 'Pending Approval';
        } else if ($booking['status'] == 2) {
            $status = 'APPROVED';
        } else if ($booking['status'] == 3) {
            $status = 'DECLINED';
        }

        $table_data[] = [
            $booking['record_id'],
            $calls->get_username($booking['user_id']),
            $booking['date_of_booking'],
            $booking['time_start'],
            $status,
            $booking['type'],
            'booking.php?record_id=' . $booking['record_id']
        ];

    }


    echo DashboardWidget::tableBlock(
        'WAITING APPROVAL',
        ['No', 'User', 'Date', 'Time', 'status', 'Type'],
        $table_data,
        '90vw'
    );
    $table_data = [];

    $bookings_res = $db->query("SELECT * FROM `bookings` WHERE `status` = 2 ORDER BY record_id DESC LIMIT 15");
    while ($booking = $bookings_res->fetch_assoc()) {
        if ($booking['status'] == 0) {
            $status = 'Pending Payment';
        } else if ($booking['status'] == 1) {
            $status = 'Pending Approval';
        } else if ($booking['status'] == 2) {
            $status = 'APPROVED';
        } else if ($booking['status'] == 3) {
            $status = 'DECLINED';
        }

        $table_data[] = [
            $booking['record_id'],
            $calls->get_username($booking['user_id']),
            $booking['date_of_booking'],
            $booking['time_start'],
            $status,
            $booking['type'],
            'booking.php?record_id=' . $booking['record_id']
        ];

    }


    echo DashboardWidget::tableBlock(
        'CONFIRMED BOOKINGS',
        ['No', 'User', 'Date', 'Time', 'status', 'Type'],
        $table_data,
        '90vw'
    );


    $table_data = [];

    $bookings_res = $db->query("SELECT * FROM `bookings` WHERE `status` = 3 ORDER BY record_id DESC LIMIT 15");
    while ($booking = $bookings_res->fetch_assoc()) {
        if ($booking['status'] == 0) {
            $status = 'Pending Payment';
        } else if ($booking['status'] == 1) {
            $status = 'Pending Approval';
        } else if ($booking['status'] == 2) {
            $status = 'APPROVED';
        } else if ($booking['status'] == 3) {
            $status = 'DECLINED';
        }

        $table_data[] = [
            $booking['record_id'],
            $calls->get_username($booking['user_id']),
            $booking['date_of_booking'],
            $booking['time_start'],
            $status,
            $booking['type'],
            'booking.php?record_id=' . $booking['record_id']
        ];

    }


    echo DashboardWidget::tableBlock(
        'CANCELED BOOKINGS',
        ['No', 'User', 'Date', 'Time', 'status', 'Type'],
        $table_data,
        '90vw'
    );
    ?>
</div>

<?php $html->html_end(); ?>