<?php
require_once 'header.php';
require_once '../db/config.php';

$promotion_id = $_GET['id'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'];
    $description = $_POST['description'];
    $discount_type = $_POST['discount_type'];
    $discount_value = $_POST['discount_value'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    $is_active = isset($_POST['is_active']) ? 1 : 0;

    $stmt = db()->prepare("UPDATE special_promotions SET name = ?, description = ?, discount_type = ?, discount_value = ?, start_date = ?, end_date = ?, is_active = ? WHERE id = ?");
    $stmt->execute([$name, $description, $discount_type, $discount_value, $start_date, $end_date, $is_active, $promotion_id]);

    header('Location: promotions.php');
    exit;
}

$stmt = db()->prepare("SELECT * FROM special_promotions WHERE id = ?");
$stmt->execute([$promotion_id]);
$promotion = $stmt->fetch();

?>

<div class="container mt-4">
    <h2>Edit Promotion</h2>
    <form method="post">
        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" class="form-control" id="name" name="name" value="<?= htmlspecialchars($promotion['name']) ?>" required>
        </div>
        <div class="form-group">
            <label for="description">Description</label>
            <textarea class="form-control" id="description" name="description"><?= htmlspecialchars($promotion['description']) ?></textarea>
        </div>
        <div class="form-group">
            <label for="discount_type">Discount Type</label>
            <select class="form-control" id="discount_type" name="discount_type">
                <option value="percentage" <?= $promotion['discount_type'] == 'percentage' ? 'selected' : '' ?>>Percentage</option>
                <option value="fixed" <?= $promotion['discount_type'] == 'fixed' ? 'selected' : '' ?>>Fixed</option>
            </select>
        </div>
        <div class="form-group">
            <label for="discount_value">Discount Value</label>
            <input type="number" class="form-control" id="discount_value" name="discount_value" step="0.01" value="<?= htmlspecialchars($promotion['discount_value']) ?>" required>
        </div>
        <div class="form-group">
            <label for="start_date">Start Date</label>
            <input type="datetime-local" class="form-control" id="start_date" name="start_date" value="<?= date('Y-m-d\TH:i', strtotime($promotion['start_date'])) ?>" required>
        </div>
        <div class="form-group">
            <label for="end_date">End Date</label>
            <input type="datetime-local" class="form-control" id="end_date" name="end_date" value="<?= date('Y-m-d\TH:i', strtotime($promotion['end_date'])) ?>" required>
        </div>
        <div class="form-check">
            <input type="checkbox" class="form-check-input" id="is_active" name="is_active" value="1" <?= $promotion['is_active'] ? 'checked' : '' ?>>
            <label class="form-check-label" for="is_active">Active</label>
        </div>
        <button type="submit" class="btn btn-primary">Update Promotion</button>
    </form>
</div>

<?php require_once 'footer.php'; ?>