<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0"><?= lang('finance.invoice') ?> #<?= e($invoice['invoice_number']) ?></h4>
    <div class="d-flex gap-2">
        <?php if ($invoice['status'] === 'draft' && auth_can('invoices.create')): ?>
        <button class="btn btn-success btn-issue-invoice" data-id="<?= $invoice['id'] ?>"><i class="fas fa-check me-1"></i><?= lang('finance.issue') ?></button>
        <?php endif; ?>
        <?php if (in_array($invoice['status'], ['draft','issued']) && auth_can('invoices.void')): ?>
        <button class="btn btn-danger btn-void-invoice" data-id="<?= $invoice['id'] ?>"><i class="fas fa-ban me-1"></i><?= lang('finance.void') ?></button>
        <?php endif; ?>
        <?php if (in_array($invoice['status'], ['issued','partially_paid','overdue']) && auth_can('payments.create')): ?>
        <a href="/finance/payments/create?invoice_id=<?= $invoice['id'] ?>&member_id=<?= $invoice['member_id'] ?>" class="btn btn-primary"><i class="fas fa-money-bill me-1"></i><?= lang('finance.record_payment') ?></a>
        <?php endif; ?>
        <button class="btn btn-outline-secondary" onclick="window.print()"><i class="fas fa-print me-1"></i><?= lang('common.print') ?></button>
        <a href="/finance/invoices" class="btn btn-outline-secondary"><?= lang('common.back') ?></a>
    </div>
</div>

<div class="card mb-4" id="invoice-print-area">
    <div class="card-body p-4">
        <!-- Header -->
        <div class="row mb-4">
            <div class="col-6">
                <h4><?= e($clubName) ?></h4>
                <p class="text-muted mb-0"><?= e($clubAddress) ?></p>
                <p class="text-muted mb-0"><?= e($clubPhone) ?></p>
            </div>
            <div class="col-6 text-end">
                <h2 class="text-primary mb-1"><?= lang('finance.invoice') ?></h2>
                <p class="mb-0"><strong>#<?= e($invoice['invoice_number']) ?></strong></p>
                <p class="mb-0"><?= lang('finance.date') ?>: <?= format_date($invoice['invoice_date']) ?></p>
                <p class="mb-0"><?= lang('finance.due_date') ?>: <?= format_date($invoice['due_date']) ?></p>
                <p class="mt-1"><?= status_badge($invoice['status']) ?></p>
            </div>
        </div>

        <!-- Member Info -->
        <div class="card bg-light mb-4">
            <div class="card-body py-2">
                <div class="row">
                    <div class="col-md-6">
                        <strong><?= lang('finance.bill_to') ?>:</strong><br>
                        <?= e($invoice['member_name']) ?><br>
                        <small class="text-muted"><?= e($invoice['membership_number']) ?> | <?= e($invoice['membership_type'] ?? '') ?></small><br>
                        <?php if ($invoice['primary_phone']): ?><small><?= e($invoice['primary_phone']) ?></small><br><?php endif; ?>
                        <?php if ($invoice['primary_email']): ?><small><?= e($invoice['primary_email']) ?></small><?php endif; ?>
                    </div>
                    <div class="col-md-6 text-end">
                        <strong><?= lang('finance.type') ?>:</strong> <?= e(ucfirst(str_replace('_',' ',$invoice['invoice_type']))) ?><br>
                        <strong><?= lang('finance.currency') ?>:</strong> <?= e($invoice['currency_code'] ?? 'EGP') ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Line Items -->
        <table class="table table-bordered mb-4">
            <thead class="table-light">
                <tr>
                    <th>#</th>
                    <th><?= lang('finance.description') ?></th>
                    <th class="text-center"><?= lang('finance.qty') ?></th>
                    <th class="text-end"><?= lang('finance.unit_price') ?></th>
                    <th class="text-end"><?= lang('finance.discount') ?></th>
                    <th class="text-end"><?= lang('finance.tax') ?></th>
                    <th class="text-end"><?= lang('finance.total') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $i => $item): ?>
                <tr>
                    <td><?= $i + 1 ?></td>
                    <td><?= e($item['description_en']) ?><?php if ($item['fee_type_name']): ?> <small class="text-muted">(<?= e($item['fee_code']) ?>)</small><?php endif; ?></td>
                    <td class="text-center"><?= $item['quantity'] ?></td>
                    <td class="text-end"><?= format_number($item['unit_price']) ?></td>
                    <td class="text-end"><?= format_number($item['discount_amount']) ?></td>
                    <td class="text-end"><?= format_number($item['tax_amount']) ?></td>
                    <td class="text-end"><strong><?= format_number($item['total_amount']) ?></strong></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="6" class="text-end"><?= lang('finance.subtotal') ?>:</td>
                    <td class="text-end"><?= format_number($invoice['subtotal']) ?></td>
                </tr>
                <?php if ((float)$invoice['discount_total'] > 0): ?>
                <tr>
                    <td colspan="6" class="text-end text-success"><?= lang('finance.discount') ?>:</td>
                    <td class="text-end text-success">-<?= format_number($invoice['discount_total']) ?></td>
                </tr>
                <?php endif; ?>
                <?php if ((float)$invoice['tax_total'] > 0): ?>
                <tr>
                    <td colspan="6" class="text-end"><?= lang('finance.tax') ?>:</td>
                    <td class="text-end"><?= format_number($invoice['tax_total']) ?></td>
                </tr>
                <?php endif; ?>
                <tr class="table-primary">
                    <td colspan="6" class="text-end"><strong><?= lang('finance.total_amount') ?>:</strong></td>
                    <td class="text-end"><strong><?= money($invoice['total_amount']) ?></strong></td>
                </tr>
                <tr>
                    <td colspan="6" class="text-end"><?= lang('finance.paid') ?>:</td>
                    <td class="text-end text-success"><?= money($invoice['paid_amount']) ?></td>
                </tr>
                <tr class="table-warning">
                    <td colspan="6" class="text-end"><strong><?= lang('finance.balance_due') ?>:</strong></td>
                    <td class="text-end"><strong class="text-danger"><?= money($invoice['balance_due']) ?></strong></td>
                </tr>
            </tfoot>
        </table>

        <?php if ($invoice['notes']): ?>
        <div class="mb-3">
            <strong><?= lang('finance.notes') ?>:</strong>
            <p class="text-muted mb-0"><?= nl2br(e($invoice['notes'])) ?></p>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Payment History -->
