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

// Fetch promotions for the logged-in restaurant owner
$restaurant_id = $_SESSION['restaurant_id'];
$stmt = db()->prepare("SELECT sp.* FROM special_promotions sp JOIN menu_items mi ON sp.id = mi.promotion_id JOIN restaurants r ON mi.restaurant_id = r.id WHERE r.id = ?");
$stmt->execute([$restaurant_id]);
$promotions = $stmt->fetchAll();

?>

<div class="container mt-4">
    <h2>Manage Promotions</h2>
    <a href="add_promotion.php" class="btn btn-primary mb-3">Add Promotion</a>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>Description</th>
                <th>Discount</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Active</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($promotions as $promotion): ?>
                <tr>
                    <td><?= htmlspecialchars($promotion['name']) ?></td>
                    <td><?= htmlspecialchars($promotion['description']) ?></td>
                    <td><?= htmlspecialchars($promotion['discount_value']) ?> <?= $promotion['discount_type'] == 'percentage' ? '%' : '' ?></td>
                    <td><?= htmlspecialchars($promotion['start_date']) ?></td>
                    <td><?= htmlspecialchars($promotion['end_date']) ?></td>
                    <td><?= $promotion['is_active'] ? 'Yes' : 'No' ?></td>
                    <td>
                        <a href="edit_promotion.php?id=<?= $promotion['id'] ?>" class="btn btn-sm btn-info">Edit</a>
                        <a href="delete_promotion.php?id=<?= $promotion['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">Delete</a>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

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