<?php

session_start();

if (!isset($_SESSION['username'])) {
    header('Location: ../Users/login.php');
}

if (isset($_POST['returnButton'])) {
    header('Location: ../Users/admin.php');
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
        crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <title>Edit a news report</title>
</head>

<body>
    <div class="container" id="addFormDiv">
        <div class="row">
            <div class="col-sm">
                <div class="container text-left">
                    <form id="editForm">
                        <h3>Edit a news:</h3>
                        <?php
                        require_once '../../Repositories/Repository.php';
                        $connection = new DBConnection();
                        if (!isset($_GET['id'])) {
                            header('Location: ../Users/admin.php');
                        }
                        $id = $_GET['id'];
                        $result = $connection->getNewsById($id);
                        if (!isset($result) || empty($result)) {
                            header('Location: ../Users/admin.php');
                        }
                        echo '<input type="hidden" id="idField" value="' . $result['id'] . '">';
                        echo '<div class="mb-1"><label for="titleField" class="form-label">Title: </label><input type="text" id="titleField" class="form-control" required pattern="[0-9A-Za-z-.\:;]{5,30}" value="' . $result['title'] . '"></div>';
                        echo '<div class="mb-1"><label for="contentField" class="form-label">Content: </label><input
                        type="text" id="contentField" class="form-control" required
                        pattern="[0-9A-Za-z-.\:;]{10,256}" value="' . $result['news_text'] . '"></div>';
                        echo '<div class="mb-1"><label for="dateField" class="form-label">Date: </label><input type="date"
                                id="dateField" class="form-control" required value=' . $result["date"] . '></div>';
                        echo '<div class="mb-1"><label for="producerField" class="form-label">Producer: </label><input
                        type="text" id="producerField" class="form-control" required
                        pattern="[0-9A-Za-z-]{5,30}" value="' . $result['producer'] . '"></div>';
                        echo '<div class="mb-1"><label for="categoryField" class="form-label">Category: </label><input
                        type="text" id="categoryField" class="form-control" required
                        pattern="[0-9A-Za-z-]{5,30}" value="' . $result['category'] . '"></div>';
                        ?>
                        <button id="updateNewsButton" type="submit" class="btn btn-success mb-1">Update
                            News</button>
                    </form>
                    <form method="post">
                        <input type="submit" class="btn btn-secondary mb-1" name="returnButton"
                            value="Return to profile">
                    </form>
                </div>
            </div>
        </div>
    </div>
    <script>
        const updateNews = (e) => {
            e.preventDefault();
            const id = $('#idField').val();
            const title = $('#titleField').val();
            const content = $('#contentField').val();
            const date = $('#dateField').val();
            const producer = $('#producerField').val();
            const category = $('#categoryField').val();
            $.ajax({
                url: 'http://localhost/web/Lab 7/Repositories/Repository.php',
                type: 'POST',
                data: {
                    id: id,
                    title: title,
                    news_text: content,
                    date: date,
                    producer: producer,
                    category: category,
                    action: 'updateNews'
                },
                success: function (response) {
                    alert('Log added successfully!');
                },
                error: function (response) {
                    alert('Error adding log!');
                }
            });
        }
        $('#editForm').submit(updateNews);
    </script>
</body>

</html>