<?php include "../parts/header.php"; ?>
<?php include "../parts/sidepanel.php"; ?>

<?php
include("../opendb.php");

$id = intval($_GET['id']); 

$sql = "SELECT * FROM course WHERE id=$id";
$result = mysqli_query($conn, $sql);
$data = mysqli_fetch_assoc($result);

$baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
           "://{$_SERVER['HTTP_HOST']}/psn_mentor_new/admin/"; 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = mysqli_real_escape_string($conn, $_POST['title']);
    $link = mysqli_real_escape_string($conn, $_POST['link']);

    $updateSql = "UPDATE notification SET title='$title', link='$link' WHERE id=$id";

    if (mysqli_query($conn, $updateSql)) {
        echo "<script>alert('Notification updated successfully'); window.location.href='notification.php';</script>";
    } else {
        echo "<div class='alert alert-danger'>Error: " . mysqli_error($conn) . "</div>";
    }
}
?>

<h4 class="page-title">NOTIFICATION</h4>
<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <div class="card-title">Edit Notification</div>
            </div>
            <form action="" method="POST" enctype="multipart/form-data">
                <div class="card-body">
                    <div class="form-group">
                        <label for="title">Title</label>
                        <input type="text" class="form-control" id="title" name="title" value="<?= htmlspecialchars($data['title']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="link">Link</label>
                        <input type="text" class="form-control" id="link" name="link" value="<?= htmlspecialchars($data['link']); ?>">
                    </div>
                    <div class="card-action">
                        <button type="submit" class="btn btn-success">Update</button>
                        <a href="notification.php" class="btn btn-danger">Cancel</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<?php include "../parts/footer.php"; ?>