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

if (isset($_POST['submit'])) {

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

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

    mysqli_query($conn, "
        INSERT INTO blogs (image, title, description, heading)
        VALUES ('$imageName', '$title', '$content', '$content')
    ");

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

<!DOCTYPE html>
<html lang="hi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ब्लॉग जोड़ें</title>

<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">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.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;
}

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

  .btn {
    width: 100%;
    margin-bottom: 10px;
  }
}
</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-3">
          <label class="form-label fw-semibold">ब्लॉग छवि</label>
          <input type="file" name="image"
                 class="form-control form-control-lg" required>
        </div>

        <!-- TITLE -->
        <div class="mb-3">
          <label class="form-label fw-semibold">ब्लॉग शीर्षक</label>
          <input type="text" name="title"
                 class="form-control form-control-lg" required>
        </div>

        <!-- DESCRIPTION / CONTENT -->
        <div class="mb-4">
          <label class="form-label fw-semibold">विवरण</label>
          <textarea name="heading" rows="6"
                    class="form-control form-control-lg"
                    placeholder="पूरा ब्लॉग कंटेंट"
                    required></textarea>
        </div>

        <div class="d-flex flex-wrap gap-2">
          <button type="submit" name="submit"
                  class="btn btn-success px-4">
            <i class="bi bi-check-circle"></i> ब्लॉग सेव करें
          </button>

          <a href="blog_manage.php"
             class="btn btn-secondary px-4">
            वापस जाएँ
          </a>
        </div>

      </form>

    </div>
  </div>

</div>

</body>
</html>
