<?php
require_once '../init.php';

$subsStats = $subscription->getSubscriptionStats($_SESSION['user_id']);

$pageTitle = 'Status Langganan - KasirMurah.biz.id';
?>
<?php include '../layout/header.php'; ?>
    <?php include '../layout/navbar.php'; ?>
    
    <div class="container mt-4">
        <h1>Status Langganan</h1>
        
        <div class="row mt-4">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5>Informasi Langganan</h5>
                    </div>
                    <div class="card-body">
                        <?php if ($_SESSION['role'] === 'admin'): ?>
                        <div class="row mb-3">
                            <div class="col-4">Status</div>
                            <div class="col-8"><span class="badge bg-success">Aktif</span></div>
                        </div>
                        <div class="row mb-3">
                            <div class="col-4">Paket</div>
                            <div class="col-8">Administrator</div>
                        </div>
                        <div class="row mb-3">
                            <div class="col-4">Tanggal Berakhir</div>
                            <div class="col-8">Selamanya</div>
                        </div>
                        <div class="row mb-3">
                            <div class="col-4">Sisa Hari</div>
                            <div class="col-8"><span class="badge bg-info">Tidak Terbatas</span></div>
                        </div>
                        <div class="row">
                            <div class="col-12">
                                <button class="btn btn-success" disabled>Akun Administrator</button>
                            </div>
                        </div>
                        <?php elseif (!$subsStats['subscription']): ?>
                        <div class="alert alert-warning">Tidak ada data langganan ditemukan untuk akun ini.</div>
                        <a href="upgrade.php" class="btn btn-primary">Mulai Berlangganan</a>
                        <?php else: $sub = $subsStats['subscription']; ?>
                        <div class="row mb-3">
                            <div class="col-4">Status</div>
                            <div class="col-8">
                                <?php echo Helper::getStatusBadge($sub['status']); ?>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-4">Paket</div>
                            <div class="col-8">
                                <?php echo ucfirst($sub['plan_type'] ?? 'monthly'); ?>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-4">Tanggal Mulai</div>
                            <div class="col-8">
                                <?php echo Helper::formatDate($sub['start_date']); ?>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-4">Tanggal Berakhir</div>
                            <div class="col-8">
                                <?php
                                if ($sub['status'] === 'trial') {
                                    echo Helper::formatDateTime($sub['trial_ends_at']);
                                } else {
                                    echo Helper::formatDate($sub['end_date']);
                                }
                                ?>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-4">Sisa Hari</div>
                            <div class="col-8">
                                <span class="badge bg-info"><?php echo $subsStats['days_remaining']; ?> hari</span>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-12">
                                <?php if (!empty($sub['is_trial']) && $sub['is_trial'] == 1): ?>
                                <a href="upgrade.php" class="btn btn-primary">Upgrade Paket</a>
                                <?php else: ?>
                                <button class="btn btn-warning" disabled>Paket Aktif</button>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5>Riwayat Pembayaran</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Invoice</th>
                                        <th>Status</th>
                                        <th>Jumlah</th>
                                        <th>Tanggal</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                    $payments = $subscription->getUserPayments($_SESSION['user_id'], 5);
                                    if (!empty($payments)):
                                        foreach ($payments as $payment):
                                    ?>
                                    <tr>
                                        <td><?php echo $payment['invoice_number']; ?></td>
                                        <td><?php echo Helper::getStatusBadge($payment['status']); ?></td>
                                        <td><?php echo Helper::formatCurrency($payment['amount']); ?></td>
                                        <td><?php echo Helper::formatDate($payment['created_at']); ?></td>
                                    </tr>
                                    <?php 
                                        endforeach;
                                    else:
                                    ?>
                                    <tr>
                                        <td colspan="4" class="text-center text-muted">Belum ada pembayaran</td>
                                    </tr>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                        
                        <a href="payments.php" class="btn btn-sm btn-info">Lihat Semua</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <?php include '../layout/footer.php'; ?>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
