<div class="row g-4">
    <div class="col-lg-4">
        <div class="card">
            <div class="card-header"><h6 class="mb-0"><?= lang('adv_finance.plan_number') ?>: <?= e($plan['plan_number']) ?></h6></div>
            <div class="card-body">
                <table class="table table-sm mb-0">
                    <tr><th><?= lang('adv_finance.member') ?></th><td><?= e($plan['member_name']) ?> <small class="text-muted">(<?= e($plan['membership_number']) ?>)</small></td></tr>
                    <tr><th><?= lang('common.status') ?></th><td><?= status_badge($plan['status']) ?></td></tr>
                    <tr><th><?= lang('adv_finance.total_amount') ?></th><td><?= format_money($plan['total_amount']) ?></td></tr>
                    <tr><th><?= lang('adv_finance.down_payment') ?></th><td><?= format_money($plan['down_payment']) ?></td></tr>
                    <tr><th><?= lang('adv_finance.financed') ?></th><td><?= format_money($plan['financed_amount']) ?></td></tr>
                    <tr><th><?= lang('adv_finance.total_paid') ?></th><td class="text-success fw-bold"><?= format_money($plan['total_paid']) ?></td></tr>
                    <tr><th><?= lang('adv_finance.remaining') ?></th><td class="text-danger fw-bold"><?= format_money($plan['total_remaining']) ?></td></tr>
                    <tr><th><?= lang('adv_finance.num_installments') ?></th><td><?= $plan['installments_paid'] ?>/<?= $plan['number_of_installments'] ?></td></tr>
                    <tr><th><?= lang('adv_finance.frequency') ?></th><td><?= ucfirst(str_replace('_', ' ', $plan['frequency'])) ?></td></tr>
                    <tr><th><?= lang('adv_finance.start_date') ?></th><td><?= format_date($plan['start_date']) ?></td></tr>
                    <tr><th><?= lang('adv_finance.next_due') ?></th><td><?= format_date($plan['next_due_date']) ?></td></tr>
                    <?php if ($plan['invoice_number']): ?>
                    <tr><th><?= lang('adv_finance.invoice') ?></th><td><?= e($plan['invoice_number']) ?></td></tr>
                    <?php endif; ?>
                </table>
                <?php if ($plan['status'] === 'active' && auth_can('installments.create')): ?>
                <div class="mt-3">
                    <form method="POST" action="/advanced-finance/installments/<?= $plan['id'] ?>/default" data-ajax="true"
                          onsubmit="return confirm('<?= lang('common.are_you_sure') ?>')">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-danger btn-sm"><i class="fas fa-exclamation-triangle me-1"></i><?= lang('adv_finance.mark_defaulted') ?></button>
                    </form>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-lg-8">
        <div class="card mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h6 class="mb-0"><?= lang('adv_finance.schedule') ?></h6>
                <?php if ($plan['status'] === 'active' && auth_can('installments.create')): ?>
                <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#payModal"><i class="fas fa-plus me-1"></i><?= lang('adv_finance.record_payment') ?></button>
                <?php endif; ?>
            </div>
            <div class="card-body p-0">
                <table class="table table-sm table-striped mb-0">
                    <thead><tr>
                        <th><?= lang('adv_finance.installment_no') ?></th>
                        <th><?= lang('adv_finance.due_date') ?></th>
                        <th><?= lang('adv_finance.amount') ?></th>
                        <th><?= lang('adv_finance.paid') ?></th>
                        <th><?= lang('adv_finance.balance') ?></th>
                        <th><?= lang('common.status') ?></th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ($schedules as $s): ?>
                    <tr class="<?= $s['status'] === 'overdue' ? 'table-danger' : '' ?>">
                        <td><?= $s['installment_number'] ?></td>
                        <td><?= format_date($s['due_date']) ?></td>
                        <td><?= format_money($s['amount']) ?></td>
                        <td><?= format_money($s['paid_amount']) ?></td>
                        <td><?= format_money($s['balance']) ?></td>
                        <td><?= status_badge($s['status']) ?></td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <?php if (!empty($payments)): ?>
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Payment History</h6></div>
            <div class="card-body p-0">
                <table class="table table-sm mb-0">
                    <thead><tr><th>Inst. #</th><th>Payment #</th><th>Date</th><th>Amount</th><th>Late Fee</th><th>Method</th></tr></thead>
                    <tbody>
                    <?php foreach ($payments as $p): ?>
                    <tr>
                        <td><?= $p['installment_number'] ?></td>
                        <td><?= e($p['payment_number']) ?></td>
                        <td><?= format_date($p['payment_date']) ?></td>
                        <td><?= format_money($p['amount']) ?></td>
                        <td><?= format_money($p['late_fee_amount']) ?></td>
                        <td><?= e($p['method_name'] ?? '—') ?></td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php if ($plan['status'] === 'active'): ?>
<div class="modal fade" id="payModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="POST" action="/advanced-finance/installments/<?= $plan['id'] ?>/pay" data-ajax="true">
                <?= csrf_field() ?>
                <div class="modal-header"><h5 class="modal-title"><?= lang('adv_finance.record_payment') ?></h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Installment <span class="text-danger">*</span></label>
                        <select name="schedule_id" class="form-select" required>
                            <option value="">Select...</option>
                            <?php foreach ($schedules as $s): ?>
                            <?php if (in_array($s['status'], ['pending','partially_paid','overdue'])): ?>
                            <option value="<?= $s['id'] ?>">#<?= $s['installment_number'] ?> — Due: <?= format_date($s['due_date']) ?> — Balance: <?= format_money($s['balance']) ?></option>
                            <?php endif; ?>
                            <?php endforeach; ?>
                        </select>
                        <div class="invalid-feedback" data-field="schedule_id"></div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label"><?= lang('adv_finance.payment_amount') ?> <span class="text-danger">*</span></label>
                        <input type="number" name="amount" class="form-control" step="0.01" required>
                        <div class="invalid-feedback" data-field="amount"></div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label"><?= lang('adv_finance.late_fee') ?></label>
                        <input type="number" name="late_fee_amount" class="form-control" step="0.01" value="0">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Payment Method <span class="text-danger">*</span></label>
                        <select name="payment_method_id" class="form-select" required>
                            <?php foreach ($paymentMethods as $pm): ?>
                            <option value="<?= $pm['id'] ?>"><?= e($pm['name_en']) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <div class="invalid-feedback" data-field="payment_method_id"></div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Payment Date <span class="text-danger">*</span></label>
                        <input type="date" name="payment_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?= lang('common.cancel') ?></button>
                    <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i><?= lang('common.save') ?></button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php endif; ?>