<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0"><i class="fas fa-file-invoice me-2"></i><?= lang('subs.subscription') ?> #<?= $sub['id'] ?></h4>
    <a href="/subscriptions" class="btn btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i><?= lang('common.back') ?></a>
</div>

<div class="row g-4">
    <!-- Main Info -->
    <div class="col-md-8">
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0">Subscription Details</h6></div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-6"><strong>Member:</strong><br><a href="/members/<?= $sub['member_id'] ?>"><?= e($sub['membership_number'] . ' - ' . $sub['member_name_en']) ?></a></div>
                    <div class="col-md-6"><strong>Type:</strong><br><?= e($sub['type_name_en']) ?></div>
                    <div class="col-md-6"><strong>Period:</strong><br><?= e($sub['period_name_en']) ?> (<?= e($sub['period_code']) ?>)</div>
                    <div class="col-md-6"><strong>Status:</strong><br><?= status_badge($sub['status']) ?></div>
                    <div class="col-md-3"><strong>Start:</strong><br><?= format_date($sub['start_date']) ?></div>
                    <div class="col-md-3"><strong>End:</strong><br><?= format_date($sub['end_date']) ?></div>
                    <div class="col-md-3"><strong>Renewal?</strong><br><?= $sub['is_renewal'] ? '<span class="badge bg-info">Yes</span>' : 'No' ?></div>
                    <div class="col-md-3"><strong>Dependents:</strong><br><?= $sub['dependents_count'] ?></div>
                </div>
                <?php if ($sub['notes']): ?><hr><p class="text-muted mb-0"><i class="fas fa-sticky-note me-1"></i><?= e($sub['notes']) ?></p><?php endif; ?>
            </div>
        </div>

        <!-- Freezes -->
        <?php if (!empty($freezes)): ?>
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0"><i class="fas fa-snowflake me-2"></i>Related Freezes</h6></div>
            <div class="card-body p-0">
                <table class="table table-sm mb-0">
                    <thead><tr><th>Freeze #</th><th>Start</th><th>End</th><th>Days</th><th>Status</th></tr></thead>
                    <tbody>
                    <?php foreach ($freezes as $f): ?>
                    <tr>
                        <td><a href="/freezes/<?= $f['id'] ?>"><?= e($f['freeze_number']) ?></a></td>
                        <td><?= format_date($f['start_date']) ?></td>
                        <td><?= format_date($f['actual_end_date'] ?? $f['original_end_date']) ?></td>
                        <td><?= $f['duration_days'] ?></td>
                        <td><?= status_badge($f['status']) ?></td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>

        <!-- Renewal Log -->
        <?php if (!empty($renewalLog)): ?>
        <div class="card">
            <div class="card-header"><h6 class="mb-0"><i class="fas fa-history me-2"></i>Renewal History</h6></div>
            <div class="card-body p-0">
                <table class="table table-sm mb-0">
                    <thead><tr><th>Date</th><th>Type</th><th>Late Days</th><th>Late Fee</th><th>By</th></tr></thead>
                    <tbody>
                    <?php foreach ($renewalLog as $rl): ?>
                    <tr>
                        <td><?= format_date($rl['renewal_date']) ?></td>
                        <td><?= status_badge($rl['renewal_type']) ?></td>
                        <td><?= $rl['late_days'] ?></td>
                        <td><?= format_money($rl['late_fee']) ?></td>
                        <td><?= e($rl['created_by_name'] ?? '-') ?></td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <!-- Sidebar -->
    <div class="col-md-4">
        <!-- Financial -->
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0">Financial</h6></div>
            <div class="card-body">
                <table class="table table-sm table-borderless mb-0">
                    <tr><td>Base Amount</td><td class="text-end"><?= format_money($sub['amount']) ?></td></tr>
                    <tr><td>Discount</td><td class="text-end text-success">-<?= format_money($sub['discount_amount']) ?></td></tr>
                    <tr><td>Dependents</td><td class="text-end"><?= format_money($sub['dependents_amount']) ?></td></tr>
                    <tr><td>Late Fee</td><td class="text-end text-danger"><?= format_money($sub['late_fee']) ?></td></tr>
                    <tr class="fw-bold border-top"><td>Net Amount</td><td class="text-end"><?= format_money($sub['net_amount']) ?></td></tr>
                    <tr><td>Paid</td><td class="text-end text-success"><?= format_money($sub['paid_amount']) ?></td></tr>
                    <tr class="fw-bold border-top"><td>Balance Due</td><td class="text-end <?= $sub['balance_due'] > 0 ? 'text-danger' : 'text-success' ?>"><?= format_money($sub['balance_due']) ?></td></tr>
                </table>
            </div>
        </div>
        <?php if ($invoice): ?>
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0">Invoice</h6></div>
            <div class="card-body">
                <a href="/finance/invoices/<?= $invoice['id'] ?>" class="fw-bold"><?= e($invoice['invoice_number']) ?></a>
                <br><?= status_badge($invoice['status']) ?>
                <br>Total: <?= format_money($invoice['total_amount']) ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>