<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Coupons - RestPO SuperAdmin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>body { font-family: 'Inter', sans-serif; background-color: #f3f4f6; }</style>
</head>
<body class="p-6">
    <div class="max-w-6xl mx-auto">
        <div class="flex justify-between items-center mb-6">
            <h1 class="text-2xl font-bold text-gray-800">Discount Coupons</h1>
            <a href="/restpo/dashboard" class="text-gray-500 hover:text-gray-700">Back to Dashboard</a>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <!-- Add Coupon Form -->
            <div class="bg-white p-6 rounded-xl shadow-sm border border-gray-100">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">Create Coupon</h3>
                <form action="/restpo/superadmin/coupons/store" method="POST">
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1">Coupon Code</label>
                        <input type="text" name="code" required class="w-full px-4 py-2 border rounded-lg outline-none focus:ring-2 focus:ring-purple-500 uppercase" placeholder="e.g. SUMMER50">
                    </div>
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1">Discount Type</label>
                        <select name="type" class="w-full px-4 py-2 border rounded-lg outline-none focus:ring-2 focus:ring-purple-500 bg-white">
                            <option value="percentage">Percentage (%)</option>
                            <option value="fixed">Fixed Amount ($)</option>
                        </select>
                    </div>
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1">Discount Value</label>
                        <input type="number" step="0.01" name="value" required class="w-full px-4 py-2 border rounded-lg outline-none focus:ring-2 focus:ring-purple-500">
                    </div>
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1">Expiry Date</label>
                        <input type="date" name="expiry_date" required class="w-full px-4 py-2 border rounded-lg outline-none focus:ring-2 focus:ring-purple-500">
                    </div>
                    <div class="mb-6">
                        <label class="block text-sm font-medium text-gray-700 mb-1">Usage Limit (Optional)</label>
                        <input type="number" name="usage_limit" class="w-full px-4 py-2 border rounded-lg outline-none focus:ring-2 focus:ring-purple-500" placeholder="Leave empty for unlimited">
                    </div>
                    <button type="submit" class="w-full bg-purple-600 hover:bg-purple-700 text-white font-medium py-2 rounded-lg transition">Save Coupon</button>
                </form>
            </div>

            <!-- Coupon List -->
            <div class="lg:col-span-2 bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
                <table class="w-full text-left border-collapse">
                    <thead class="bg-gray-50 text-gray-500 text-xs uppercase tracking-wider">
                        <tr>
                            <th class="px-6 py-4 font-medium">Code</th>
                            <th class="px-6 py-4 font-medium">Discount</th>
                            <th class="px-6 py-4 font-medium">Expiry Date</th>
                            <th class="px-6 py-4 font-medium">Usage</th>
                            <th class="px-6 py-4 font-medium">Status</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php foreach($coupons as $coupon): ?>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 font-bold text-gray-900"><?php echo htmlspecialchars($coupon['code']); ?></td>
                            <td class="px-6 py-4 text-gray-600">
                                <?php echo $coupon['type'] === 'percentage' ? $coupon['value'] . '%' : '$' . number_format($coupon['value'], 2); ?>
                            </td>
                            <td class="px-6 py-4 text-gray-600"><?php echo date('M d, Y', strtotime($coupon['expiry_date'])); ?></td>
                            <td class="px-6 py-4 text-gray-600">
                                <?php echo $coupon['used_count']; ?> / <?php echo $coupon['usage_limit'] ? $coupon['usage_limit'] : '∞'; ?>
                            </td>
                            <td class="px-6 py-4">
                                <span class="px-2 py-1 text-xs font-semibold rounded-full <?php echo $coupon['status'] == 'active' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; ?>">
                                    <?php echo htmlspecialchars($coupon['status']); ?>
                                </span>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>
