<div class="row g-4">
    <div class="col-lg-5">
        <div class="card">
            <div class="card-header"><h6 class="mb-0"><?= e($cn['credit_note_number']) ?></h6></div>
            <div class="card-body">
                <table class="table table-sm mb-0">
                    <tr><th>Member</th><td><?= e($cn['member_name']) ?> (<?= e($cn['membership_number']) ?>)</td></tr>
                    <tr><th>Date</th><td><?= format_date($cn['credit_date']) ?></td></tr>
                    <tr><th>Total</th><td class="fw-bold"><?= format_money($cn['total_amount']) ?></td></tr>
                    <tr><th>Applied</th><td><?= format_money($cn['applied_amount']) ?></td></tr>
                    <tr><th>Remaining</th><td class="text-success fw-bold"><?= format_money($cn['remaining_amount']) ?></td></tr>
                    <tr><th>Status</th><td><?= status_badge($cn['status']) ?></td></tr>
                    <tr><th>Reason</th><td><?= e($cn['reason']) ?></td></tr>
                    <?php if($cn['original_invoice_number']): ?><tr><th>Original Invoice</th><td><?= e($cn['original_invoice_number']) ?></td></tr><?php endif; ?>
                </table>
                <?php if($cn['status'] === 'draft' && auth_can('installments.create')): ?>
                <div class="mt-3">
                    <form method="POST" action="/advanced-finance/credit-notes/<?= $cn['id'] ?>/issue" data-ajax="true">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-success btn-sm"><i class="fas fa-check me-1"></i>Issue</button>
                    </form>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-lg-7">
        <?php if(in_array($cn['status'], ['issued','partially_applied']) && !empty($outstandingInvoices)): ?>
        <div class="card mb-4">
            <div class="card-header"><h6 class="mb-0"><?= lang('adv_finance.apply_to_invoice') ?></h6></div>
            <div class="card-body">
                <form method="POST" action="/advanced-finance/credit-notes/<?= $cn['id'] ?>/apply" data-ajax="true">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">Invoice</label>
                            <select name="invoice_id" class="form-select" required>
                                <option value="">Select...</option>
                                <?php foreach($outstandingInvoices as $inv): ?>
                                <option value="<?= $inv['id'] ?>"><?= e($inv['invoice_number']) ?> — Due: <?= format_money($inv['balance_due']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Amount to Apply</label>
                            <input type="number" name="apply_amount" class="form-control" step="0.01" max="<?= $cn['remaining_amount'] ?>" required>
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                            <button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-arrow-right me-1"></i>Apply</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <?php endif; ?>
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Items</h6></div>
            <div class="card-body p-0">
                <table class="table table-sm mb-0">
                    <thead><tr><th>Description</th><th>Amount</th></tr></thead>
                    <tbody>
                    <?php foreach($items as $item): ?>
                    <tr><td><?= e($item['description_en']) ?></td><td><?= format_money($item['amount']) ?></td></tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>