<?php
include "auth.php";
include "db.php";

$id = $_GET['id'] ?? 0;


$result = $conn->query("
SELECT * FROM offers WHERE id='$id' LIMIT 1
");

$offer = $result->fetch_assoc();

if(!$offer){
    die("Offer not found");
}


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

    $product_id = $_POST['product_id'];
    $offer_title = $_POST['offer_title'];
    $badge = $_POST['badge'];
    $old_price = $_POST['old_price'];
    $new_price = $_POST['new_price'];
    $offer_text = $_POST['offer_text'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];

    $conn->query("
    UPDATE offers SET
        product_id='$product_id',
        offer_title='$offer_title',
        badge='$badge',
        old_price='$old_price',
        new_price='$new_price',
        offer_text='$offer_text',
        start_date='$start_date',
        end_date='$end_date'
    WHERE id='$id'
    ");

    header("Location: offers.php");
    exit;
}


$products = $conn->query("SELECT id, name FROM products");
?>

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description"
content="Maria Curtains offers quality curtains, sheers, curtain rods, mats and home décor in Thika, Kenya. Affordable prices, custom fitting and professional installation.">
  
<title>Edit Offer</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">


   <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&family=Playfair+Display:wght@500;600;700&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&family=Playfair+Display:wght@500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="admin.css">
</head>

<body>

<div class="admin-container">
<button class="menu-toggle" id="menu-toggle">
    <i class="fa-solid fa-bars"></i>
</button>

  <aside class="sidebar" id="sidebar">

    <h2 class="logo">Maria Admin</h2>

   <nav>
      <a href="dashboard.php">Dashboard</a>
      <a href="addproducts.php">Add Product</a>
      <a href="add_offer.php">Add Offer</a>
      <a href="offers.php" class="active">Offers</a>
      <a href="products.php">Products</a>
      <a href="categoriesadmin.php">Categories</a>
      <a href="settings.php">Settings</a>
      <a href="logout.php" class="logout">Logout</a>
    </nav>

  </aside>

  
  <main class="main-content">

    <h1>Edit Offer</h1>

    <div class="form-card">

      <form method="POST" class="admin-form">

        
        <label>Product</label>
        <select name="product_id" required>

          <?php while($p = $products->fetch_assoc()): ?>

            <option value="<?= $p['id'] ?>"
              <?= $p['id'] == $offer['product_id'] ? 'selected' : '' ?>>

              <?= $p['name'] ?>

            </option>

          <?php endwhile; ?>

        </select>

        
        <label>Offer Title</label>
        <input type="text" name="offer_title"
        value="<?= $offer['offer_title'] ?>" required>

        
        <label>Badge</label>
        <input type="text" name="badge"
        value="<?= $offer['badge'] ?>">

        
        <label>Old Price</label>
        <input type="number" name="old_price"
        value="<?= $offer['old_price'] ?>">

        
        <label>New Price</label>
        <input type="number" name="new_price"
        value="<?= $offer['new_price'] ?>">

        
        <label>Offer Text</label>
        <textarea name="offer_text" rows="5"><?= $offer['offer_text'] ?></textarea>

        
        <label>Start Date</label>
        <input type="date" name="start_date"
        value="<?= $offer['start_date'] ?>">

        
        <label>End Date</label>
        <input type="date" name="end_date"
        value="<?= $offer['end_date'] ?>">

        
        <button type="submit" name="update">
          Update Offer
        </button>

      </form>

    </div>

  </main>

</div>
  <script>
const menuToggle = document.getElementById("menu-toggle");
const sidebar = document.getElementById("sidebar");
const icon = menuToggle.querySelector("i");

menuToggle.addEventListener("click", function(){

    sidebar.classList.toggle("active");

    if(sidebar.classList.contains("active")){

        icon.classList.remove("fa-bars");
        icon.classList.add("fa-xmark");

    }else{

        icon.classList.remove("fa-xmark");
        icon.classList.add("fa-bars");

    }

});

</script>
</body>
</html>