<?php if (!empty($payments)): ?>
<div class="card">
    <div class="card-header"><h6 class="mb-0"><?= lang('finance.payment_history') ?></h6></div>
    <div class="card-body">
        <table class="table table-sm">
            <thead><tr><th><?= lang('finance.payment_no') ?></th><th><?= lang('finance.date') ?></th><th><?= lang('finance.method') ?></th><th class="text-end"><?= lang('finance.amount') ?></th><th><?= lang('common.status') ?></th></tr></thead>
            <tbody>
            <?php foreach ($payments as $p): ?>
            <tr>
                <td><?= e($p['payment_number']) ?></td>
                <td><?= format_date($p['payment_date']) ?></td>
                <td><?= e($p['method_name'] ?? '') ?></td>
                <td class="text-end"><?= money($p['allocated_amount']) ?></td>
                <td><?= status_badge($p['payment_status']) ?></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php $__view->push('scripts'); ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.btn-issue-invoice').forEach(function(btn) {
        btn.addEventListener('click', function() {
            var id = this.dataset.id;
            Swal.fire({
                title: '<?= lang('finance.confirm_issue') ?>',
                text: '<?= lang('finance.confirm_issue_text') ?>',
                icon: 'question',
                showCancelButton: true,
                confirmButtonText: '<?= lang('finance.issue') ?>',
                cancelButtonText: '<?= lang('common.cancel') ?>'
            }).then(function(result) {
                if (result.isConfirmed) {
                    $.ajax({
                        url: '/finance/invoices/' + id + '/issue',
                        type: 'POST',
                        data: { _token: '<?= csrf_token() ?>' },
                        success: function(r) {
                            if (r.success) {
                                FormHelper.showToast('success', r.message);
                                setTimeout(function() { location.reload(); }, 800);
                            } else {
                                FormHelper.showToast('error', r.message);
                            }
                        }
                    });
                }
            });
        });
    });

    document.querySelectorAll('.btn-void-invoice').forEach(function(btn) {
        btn.addEventListener('click', function() {
            var id = this.dataset.id;
            Swal.fire({
                title: '<?= lang('finance.void_invoice') ?>',
                input: 'textarea',
                inputPlaceholder: '<?= lang('finance.void_reason') ?>',
                inputValidator: function(v) { if (!v) return '<?= lang('finance.reason_required') ?>'; },
                showCancelButton: true,
                confirmButtonText: '<?= lang('finance.void') ?>',
                confirmButtonColor: '#dc3545',
                cancelButtonText: '<?= lang('common.cancel') ?>'
            }).then(function(result) {
                if (result.isConfirmed) {
                    $.ajax({
                        url: '/finance/invoices/' + id + '/void',
                        type: 'POST',
                        data: { _token: '<?= csrf_token() ?>', reason: result.value },
                        success: function(r) {
                            if (r.success) {
                                FormHelper.showToast('success', r.message);
                                setTimeout(function() { location.reload(); }, 800);
                            } else {
                                FormHelper.showToast('error', r.message);
                            }
                        }
                    });
                }
            });
        });
    });
});
</script>
<?php $__view->endPush(); ?>