<?php
include("auth.php");
include("config/db.php");

/* =======================
   सुरक्षा जाँच
======================= */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: blog_manage.php");
    exit;
}

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

/* =======================
   ब्लॉग प्राप्त करें
======================= */
$result = mysqli_query($conn, "SELECT * FROM blogs WHERE id=$id");
if (!$result || mysqli_num_rows($result) === 0) {
    header("Location: blog_manage.php");
    exit;
}
$blog = mysqli_fetch_assoc($result);

/* =======================
   ब्लॉग अपडेट करें
======================= */
if (isset($_POST['update'])) {

    $title   = mysqli_real_escape_string($conn, $_POST['title']);
    $content = mysqli_real_escape_string($conn, $_POST['heading']);

    if (!empty($_FILES['image']['name'])) {

        $imageName = time() . "_" . $_FILES['image']['name'];
        move_uploaded_file(
            $_FILES['image']['tmp_name'],
            "uploads/blogs/" . $imageName
        );

        mysqli_query($conn, "
            UPDATE blogs SET 
                image='$imageName',
                title='$title',
                description='$content',
                heading='$content'
            WHERE id=$id
        ");

    } else {

        mysqli_query($conn, "
            UPDATE blogs SET 
                title='$title',
                description='$content',
                heading='$content'
            WHERE id=$id
        ");
    }

    header("Location: blog_manage.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="hi">
<head>
<meta charset="UTF-8">
<title>ब्लॉग संपादित करें</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@300;400;600;700&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
html, body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  font-family: 'Source Sans Pro', sans-serif;
  background: #f5f7fa;
}

* {
  box-sizing: border-box;
}

.main {
  width: 100%;
  min-height: 100vh;
  padding: 30px;
}

.card {
  max-width: 1000px;
  margin: 0 auto;
}

.preview-img {
  width: 240px;
  height: 140px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid #ddd;
  background: #fff;
}

@media (max-width: 768px) {
  .main {
    padding: 15px;
  }

  .preview-img {
    width: 100%;
    height: auto;
  }
}
</style>
</head>

<body>

<div class="main">

  <div class="card shadow-sm">
    <div class="card-header bg-white">
      <h5 class="mb-0">✏️ ब्लॉग संपादित करें</h5>
    </div>

    <div class="card-body">

      <form method="POST" enctype="multipart/form-data">

        <!-- IMAGE -->
        <div class="mb-4">
          <label class="form-label fw-semibold">वर्तमान छवि</label><br>

          <?php if($blog['image']): ?>
            <img src="uploads/blogs/<?= htmlspecialchars($blog['image']) ?>"
                 class="preview-img mb-2">
          <?php else: ?>
            <p class="text-muted">कोई छवि अपलोड नहीं की गई है</p>
          <?php endif; ?>

          <input type="file" name="image" class="form-control mt-2">
        </div>

        <!-- TITLE -->
        <div class="mb-3">
          <label class="form-label fw-semibold">शीर्षक</label>
          <input type="text" name="title"
                 class="form-control form-control-lg"
                 value="<?= htmlspecialchars($blog['title']) ?>" required>
        </div>

        <!-- DESCRIPTION -->
        <div class="mb-4">
          <label class="form-label fw-semibold">विवरण</label>
          <textarea name="heading" rows="6"
                    class="form-control form-control-lg"
                    required><?= htmlspecialchars($blog['description']) ?></textarea>
        </div>

        <div class="d-flex gap-2 flex-wrap">
          <button type="submit" name="update" class="btn btn-success px-4">
            💾 ब्लॉग अपडेट करें
          </button>
          <a href="blog_manage.php" class="btn btn-secondary px-4">
            वापस जाएँ
          </a>
        </div>

      </form>

    </div>
  </div>

</div>

</body>
</html>
