<?php
include "classes/class.loader.php";
$html = new html();
$html->bacground_emoji();
$calls = new functions();
$db = new db();
$html->header();
$news_res = $db->query("SELECT * FROM `news` WHERE `record_id` = " . $_GET['record_id'] . "");
$news = $news_res->fetch_assoc();

?>
<style>
    body {
        background-color: #bebebee7;
        color: white;
    }
</style>



<div class="home_container">
    <div class="data_container_for_adding">
        <div class="data_row">
            <form action="upload_news.php" method="post" id="news_form" class="form_class">
                <label>HEADER</label>
                <input type="text" id="record_id" name="record_id" hidden value="<?php echo $_GET['record_id']; ?>">
                <input type="text" id="heading" name="heading" value="<?php echo $news['heading']; ?>">
                <br>
                <label>DESCRIPTION</label>
                <textarea id="description" name="description" rows="10"><?php echo $news['description']; ?></textarea>
                <br>
                <label>IMAGE</label>
                <?php if ($news['file_name'] != "") { ?>
                    <img src="https://unleashed.elegantwork.co.za/news_upload/<?php echo $news['file_name']; ?>" width="80%">
                <?php } ?>
                <br>
                <input type="file" id="image" name="image">
                <br><br>
                <button type="submit" id="button_1">EDIT</button>
            </form>
            <form action="delete_news.php" method="post" id="news_form">
                <input type="text" id="record_id" name="record_id" hidden value="<?php echo $_GET['record_id']; ?>">
                <button type="submit" id="button_2">DELETE</button>
            </form>
            <div id="message"></div>
            <script>
                document.addEventListener("DOMContentLoaded", function () {
                    document.getElementById('news_form').addEventListener('submit', function (e) {
                        document.getElementById("button_1").innerHTML = "UPLODAING...";
                        e.preventDefault();
                        let formData = new FormData(this);
                        let xhr = new XMLHttpRequest();
                        xhr.open('POST', 'edit_news_upload.php', true);
                        xhr.onload = function () {
                            if (xhr.status >= 200 && xhr.status < 400) {
                                if (xhr.responseText == "NEWS UPLOADED") {
                                    document.getElementById("button_1").innerHTML = "UPLODAING...";
                                    document.getElementById("news_form").reset();
                                    window.location.href = "news.php";
                                }
                            }
                        };
                        xhr.send(formData);
                    });
                });
            </script>

        </div>
    </div>

    <?php $html->html_end(); ?>