<div class="space-y-6">
    <div class="flex justify-between items-center">
        <h1 class="text-xl font-bold text-white flex items-center gap-2">
            <i class="fas fa-ticket-alt text-emerald-400"></i>
            <span>Discount Coupons Manager</span>
        </h1>
        <div class="text-xs text-slate-400">Manage promotional codes and floor-specific rules</div>
    </div>

    <?php if(isset($_GET['msg'])): ?>
    <div class="p-3.5 rounded-xl text-xs font-semibold" style="background:rgba(16,185,129,0.12); color:#6ee7b7; border:1px solid rgba(16,185,129,0.2)">
        <i class="fas fa-check-circle mr-1"></i> <?php echo htmlspecialchars($_GET['msg']); ?>
    </div>
    <?php endif; ?>

    <?php if(isset($_GET['error'])): ?>
    <div class="p-3.5 rounded-xl text-xs font-semibold" style="background:rgba(239,68,68,0.12); color:#f87171; border:1px solid rgba(239,68,68,0.2)">
        <i class="fas fa-exclamation-circle mr-1"></i> <?php echo htmlspecialchars($_GET['error']); ?>
    </div>
    <?php endif; ?>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Coupon Form -->
        <div class="rounded-2xl p-5" style="background:rgba(255,255,255,0.03); border:1px solid rgba(255,255,255,0.06)">
            <h3 class="text-sm font-bold text-white mb-4 flex items-center gap-2">
                <i class="fas fa-plus-circle text-emerald-400"></i> Create Coupon
            </h3>
            
            <form action="<?= BASE_URL ?>/admin/coupons/store" method="POST" class="space-y-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-400 mb-1">Coupon Code</label>
                    <input type="text" name="code" required maxlength="30" class="form-input uppercase" placeholder="e.g. SPECIAL10">
                </div>

                <div>
                    <label class="block text-xs font-semibold text-slate-400 mb-1">Floor (Branch)</label>
                    <select name="floor_id" class="form-input" style="background:#0d2137">
                        <option value="">— All Branches (All Floors) —</option>
                        <?php foreach($floors as $f): ?>
                            <option value="<?php echo $f['id']; ?>"><?php echo htmlspecialchars($f['name']); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div>
                    <label class="block text-xs font-semibold text-slate-400 mb-1">Restrict to Category <span class="text-slate-600">(optional)</span></label>
                    <select name="category_id" class="form-input" style="background:#0d2137">
                        <option value="">— All Categories —</option>
                        <?php foreach($categories as $cat): ?>
                            <option value="<?php echo $cat['id']; ?>"><?php echo htmlspecialchars($cat['name']); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div>
                    <label class="block text-xs font-semibold text-slate-400 mb-1">Restrict to Product <span class="text-slate-600">(optional)</span></label>
                    <select name="product_id" class="form-input" style="background:#0d2137">
                        <option value="">— All Products —</option>
                        <?php foreach($products as $p): ?>
                            <option value="<?php echo $p['id']; ?>"><?php echo htmlspecialchars($p['name']); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="grid grid-cols-2 gap-3">
                    <div>
                        <label class="block text-xs font-semibold text-slate-400 mb-1">Discount Type</label>
                        <select name="type" class="form-input" style="background:#0d2137">
                            <option value="percentage">Percentage (%)</option>
                            <option value="fixed">Fixed (<?php echo $currency; ?>)</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-xs font-semibold text-slate-400 mb-1">Value</label>
                        <input type="number" step="0.01" name="value" required min="0.01" class="form-input" placeholder="e.g. 10">
                    </div>
                </div>

                <div class="border-t border-white/5 pt-3 space-y-3">
                    <span class="block text-[10px] font-bold uppercase tracking-wider text-emerald-400">Match Conditions</span>
                    
                    <div>
                        <label class="block text-xs font-semibold text-slate-400 mb-1">Minimum Order Amount</label>
                        <input type="number" step="0.01" name="min_order_amount" value="0.00" min="0" class="form-input" placeholder="e.g. 500">
                        <p class="text-[9px] text-slate-500 mt-0.5">Coupon applies only if order total matches or exceeds this amount.</p>
                    </div>

                    <div>
                        <label class="block text-xs font-semibold text-slate-400 mb-1.5">Applicable Days</label>
                        <div class="grid grid-cols-2 gap-1 text-[11px] font-medium text-slate-300">
                            <?php 
                            $days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
                            foreach($days as $day): ?>
                                <label class="flex items-center gap-1.5 cursor-pointer py-1 px-1 hover:bg-white/5 rounded transition">
                                    <input type="checkbox" name="applicable_days[]" value="<?php echo $day; ?>" checked class="accent-emerald-500">
                                    <span><?php echo $day; ?></span>
                                </label>
                            <?php endforeach; ?>
                        </div>
                        <p class="text-[9px] text-slate-500 mt-1">Uncheck days to restrict the coupon.</p>
                    </div>
                </div>

                <div class="border-t border-white/5 pt-3">
                    <label class="block text-xs font-semibold text-slate-400 mb-1">Expiry Date</label>
                    <input type="date" name="expiry_date" required class="form-input" min="<?php echo date('Y-m-d'); ?>" value="<?php echo date('Y-m-d', strtotime('+30 days')); ?>">
                </div>

                <button type="submit" class="btn-success w-full mt-2">
                    <i class="fas fa-check mr-1"></i> Save Coupon
                </button>
            </form>
        </div>

        <!-- Coupons List -->
        <div class="lg:col-span-2 space-y-4">
            <div class="rounded-2xl overflow-hidden border border-white/6" style="background:rgba(255,255,255,0.01)">
                <table class="w-full text-xs text-left border-collapse">
                    <thead>
                        <tr class="bg-white/5 border-b border-white/5 text-slate-400 font-bold uppercase tracking-wider text-[10px]">
                            <th class="px-4 py-3">Code</th>
                            <th class="px-4 py-3">Floor (Branch)</th>
                            <th class="px-4 py-3">Discount</th>
                            <th class="px-4 py-3">Conditions</th>
                            <th class="px-4 py-3">Expires</th>
                            <th class="px-4 py-3">Status</th>
                            <th class="px-4 py-3 text-center">Action</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-white/5 text-slate-300">
                        <?php foreach($coupons as $c): ?>
                        <tr class="hover:bg-white/5 transition">
                            <td class="px-4 py-3 font-bold text-white uppercase"><?php echo htmlspecialchars($c['code']); ?></td>
                            <td class="px-4 py-3 font-semibold text-slate-400">
                                <?php echo $c['floor_name'] ? htmlspecialchars($c['floor_name']) : '<span class="text-emerald-400/80">All Branches</span>'; ?>
                            </td>
                            <td class="px-4 py-3 font-bold text-white">
                                <?php echo $c['type'] === 'percentage' ? $c['value'] . '%' : $currency . number_format($c['value'], 2); ?>
                            </td>
                            <td class="px-4 py-3 space-y-1">
                                <?php if ($c['min_order_amount'] > 0): ?>
                                    <div class="text-[10px]"><span class="text-slate-500 font-bold">Min:</span> <span class="font-bold text-white"><?php echo $currency . number_format($c['min_order_amount'], 2); ?></span></div>
                                <?php endif; ?>
                                <?php if ($c['category_name']): ?>
                                    <div class="text-[10px]"><span class="text-slate-500 font-bold">Category:</span> <span class="font-bold text-yellow-400"><?php echo htmlspecialchars($c['category_name']); ?></span></div>
                                <?php endif; ?>
                                <?php if ($c['product_name']): ?>
                                    <div class="text-[10px]"><span class="text-slate-500 font-bold">Product:</span> <span class="font-bold text-blue-400"><?php echo htmlspecialchars($c['product_name']); ?></span></div>
                                <?php endif; ?>
                                <?php if ($c['applicable_days']): ?>
                                    <div class="text-[9px] text-slate-400 font-medium leading-tight">
                                        <i class="fas fa-calendar-alt text-emerald-400 mr-0.5"></i>
                                        <?php 
                                        $activeDays = explode(',', $c['applicable_days']);
                                        if (count($activeDays) === 7) {
                                            echo "All Days";
                                        } else {
                                            echo implode(', ', array_map(fn($d) => substr($d, 0, 3), $activeDays));
                                        }
                                        ?>
                                    </div>
                                <?php endif; ?>
                            </td>
                            <td class="px-4 py-3 font-medium text-slate-400">
                                <?php 
                                $expired = strtotime($c['expiry_date']) < strtotime(date('Y-m-d'));
                                if ($expired): ?>
                                    <span class="text-red-400 line-through font-bold"><?php echo date('M d, Y', strtotime($c['expiry_date'])); ?></span>
                                <?php else: ?>
                                    <?php echo date('M d, Y', strtotime($c['expiry_date'])); ?>
                                <?php endif; ?>
                            </td>
                            <td class="px-4 py-3">
                                <form action="<?= BASE_URL ?>/admin/coupons/toggle" method="POST" class="inline">
                                    <input type="hidden" name="id" value="<?php echo $c['id']; ?>">
                                    <input type="hidden" name="status" value="<?php echo $c['status'] === 'active' ? 'inactive' : 'active'; ?>">
                                    <button type="submit" class="px-2 py-1 rounded text-[10px] font-bold uppercase transition" 
                                            style="background:<?php echo $c['status'] === 'active' ? 'rgba(16,185,129,0.12)' : 'rgba(239,68,68,0.12)'; ?>; 
                                                   color:<?php echo $c['status'] === 'active' ? '#6ee7b7' : '#f87171'; ?>;
                                                   border: 1px solid <?php echo $c['status'] === 'active' ? 'rgba(16,185,129,0.2)' : 'rgba(239,68,68,0.2)'; ?>;">
                                        <?php echo htmlspecialchars($c['status']); ?>
                                    </button>
                                </form>
                            </td>
                            <td class="px-4 py-3 text-center">
                                <form action="<?= BASE_URL ?>/admin/coupons/delete" method="POST" class="inline" onsubmit="return confirm('Delete this coupon?')">
                                    <input type="hidden" name="id" value="<?php echo $c['id']; ?>">
                                    <button type="submit" class="text-slate-500 hover:text-red-400 transition py-1 px-2 text-xs">
                                        <i class="fas fa-trash-alt"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php if(count($coupons) === 0): ?>
                        <tr>
                            <td colspan="7" class="text-center py-10 text-slate-500 font-semibold">No coupons configured</td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
