<?php
require_once __DIR__ . '/../functions.php';
requireAdmin();
$page_title = 'Coupons';
$db = db();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save'])) {
    $db->prepare("INSERT INTO coupons (code, type, value, min_order_amount, max_discount_amount, max_uses, starts_at, expires_at, description) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
        ->execute([$_POST['code'], $_POST['type'], $_POST['value'], $_POST['min_order_amount'] ?: null, $_POST['max_discount_amount'] ?: null, $_POST['max_uses'] ?: null, $_POST['starts_at'] ?: null, $_POST['expires_at'] ?: null, $_POST['description']]);
    flash('success', 'Coupon created!');
    header('Location: coupons.php');
    exit;
}

if (isset($_GET['delete'])) {
    $db->prepare("DELETE FROM coupons WHERE id = ?")->execute([(int)$_GET['delete']]);
    flash('success', 'Coupon deleted!');
    header('Location: coupons.php');
    exit;
}

$coupons = $db->query("SELECT * FROM coupons ORDER BY created_at DESC")->fetchAll();

require_once __DIR__ . '/../includes/admin-header.php';
?>
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
    <div class="lg:col-span-2">
        <div class="bg-white rounded-xl border border-gray-200 overflow-hidden">
            <table class="w-full text-sm">
                <thead><tr class="text-left bg-gray-50 text-gray-500"><th class="px-4 py-3 font-medium">Code</th><th class="px-4 py-3 font-medium">Type</th><th class="px-4 py-3 font-medium">Value</th><th class="px-4 py-3 font-medium">Uses</th><th class="px-4 py-3 font-medium">Status</th><th class="px-4 py-3 font-medium">Expires</th><th class="px-4 py-3 font-medium"></th></tr></thead>
                <tbody>
                    <?php foreach ($coupons as $c): ?>
                    <tr class="border-t">
                        <td class="px-4 py-3 font-medium"><?= h($c['code']) ?></td>
                        <td class="px-4 py-3"><?= $c['type'] === 'percentage' ? '%' : '₹' ?></td>
                        <td class="px-4 py-3"><?= $c['type'] === 'percentage' ? $c['value'] . '%' : formatPrice($c['value']) ?></td>
                        <td class="px-4 py-3"><?= $c['used_count'] ?><?= $c['max_uses'] ? '/' . $c['max_uses'] : '' ?></td>
                        <td class="px-4 py-3"><?= $c['is_active'] ? '<span class="text-green-600 text-xs font-medium">Active</span>' : '<span class="text-gray-400 text-xs font-medium">Inactive</span>' ?></td>
                        <td class="px-4 py-3 text-xs text-gray-500"><?= $c['expires_at'] ? date('d M Y', strtotime($c['expires_at'])) : '-' ?></td>
                        <td class="px-4 py-3"><a href="coupons.php?delete=<?= $c['id'] ?>" class="text-red-500 hover:underline text-xs" onclick="return confirm('Delete?')">Delete</a></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div>
        <form method="POST" class="bg-white rounded-xl border border-gray-200 p-6">
            <h3 class="font-semibold text-gray-800 mb-4">Add Coupon</h3>
            <div class="space-y-3">
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Code</label><input type="text" name="code" required class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-1 focus:ring-primary-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Type</label>
                    <select name="type" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm">
                        <option value="fixed">Fixed (₹)</option>
                        <option value="percentage">Percentage (%)</option>
                    </select>
                </div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Value</label><input type="number" step="0.01" name="value" required class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Min Order Amount</label><input type="number" step="0.01" name="min_order_amount" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Max Discount</label><input type="number" step="0.01" name="max_discount_amount" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Max Uses</label><input type="number" name="max_uses" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Start Date</label><input type="datetime-local" name="starts_at" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Expiry Date</label><input type="datetime-local" name="expires_at" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Description</label><textarea name="description" rows="2" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm"></textarea></div>
                <button type="submit" name="save" class="w-full bg-primary-600 text-white py-2 rounded-lg text-sm font-semibold hover:bg-primary-700">Add Coupon</button>
            </div>
        </form>
    </div>
</div>
<?php require_once __DIR__ . '/../includes/admin-footer.php'; ?>
