<?php
include "classes/class.loader.php";
$html = new html();
$html->bacground_emoji();
$calls = new functions();
$db = new db();
$DashboardWidget = new DashboardWidget();
echo DashboardWidget::includeAssets();
$html->header();

?>
<style>
    body {
        background-color: #bebebee7;
        color: white;
    }
</style>

<div class="home_container">
    <div class="container_square">
        <div class="line"></div>
        <br>
        <button onclick="window.location.href='add_service.php'">ADD NEW</button>
    </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 = [];

    $services_res = $db->query("SELECT * FROM services WHERE 1 ORDER BY `name` ASC");
    while ($services = $services_res->fetch_assoc()) {
        $table_data[] = [
            $services['record_id'],
            $services['name'],
            $services['description'],
            $services['price'],
            "service.php?record_id=" . $services['record_id']
        ];
    }
    echo DashboardWidget::tableBlock(
        'Current Services',
        ['No', 'Name', 'Desc', 'Price'],
        $table_data,
        '90vw'
    );
    ?>
</div>

<?php $html->html_end(); ?>