<?php
// app/Views/superadmin/restaurants/billing.php
$currency = $restaurant['currency'] ?? '$';
$today = time();
?>
<div class="space-y-6">
    <!-- Breadcrumb / Header -->
    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
        <div>
            <div class="flex items-center gap-2 text-slate-400 text-xs">
                <a href="/restpo/superadmin/restaurants" class="hover:text-purple-400 transition">Restaurants</a>
                <i class="fas fa-chevron-right text-3xs text-slate-600"></i>
                <span class="text-slate-300">Billing History</span>
            </div>
            <h2 class="text-xl font-bold text-white mt-1 flex items-center gap-2">
                <i class="fas fa-credit-card text-purple-400"></i>
                <?php echo htmlspecialchars($restaurant['name']); ?> Billing Details
            </h2>
            <p class="text-slate-400 text-xs mt-0.5">Manage subscription payments and view granular revenue stats.</p>
        </div>
        <a href="/restpo/superadmin/restaurants" class="inline-flex items-center gap-1.5 py-2 px-4 rounded-xl text-xs font-semibold text-slate-300 bg-white/5 border border-white/10 hover:bg-white/10 transition active:scale-95">
            <i class="fas fa-arrow-left"></i> Back to List
        </a>
    </div>

    <!-- Stats Grid -->
    <div class="grid grid-cols-2 lg:grid-cols-4 gap-4">
        <!-- Today's Revenue -->
        <div class="stat-card p-5 relative overflow-hidden group">
            <div class="absolute -right-6 -bottom-6 w-20 h-20 bg-emerald-500/5 rounded-full blur-xl group-hover:bg-emerald-500/10 transition duration-300"></div>
            <p class="text-3xs font-extrabold uppercase tracking-wider text-slate-500">Today's Revenue</p>
            <p class="text-lg font-black text-white mt-2"><?php echo $currency . number_format($revenueStats['today_revenue'], 2); ?></p>
            <div class="text-3xs text-emerald-400 mt-2 flex items-center gap-1">
                <i class="fas fa-calendar-day"></i> Single Day Sum
            </div>
        </div>

        <!-- This Week's Revenue -->
        <div class="stat-card p-5 relative overflow-hidden group">
            <div class="absolute -right-6 -bottom-6 w-20 h-20 bg-blue-500/5 rounded-full blur-xl group-hover:bg-blue-500/10 transition duration-300"></div>
            <p class="text-3xs font-extrabold uppercase tracking-wider text-slate-500">This Week's Revenue</p>
            <p class="text-lg font-black text-white mt-2"><?php echo $currency . number_format($revenueStats['week_revenue'], 2); ?></p>
            <div class="text-3xs text-blue-400 mt-2 flex items-center gap-1">
                <i class="fas fa-calendar-week"></i> Mon - Sun Cycle
            </div>
        </div>

        <!-- This Month's Revenue -->
        <div class="stat-card p-5 relative overflow-hidden group">
            <div class="absolute -right-6 -bottom-6 w-20 h-20 bg-purple-500/5 rounded-full blur-xl group-hover:bg-purple-500/10 transition duration-300"></div>
            <p class="text-3xs font-extrabold uppercase tracking-wider text-slate-500">This Month's Revenue</p>
            <p class="text-lg font-black text-white mt-2"><?php echo $currency . number_format($revenueStats['month_revenue'], 2); ?></p>
            <div class="text-3xs text-purple-400 mt-2 flex items-center gap-1">
                <i class="fas fa-calendar-alt"></i> Calendar Month
            </div>
        </div>

        <!-- All-Time Revenue -->
        <div class="stat-card p-5 relative overflow-hidden group">
            <div class="absolute -right-6 -bottom-6 w-20 h-20 bg-amber-500/5 rounded-full blur-xl group-hover:bg-amber-500/10 transition duration-300"></div>
            <p class="text-3xs font-extrabold uppercase tracking-wider text-slate-500">All-Time Revenue</p>
            <p class="text-lg font-black text-white mt-2"><?php echo $currency . number_format($revenueStats['all_time_revenue'], 2); ?></p>
            <div class="text-3xs text-amber-400 mt-2 flex items-center gap-1">
                <i class="fas fa-coins"></i> Total Platform Paid
            </div>
        </div>
    </div>

    <!-- Filter Bar & Custom Range Card -->
    <div class="rounded-2xl p-5 border border-white/5 bg-white/[0.02] flex flex-col md:flex-row md:items-center justify-between gap-5">
        <form action="/restpo/superadmin/restaurants/billing" method="GET" class="flex flex-wrap items-end gap-3 flex-1">
            <input type="hidden" name="id" value="<?php echo $restaurant['id']; ?>">
            
            <div class="space-y-1.5">
                <label class="block text-3xs uppercase tracking-wider text-slate-400 font-bold">Start Date</label>
                <input 
                    type="date" 
                    name="start_date" 
                    value="<?php echo htmlspecialchars($startDate ?? ''); ?>"
                    class="form-input text-xs py-1.5 px-3 rounded-xl bg-white/5 border-white/10 text-white w-40 animate-none" 
                    required
                />
            </div>
            
            <div class="space-y-1.5">
                <label class="block text-3xs uppercase tracking-wider text-slate-400 font-bold">End Date</label>
                <input 
                    type="date" 
                    name="end_date" 
                    value="<?php echo htmlspecialchars($endDate ?? ''); ?>"
                    class="form-input text-xs py-1.5 px-3 rounded-xl bg-white/5 border-white/10 text-white w-40 animate-none" 
                    required
                />
            </div>

            <div class="flex items-center gap-2">
                <button type="submit" class="text-xs font-semibold py-2 px-4 rounded-xl text-white bg-purple-600 hover:bg-purple-700 transition flex items-center gap-1">
                    <i class="fas fa-filter"></i> Filter Range
                </button>
                <?php if ($startDate || $endDate): ?>
                    <a href="/restpo/superadmin/restaurants/billing?id=<?php echo $restaurant['id']; ?>" class="text-xs font-semibold py-2 px-4 rounded-xl text-slate-300 bg-white/5 border border-white/10 hover:bg-white/10 transition">
                        Reset
                    </a>
                <?php endif; ?>
            </div>
        </form>

        <?php if ($startDate && $endDate): ?>
        <div class="stat-card px-5 py-3 flex items-center gap-4 bg-purple-500/[0.03] border-purple-500/20 md:w-80">
            <div class="w-9 h-9 rounded-xl bg-purple-500/10 flex items-center justify-center text-purple-400">
                <i class="fas fa-calendar-check text-sm"></i>
            </div>
            <div>
                <p class="text-4xs font-bold uppercase tracking-widest text-slate-500">Range Revenue</p>
                <p class="text-sm font-black text-white"><?php echo $currency . number_format($customRevenue, 2); ?></p>
                <p class="text-4xs text-slate-400 truncate"><?php echo htmlspecialchars($startDate) . ' to ' . htmlspecialchars($endDate); ?></p>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <!-- Active Subscription details & Subscription History Logs -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <div class="lg:col-span-1 stat-card p-5 space-y-4 h-fit">
            <h3 class="text-sm font-bold text-white border-b border-white/5 pb-2 flex items-center gap-2">
                <i class="fas fa-award text-purple-400"></i> Active Subscription
            </h3>
            <?php if ($activeSub): 
                $expiry = strtotime($activeSub['end_date']);
                $daysLeft = ceil(($expiry - $today) / 86400);
                $expired = $daysLeft < 0;
                $warning = $daysLeft >= 0 && $daysLeft <= 7;
            ?>
                <div class="space-y-3">
                    <div class="flex justify-between items-center">
                        <span class="text-slate-400 text-xs">Plan Name:</span>
                        <span class="px-2 py-0.5 rounded bg-purple-500/10 border border-purple-500/20 text-purple-400 font-extrabold text-xs">
                            <?php echo htmlspecialchars($activeSub['package_name']); ?>
                        </span>
                    </div>
                    <div class="flex justify-between items-center">
                        <span class="text-slate-400 text-xs">Amount:</span>
                        <span class="text-white font-extrabold text-xs">
                            <?php echo $currency . number_format($activeSub['amount'], 2); ?>
                        </span>
                    </div>
                    <div class="flex justify-between items-center">
                        <span class="text-slate-400 text-xs">Start Date:</span>
                        <span class="text-slate-300 font-medium text-xs">
                            <?php echo date('M d, Y', strtotime($activeSub['start_date'])); ?>
                        </span>
                    </div>
                    <div class="flex justify-between items-center">
                        <span class="text-slate-400 text-xs">Expires On:</span>
                        <span class="text-slate-300 font-medium text-xs">
                            <?php echo date('M d, Y', $expiry); ?>
                        </span>
                    </div>
                    <div class="flex justify-between items-center border-t border-white/5 pt-2">
                        <span class="text-slate-400 text-xs">Time Remaining:</span>
                        <span class="font-extrabold text-xs <?php echo $expired ? 'text-red-400' : ($warning ? 'text-yellow-400' : 'text-emerald-400'); ?>">
                            <?php echo $expired ? 'Expired' : $daysLeft . ' Days Left'; ?>
                        </span>
                    </div>
                </div>
            <?php else: ?>
                <div class="py-6 text-center">
                    <div class="w-12 h-12 rounded-full bg-red-500/10 flex items-center justify-center mx-auto text-red-400 mb-3">
                        <i class="fas fa-times-circle text-lg"></i>
                    </div>
                    <p class="text-xs text-slate-400 font-medium">No active subscription found</p>
                    <p class="text-4xs text-slate-500 mt-1">This restaurant does not currently have an active plan.</p>
                </div>
            <?php endif; ?>
        </div>

        <!-- Billing logs -->
        <div class="lg:col-span-2 stat-card overflow-hidden flex flex-col">
            <div class="px-5 py-4 border-b border-white/5 flex justify-between items-center bg-white/[0.01]">
                <h3 class="font-bold text-white text-sm flex items-center gap-2">
                    <i class="fas fa-history text-purple-400"></i> Subscription Logs
                </h3>
                <span class="text-3xs text-slate-500 uppercase tracking-widest font-semibold"><?php echo count($billingHistory); ?> Records Found</span>
            </div>
            
            <div class="overflow-x-auto flex-1">
                <table class="w-full text-xs">
                    <thead>
                        <tr class="border-b border-white/5 bg-white/[0.01] text-left">
                            <th class="px-5 py-3 text-slate-400 font-bold uppercase tracking-wider">Package</th>
                            <th class="px-5 py-3 text-slate-400 font-bold uppercase tracking-wider">Period</th>
                            <th class="px-5 py-3 text-slate-400 font-bold uppercase tracking-wider">Amount</th>
                            <th class="px-5 py-3 text-slate-400 font-bold uppercase tracking-wider">Payment Details</th>
                            <th class="px-5 py-3 text-slate-400 font-bold uppercase tracking-wider">Status</th>
                            <th class="px-5 py-3 text-slate-400 font-bold uppercase tracking-wider">Created At</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-white/5">
                        <?php if (empty($billingHistory)): ?>
                            <tr>
                                <td colspan="6" class="px-5 py-12 text-center text-slate-500 font-medium">
                                    No subscription transactions match your filters.
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php foreach ($billingHistory as $row): 
                            $payDetails = json_decode($row['payment_details'] ?? '{}', true) ?: [];
                            $payMethod = !empty($row['payment_method']) ? ucfirst($row['payment_method']) : 'N/A';
                            
                            $statusBg = 'rgba(255,255,255,0.06)';
                            $statusColor = '#94a3b8';
                            if ($row['status'] === 'active') {
                                $statusBg = 'rgba(16,185,129,0.15)';
                                $statusColor = '#6ee7b7';
                            } elseif ($row['status'] === 'expired') {
                                $statusBg = 'rgba(239,68,68,0.15)';
                                $statusColor = '#fca5a5';
                            } elseif ($row['status'] === 'pending') {
                                $statusBg = 'rgba(245,158,11,0.15)';
                                $statusColor = '#fcd34d';
                            }
                        ?>
                            <tr class="hover:bg-white/[0.015] transition">
                                <td class="px-5 py-3.5 font-semibold text-white">
                                    <?php echo htmlspecialchars($row['package_name']); ?>
                                </td>
                                <td class="px-5 py-3.5 text-slate-300">
                                    <div class="font-medium"><?php echo date('M d, Y', strtotime($row['start_date'])); ?></div>
                                    <div class="text-3xs text-slate-500">to <?php echo date('M d, Y', strtotime($row['end_date'])); ?></div>
                                </td>
                                <td class="px-5 py-3.5 font-bold text-white">
                                    <div><?php echo $currency . number_format($row['amount'], 2); ?></div>
                                    <?php if (!empty($row['coupon_code'])): ?>
                                        <div class="text-emerald-400 text-4xs font-bold flex items-center gap-0.5 mt-0.5">
                                            <i class="fas fa-tag"></i> <?php echo htmlspecialchars($row['coupon_code']); ?>
                                        </div>
                                    <?php endif; ?>
                                </td>
                                <td class="px-5 py-3.5 text-slate-300">
                                    <div class="flex items-center gap-1.5">
                                        <span class="px-1.5 py-0.5 rounded text-4xs font-extrabold uppercase bg-white/5 border border-white/10 text-slate-300">
                                            <?php echo htmlspecialchars($payMethod); ?>
                                        </span>
                                        <span class="text-4xs uppercase tracking-wider font-extrabold <?php echo $row['payment_status'] === 'paid' ? 'text-emerald-400' : 'text-yellow-400'; ?>">
                                            <?php echo htmlspecialchars($row['payment_status']); ?>
                                        </span>
                                    </div>
                                    <?php if (!empty($payDetails['transaction_ref'])): ?>
                                        <div class="text-4xs text-slate-500 font-mono mt-1">Ref: <?php echo htmlspecialchars($payDetails['transaction_ref']); ?></div>
                                    <?php endif; ?>
                                    <?php if (!empty($payDetails['sender_number'])): ?>
                                        <div class="text-4xs text-slate-500 font-mono">From: <?php echo htmlspecialchars($payDetails['sender_number']); ?></div>
                                    <?php endif; ?>
                                </td>
                                <td class="px-5 py-3.5">
                                    <span class="px-2 py-0.5 rounded-full text-4xs font-extrabold uppercase border" style="background:<?php echo $statusBg; ?>; color:<?php echo $statusColor; ?>; border-color: currentColor;">
                                        <?php echo htmlspecialchars($row['status']); ?>
                                    </span>
                                </td>
                                <td class="px-5 py-3.5 text-slate-400">
                                    <?php echo date('M d, Y H:i', strtotime($row['created_at'])); ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
