<?php
$products = json_decode(file_get_contents('../js/products.json'), true);
$product = current(array_filter($products, fn($p) => $p['id'] == $_GET['id']));
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Edit Product</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
  <style>
    body {
      font-family: 'Inter', sans-serif;
      background: linear-gradient(to right, #0f172a, #1e293b);
    }
    .glass {
      background: rgba(255, 255, 255, 0.05);
      backdrop-filter: blur(20px);
      border: 1px solid rgba(255, 255, 255, 0.1);
    }
    input:focus {
      box-shadow: 0 0 0 3px rgba(34, 211, 238, 0.6);
      outline: none;
    }
  </style>
</head>
<body class="text-white min-h-screen flex items-center justify-center p-6">
  <div class="w-full max-w-2xl glass rounded-2xl shadow-2xl p-8">
    <h1 class="text-3xl font-extrabold mb-8 text-cyan-300 text-center">Edit Product</h1>
    <form action="process_product.php" method="POST" class="space-y-6">
      <input type="hidden" name="id" value="<?= $product['id'] ?>">

      <div>
        <label class="block text-sm font-medium mb-2 text-cyan-100">Product Name</label>
        <input type="text" name="name" value="<?= htmlspecialchars($product['name']) ?>" required
               class="w-full px-4 py-3 rounded-lg bg-gray-900 text-white border border-gray-600 focus:ring-2 focus:ring-cyan-400">
      </div>

      <div class="grid grid-cols-2 gap-4">
        <div>
          <label class="block text-sm font-medium mb-2 text-cyan-100">Selling Price</label>
          <input type="number" name="selling_price" value="<?= $product['selling_price'] ?>" required
                 class="w-full px-4 py-3 rounded-lg bg-gray-900 text-white border border-gray-600 focus:ring-2 focus:ring-cyan-400">
        </div>
        <div>
          <label class="block text-sm font-medium mb-2 text-cyan-100">MRP</label>
          <input type="number" name="mrp" value="<?= $product['mrp'] ?>" required
                 class="w-full px-4 py-3 rounded-lg bg-gray-900 text-white border border-gray-600 focus:ring-2 focus:ring-cyan-400">
        </div>
      </div>

      <div>
        <label class="block text-sm font-medium mb-2 text-cyan-100">Image URLs</label>
        <div class="space-y-3">
          <?php for ($i = 1; $i <= 5; $i++): ?>
            <input type="url" name="img<?= $i ?>" value="<?= $product["img$i"] ?? '' ?>"
                   placeholder="Image <?= $i ?> URL"
                   class="w-full px-4 py-3 rounded-lg bg-gray-900 text-white border border-gray-600 focus:ring-2 focus:ring-cyan-400">
          <?php endfor; ?>
        </div>
      </div>

      <div class="flex justify-between items-center pt-4">
        <a href="main.php"
           class="px-6 py-3 bg-gray-700 hover:bg-gray-600 rounded-lg text-sm font-semibold transition shadow">
          Cancel
        </a>
        <button type="submit" name="action" value="update"
                class="px-6 py-3 bg-cyan-500 hover:bg-cyan-600 text-black font-semibold rounded-lg shadow-lg transition">
          Save Changes
        </button>
      </div>
    </form>
  </div>
</body>
</html>