<?php
include "classes/class.loader.php";
$html = new html();
$html->bacground_emoji();
$calls = new functions();
$db = new db();
$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_news.php'">ADD NEWS</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";
                    } else {
                        row.style.backgroundColor = "";
                    }
                });
            }
        </script>
    </div>
    <?php

    echo DashboardWidget::includeAssets();


    $table_data = [];

    $bookings_res = $db->query("SELECT * FROM `news` WHERE 1 ORDER BY record_id DESC");
    while ($booking = $bookings_res->fetch_assoc()) {

        $table_data[] = [
            $booking['record_id'],
            $booking['heading'],
            "<textarea rows='4' >" . $booking['description'] . "</textarea>",
            "<img class='table_image' src='https://unleashed.elegantwork.co.za/news_upload/" . $booking['file_name'] . " ' alt=''>",
            $booking['date_time'],
            'edit_news.php?record_id=' . $booking['record_id']
        ];

    }

    
    echo DashboardWidget::tableBlock(
        'NEWS POSTS',
        ['No', 'HEADING', 'DESCRIPTION', 'IMAGE', 'DATE'],
        $table_data,
        '90vw'
    );
    ?>

</div>

<?php $html->html_end(); ?>