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

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

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

/* =======================
   प्रशंसापत्र छवि प्राप्त करें
======================= */
$stmt = mysqli_prepare(
    $conn,
    "SELECT id, image FROM testimonials WHERE id = ?"
);
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

if (mysqli_num_rows($result) === 0) {
    header("Location: testimonials.php");
    exit;
}

$data = mysqli_fetch_assoc($result);
mysqli_stmt_close($stmt);

/* =======================
   केवल छवि अपडेट करें
======================= */
if (isset($_POST['update'])) {

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

        /* पुरानी छवि हटाएँ */
        if (!empty($data['image']) && file_exists("uploads/testimonials/" . $data['image'])) {
            unlink("uploads/testimonials/" . $data['image']);
        }

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

        $stmt = mysqli_prepare(
            $conn,
            "UPDATE testimonials SET image = ? WHERE id = ?"
        );
        mysqli_stmt_bind_param($stmt, "si", $imageName, $id);

        if (!mysqli_stmt_execute($stmt)) {
            die("डेटाबेस त्रुटि: " . mysqli_error($conn));
        }

        mysqli_stmt_close($stmt);
    }

    header("Location: testimonials.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: 600px;
  margin: 0 auto;
}

/* IMAGE PREVIEW */
.preview-img {
  width: 140px;
  height: 140px;
  object-fit: cover;
  border-radius: 50%;
  border: 1px solid #ddd;
  background: #fff;
}

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

  .preview-img {
    width: 100px;
    height: 100px;
  }

  .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">

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

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

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

        <div class="d-flex gap-2 flex-wrap">
          <button type="submit"
                  name="update"
                  class="btn btn-success px-4">
            <i class="bi bi-save"></i> छवि अपडेट करें
          </button>

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

      </form>

    </div>
  </div>

</div>

</body>
</html>
