<?php
// app/Views/admin/subscription/all_invoices.php
?>
<div class="space-y-8 animate-fade-in pb-12">
    <!-- Header -->
    <div>
        <a href="/restpo/admin/subscription" class="inline-flex items-center gap-1.5 text-xs text-slate-400 hover:text-white transition mb-3">
            <i class="fas fa-chevron-left text-3xs"></i> Back to Subscription
        </a>
        <h2 class="text-xl font-bold text-white">All Billing &amp; Subscription Invoices</h2>
        <p class="text-slate-400 text-sm mt-1">Review all historical transactions and download printable invoice receipts.</p>
    </div>

    <!-- History Table -->
    <div class="rounded-2xl overflow-hidden border border-white/5" style="background: rgba(255,255,255,0.02);">
        <div class="overflow-x-auto">
            <table class="w-full text-xs">
                <thead>
                    <tr class="border-b border-white/5" style="background: rgba(255,255,255,0.015);">
                        <th class="text-left px-5 py-3.5 text-slate-400 font-bold uppercase tracking-wider">Invoice ID</th>
                        <th class="text-left px-5 py-3.5 text-slate-400 font-bold uppercase tracking-wider">Package</th>
                        <th class="text-left px-5 py-3.5 text-slate-400 font-bold uppercase tracking-wider">Purchase Date</th>
                        <th class="text-left px-5 py-3.5 text-slate-400 font-bold uppercase tracking-wider">Billing Duration</th>
                        <th class="text-left px-5 py-3.5 text-slate-400 font-bold uppercase tracking-wider">Amount Paid</th>
                        <th class="text-left px-5 py-3.5 text-slate-400 font-bold uppercase tracking-wider">Status</th>
                        <th class="text-right px-5 py-3.5 text-slate-400 font-bold uppercase tracking-wider">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/5">
                    <?php if (empty($invoices)): ?>
                    <tr>
                        <td colspan="7" class="px-5 py-10 text-center text-slate-500 font-medium">No invoices found.</td>
                    </tr>
                    <?php endif; ?>
                    <?php foreach ($invoices as $h): 
                        $statusStyle = '';
                        if ($h['status'] === 'active') {
                            $statusStyle = 'background: rgba(16,185,129,0.15); color: #6ee7b7; border-color: rgba(16,185,129,0.25);';
                        } elseif ($h['status'] === 'expired') {
                            $statusStyle = 'background: rgba(239,68,68,0.15); color: #fca5a5; border-color: rgba(239,68,68,0.25);';
                        } elseif ($h['status'] === 'pending') {
                            $statusStyle = 'background: rgba(234,179,8,0.15); color: #facc15; border-color: rgba(234,179,8,0.25);';
                        } else {
                            $statusStyle = 'background: rgba(255,255,255,0.06); color: #94a3b8; border-color: rgba(255,255,255,0.1);';
                        }
                    ?>
                    <tr class="hover:bg-white/[0.015] transition">
                        <td class="px-5 py-3.5 text-slate-400 font-mono">
                            #SUB-<?php echo str_pad($h['id'], 5, '0', STR_PAD_LEFT); ?>
                        </td>
                        <td class="px-5 py-3.5 font-semibold text-white">
                            <?php echo htmlspecialchars($h['package_name']); ?>
                        </td>
                        <td class="px-5 py-3.5 text-slate-300">
                            <?php echo date('M d, Y', strtotime($h['start_date'])); ?>
                        </td>
                        <td class="px-5 py-3.5 text-slate-400">
                            <?php echo date('M d, Y', strtotime($h['start_date'])) . ' — ' . date('M d, Y', strtotime($h['end_date'])); ?>
                        </td>
                        <td class="px-5 py-3.5 font-bold text-white">
                            <?php echo htmlspecialchars(FeatureGate::currency($_SESSION['restaurant_id'])) . number_format($h['amount'], 2); ?>
                        </td>
                        <td class="px-5 py-3.5">
                            <span class="px-2.5 py-1 rounded-full text-3xs font-extrabold uppercase border" style="<?php echo $statusStyle; ?>">
                                <?php echo htmlspecialchars($h['status']); ?>
                            </span>
                        </td>
                        <td class="px-5 py-3.5 text-right">
                            <a href="/restpo/admin/subscription/invoice?id=<?php echo $h['id']; ?>" target="_blank" class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-slate-400 hover:text-white bg-white/5 hover:bg-white/10 border border-white/10 transition font-semibold">
                                <i class="fas fa-file-invoice text-xs"></i> Invoice
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
