<?php include 'include/navbar.php'; ?>
<?php include 'include/sidebar.php'; ?>

<div id="layoutSidenav_content">
    <main>
        <div class="container-fluid px-4">
            <h1 class="mt-4">Dashboard</h1>
            <ol class="breadcrumb mb-4">
                <li class="breadcrumb-item active">Edit Blog</li>
            </ol>

            <div class="mt-4">

<?php
// Get blog ID from URL
if (!isset($_GET['id'])) {
    echo "<p class='text-danger'>No blog ID provided.</p>";
    exit;
}

$id = $_GET['id'];

// Fetch current blog data
$query = mysqli_query($conn, "SELECT * FROM blogs WHERE id = $id");
$row = mysqli_fetch_assoc($query);

if (!$row) {
    echo "<p class='text-danger'>Blog not found.</p>";
    exit;
}

// Handle update
if (isset($_POST['submit'])) {
    $title = $conn->real_escape_string($_POST['title']);
    $shortDesc = $conn->real_escape_string($_POST['short_description']);
    $longDesc = $conn->real_escape_string($_POST['long_description']);

    $updateQuery = "";

    if (!empty($_FILES['image']['name'])) {
        // New image selected
        $imageName = $_FILES['image']['name'];
        $imageTmp = $_FILES['image']['tmp_name'];
        $imagePath = "../uploads/" . $imageName;
        move_uploaded_file($imageTmp, $imagePath);

        // Delete old image
        if (file_exists($row['image'])) {
            unlink($row['image']);
        }

        $updateQuery = "UPDATE blogs SET 
                        title = '$title', 
                        image = '$imagePath', 
                        short_description = '$shortDesc', 
                        long_description = '$longDesc' 
                        WHERE id = $id";
    } else {
        // No new image selected
        $updateQuery = "UPDATE blogs SET 
                        title = '$title', 
                        short_description = '$shortDesc', 
                        long_description = '$longDesc' 
                        WHERE id = $id";
    }

    if (mysqli_query($conn, $updateQuery)) {
          header("Location: total-blog.php");
        
        $query = mysqli_query($conn, "SELECT * FROM blogs WHERE id = $id");
        $row = mysqli_fetch_assoc($query);
    } else {
        echo "<p class='text-danger'>Error updating blog: " . mysqli_error($conn) . "</p>";
    }
}
?>

<form action="" method="POST" enctype="multipart/form-data">
  <label>Blog Title:</label>
  <input type="text" class="form-control" name="title" value="<?= htmlspecialchars($row['title']); ?>" required><br><br>

  <label>Current Image:</label><br>
  <img src="<?= $row['image']; ?>" alt="Current Image" width="150"><br><br>

  <label>Change Image:</label>
  <input type="file" class="form-control" name="image" accept="image/*"><br><br>

  <label>Short Description:</label>
  <textarea name="short_description" class="form-control" rows="3" required><?= htmlspecialchars($row['short_description']); ?></textarea><br><br>

  <label>Long Description:</label>
  <textarea name="long_description" class="form-control" rows="6" id="arContent" required><?= htmlspecialchars($row['long_description']); ?></textarea><br><br>

  <button type="submit" name="submit" class="btn btn-primary">Update Blog</button>
</form>

            </div>
        </div>
    </main>
    <?php include "include/footer.php"; ?>
</div>

<!-- CKEditor -->
<script src="//cdn.ckeditor.com/4.13.1/standard/ckeditor.js"></script>
<script>CKEDITOR.replace('arContent');</script>

        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
        <script src="js/scripts.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>
        <script src="assets/demo/chart-area-demo.js"></script>
        <script src="assets/demo/chart-bar-demo.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
        <script src="js/datatables-simple-demo.js"></script>
    </body>
</html>
