

<?php $__env->startSection('title', $tenant->isOnTrial() ? 'Pay Subscription' : 'Billing'); ?>
<?php $__env->startSection('page-title', $tenant->isOnTrial() ? 'Pay Subscription' : 'Subscription Billing'); ?>
<?php $__env->startSection('page-subtitle', $tenant->isOnTrial() ? 'Activate your subscription before the free trial ends' : 'Plan renewal, payment proof, automatic billing, invoices, and expiration'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $canManageBilling = auth()->user()->hasPermission('billing.manage');
    $tenantPortalUrl = route('public.tenant.show', ['tenant' => $tenant->slug]);
    $currency = $tenant->settings['currency'] ?? 'NGN';
    $money = fn ($amount) => $currency.' '.number_format((float) $amount, 2);
    $currentPlanPrice = $tenant->plan ? ($tenant->billing_cycle === 'yearly' ? $tenant->plan->price_yearly : $tenant->plan->price_monthly) : 0;
    $isTrialTenant = $tenant->isOnTrial();
    $selectedPlan = $tenant->plan ?? $plans->first();
?>

<section class="section-hero mb-4">
    <div class="section-hero-copy">
        <span>Account center</span>
        <h2><?php echo e($isTrialTenant ? 'Pay and activate your subscription' : 'Subscription billing'); ?></h2>
        <p><?php echo e($isTrialTenant ? 'Your free trial is active. Choose online payment or switch to manual payment to activate your subscription.' : 'Manage plan renewal, online payments, manual approval, automatic deduction, invoices, and account expiration from one workspace.'); ?></p>
    </div>
    <div class="section-hero-panel">
        <span>Current plan</span>
        <strong><?php echo e($tenant->plan?->name ?? 'No plan'); ?></strong>
        <small><?php echo e(app_date($tenant->subscription_ends_at, 'No expiration date')); ?></small>
    </div>
</section>

<div class="section-stat-grid mb-4">
    <div class="section-stat-card">
        <i class="bi bi-box-seam"></i>
        <span>Plan</span>
        <strong><?php echo e($tenant->plan?->name ?? 'None'); ?></strong>
        <small><?php echo e($currentPlanPrice ? $money($currentPlanPrice) : 'No active price'); ?></small>
    </div>
    <div class="section-stat-card">
        <i class="bi bi-calendar2-check"></i>
        <span>Subscription</span>
        <strong><?php echo e($tenant->isOnTrial() ? 'Trial' : ($tenant->isActive() ? 'Active' : 'Expired')); ?></strong>
        <small><?php echo e($tenant->isOnTrial() ? $tenant->trialDaysRemaining().' days left' : 'Billing status'); ?></small>
    </div>
    <div class="section-stat-card">
        <i class="bi bi-arrow-repeat"></i>
        <span>Auto renewal</span>
        <strong><?php echo e($tenant->autoBillingLabel()); ?></strong>
        <small><?php echo e(app_date($tenant->auto_billing_next_charge_at, 'No next deduction')); ?></small>
    </div>
    <div class="section-stat-card">
        <i class="bi bi-receipt"></i>
        <span>Invoices</span>
        <strong><?php echo e(number_format($invoices->count())); ?></strong>
        <small>Recent invoices</small>
    </div>
</div>

<div class="section-workspace mb-4" id="payment-options">
    <div class="section-card-title">
        <span><i class="bi bi-cash-coin"></i></span>
        <div>
            <strong><?php echo e($isTrialTenant ? 'Pay now' : 'Make payment'); ?></strong>
            <small>Online payment is selected by default. Switch to manual if you want super admin approval.</small>
        </div>
    </div>
    <div class="section-tabbar px-3 px-lg-4 pt-3">
        <ul class="nav section-tabs" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" data-bs-toggle="tab" data-bs-target="#online-payment-pane" type="button" role="tab">
                    <i class="bi bi-lightning-charge me-1"></i> Online
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" data-bs-toggle="tab" data-bs-target="#manual-payment-pane" type="button" role="tab">
                    <i class="bi bi-upload me-1"></i> Manual
                </button>
            </li>
        </ul>
    </div>
    <div class="tab-content">
        <div class="tab-pane fade show active" id="online-payment-pane" role="tabpanel" tabindex="0">
            <div class="p-3 p-lg-4">
                <?php if($canManageBilling): ?>
                    <form method="POST" action="<?php echo e(route('tenant.billing.gateway.start')); ?>" class="row g-3" data-subscription-payment-form>
                        <?php echo csrf_field(); ?>
                        <div class="col-12">
                            <div class="alert alert-success border-0 small mb-1">
                                <strong>Online payment:</strong> Pay and get your subscription activated automatically. Automatic renewal is turned on after a successful card authorization.
                            </div>
                        </div>
                        <div class="col-md-5">
                            <label class="form-label">Plan</label>
                            <select name="subscription_plan_id" class="form-select" required data-plan-select>
                                <?php $__currentLoopData = $plans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $plan): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option
                                        value="<?php echo e($plan->id); ?>"
                                        data-plan-name="<?php echo e($plan->name); ?>"
                                        data-monthly="<?php echo e($plan->price_monthly); ?>"
                                        data-yearly="<?php echo e($plan->price_yearly); ?>"
                                        data-monthly-label="<?php echo e($money($plan->price_monthly)); ?>"
                                        data-yearly-label="<?php echo e($money($plan->price_yearly)); ?>"
                                        <?php if($selectedPlan?->id === $plan->id): echo 'selected'; endif; ?>
                                    ><?php echo e($plan->name); ?> - <?php echo e($money($plan->price_monthly)); ?>/mo</option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Billing cycle</label>
                            <select name="billing_cycle" class="form-select" data-cycle-select>
                                <option value="monthly">Monthly</option>
                                <option value="yearly">Yearly</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <div class="section-mini-card h-100" data-plan-preview>
                                <span>Amount to pay</span>
                                <strong data-plan-preview-amount><?php echo e($selectedPlan ? $money($selectedPlan->price_monthly) : '-'); ?></strong>
                                <small data-plan-preview-cycle>Monthly subscription</small>
                            </div>
                        </div>
                        <div class="col-12 d-grid d-md-flex justify-content-md-end">
                            <button class="btn btn-success px-4" <?php if(! $gatewayEnabled): echo 'disabled'; endif; ?>>
                                <i class="bi bi-credit-card me-1"></i> Continue to online payment
                            </button>
                        </div>
                        <?php if(! $gatewayEnabled): ?>
                            <div class="col-12">
                                <div class="alert alert-warning mb-0 small">A super admin must enable a payment gateway from Platform Settings before online payments are available. Switch to manual payment for now.</div>
                            </div>
                        <?php endif; ?>
                    </form>
                <?php else: ?>
                    <div class="text-muted small">Only tenant admins, managers, and accountants can pay subscriptions.</div>
                <?php endif; ?>
            </div>
        </div>
        <div class="tab-pane fade" id="manual-payment-pane" role="tabpanel" tabindex="0">
            <div class="p-3 p-lg-4">
                <?php if($canManageBilling): ?>
                    <form method="POST" action="<?php echo e(route('tenant.billing.payments.store')); ?>" enctype="multipart/form-data" class="row g-3" data-subscription-payment-form>
                        <?php echo csrf_field(); ?>
                        <div class="col-12">
                            <div class="alert alert-info border-0 small mb-1">
                                <strong>Manual payment:</strong> Upload payment proof after transfer for approval.
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="section-mini-card">
                                <span>Bank account for manual payment</span>
                                <strong><?php echo nl2br(e($bankDetails)); ?></strong>
                            </div>
                        </div>
                        <div class="col-md-5">
                            <label class="form-label">Plan</label>
                            <select name="subscription_plan_id" class="form-select" required data-plan-select>
                                <?php $__currentLoopData = $plans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $plan): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option
                                        value="<?php echo e($plan->id); ?>"
                                        data-plan-name="<?php echo e($plan->name); ?>"
                                        data-monthly="<?php echo e($plan->price_monthly); ?>"
                                        data-yearly="<?php echo e($plan->price_yearly); ?>"
                                        data-monthly-label="<?php echo e($money($plan->price_monthly)); ?>"
                                        data-yearly-label="<?php echo e($money($plan->price_yearly)); ?>"
                                        <?php if($selectedPlan?->id === $plan->id): echo 'selected'; endif; ?>
                                    ><?php echo e($plan->name); ?> - <?php echo e($money($plan->price_monthly)); ?>/mo</option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Billing cycle</label>
                            <select name="billing_cycle" class="form-select" data-cycle-select>
                                <option value="monthly">Monthly</option>
                                <option value="yearly">Yearly</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <div class="section-mini-card h-100" data-plan-preview>
                                <span>Amount to submit</span>
                                <strong data-plan-preview-amount><?php echo e($selectedPlan ? $money($selectedPlan->price_monthly) : '-'); ?></strong>
                                <small data-plan-preview-cycle>Monthly subscription</small>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Payment proof</label>
                            <input name="proof" type="file" class="form-control" accept="image/*,.pdf" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Notes</label>
                            <textarea name="notes" class="form-control" rows="1" placeholder="Payment notes"></textarea>
                        </div>
                        <div class="col-12 d-grid d-md-flex justify-content-md-end">
                            <button class="btn btn-primary px-4">Submit manual payment</button>
                        </div>
                    </form>
                <?php else: ?>
                    <div class="text-muted small">Only tenant admins, managers, and accountants can submit payments.</div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<div class="row g-4">
    <div class="col-xl-4">
        <div class="section-data-card mb-4">
            <div class="section-card-title">
                <span><i class="bi bi-building"></i></span>
                <div>
                    <strong>Account summary</strong>
                    <small>Plan, portal link, and bank transfer instructions.</small>
                </div>
            </div>
            <div class="billing-summary">
                <div>
                    <span>Current plan</span>
                    <strong><?php echo e($tenant->plan?->name ?? 'No plan'); ?></strong>
                </div>
                <div>
                    <span>Subscription expires</span>
                    <strong><?php echo e(app_date($tenant->subscription_ends_at) ?? '-'); ?></strong>
                </div>
                <div>
                    <span>Member website</span>
                    <a href="<?php echo e($tenantPortalUrl); ?>" target="_blank"><?php echo e($tenantPortalUrl); ?></a>
                </div>
                <div>
                    <span>Bank details</span>
                    <p><?php echo nl2br(e($bankDetails)); ?></p>
                </div>
            </div>
        </div>

    </div>

    <div class="col-xl-8">
        <div class="section-workspace">
            <div class="section-tabbar">
                <ul class="nav section-tabs" role="tablist">
                    <li class="nav-item" role="presentation"><button class="nav-link active" data-bs-toggle="tab" data-bs-target="#auto-renewal-pane" type="button">Auto renewal</button></li>
                    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#payments-pane" type="button">Payments</button></li>
                    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#invoices-pane" type="button">Invoices</button></li>
                </ul>
            </div>
            <div class="tab-content">
                <div class="tab-pane fade show active" id="auto-renewal-pane" role="tabpanel" tabindex="0">
                    <div class="section-pane-head">
                        <div>
                            <span>Automatic renewal</span>
                            <h3>Configure monthly or yearly automatic deduction</h3>
                        </div>
                        <span class="badge <?php echo e($tenant->auto_billing_enabled ? 'bg-success-subtle text-success border border-success-subtle' : 'bg-secondary-subtle text-secondary border border-secondary-subtle'); ?>">
                            <?php echo e($tenant->autoBillingLabel()); ?>

                        </span>
                    </div>
                    <div class="p-3 p-lg-4">
                        <?php if($tenant->auto_billing_failed_at): ?>
                            <div class="alert alert-warning">
                                Last automatic billing failed on <?php echo e(app_datetime($tenant->auto_billing_failed_at)); ?>.
                                <?php echo e($tenant->auto_billing_failure_reason); ?>

                            </div>
                        <?php endif; ?>
                        <?php if($canManageBilling): ?>
                            <form method="POST" action="<?php echo e(route('tenant.billing.auto-renewal.update')); ?>" class="row g-3">
                                <?php echo csrf_field(); ?>
                                <div class="col-12">
                                    <div class="form-check form-switch auto-renewal-switch">
                                        <input class="form-check-input" type="checkbox" role="switch" id="auto_billing_enabled" name="auto_billing_enabled" value="1" <?php if($tenant->auto_billing_enabled): echo 'checked'; endif; ?>>
                                        <label class="form-check-label fw-semibold" for="auto_billing_enabled">Enable automatic monthly or yearly deduction</label>
                                    </div>
                                    <div class="form-text">Online card payment turns this on automatically when Paystack, Monnify, or Flutterwave returns a reusable authorization.</div>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label">Billing cycle</label>
                                    <select name="auto_billing_cycle" class="form-select">
                                        <option value="monthly" <?php if(($tenant->auto_billing_cycle ?? 'monthly') === 'monthly'): echo 'selected'; endif; ?>>Monthly</option>
                                        <option value="yearly" <?php if($tenant->auto_billing_cycle === 'yearly'): echo 'selected'; endif; ?>>Yearly</option>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label">Payment method</label>
                                    <select name="auto_billing_method" class="form-select">
                                        <option value="card" <?php if(($tenant->auto_billing_method ?? 'card') === 'card'): echo 'selected'; endif; ?>>Card authorization</option>
                                        <option value="bank" <?php if($tenant->auto_billing_method === 'bank'): echo 'selected'; endif; ?>>Bank mandate</option>
                                        <option value="gateway_authorization" <?php if($tenant->auto_billing_method === 'gateway_authorization'): echo 'selected'; endif; ?>>Gateway authorization</option>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label">Authorization reference</label>
                                    <input name="auto_billing_reference" value="<?php echo e(old('auto_billing_reference', $tenant->auto_billing_reference)); ?>" class="form-control" placeholder="AUTH_xxxxxxxxx">
                                </div>
                                <div class="col-md-4"><div class="section-mini-card"><span>Next deduction</span><strong><?php echo e(app_date($tenant->auto_billing_next_charge_at) ?? '-'); ?></strong></div></div>
                                <div class="col-md-4"><div class="section-mini-card"><span>Last charged</span><strong><?php echo e(app_date($tenant->auto_billing_last_charged_at) ?? '-'); ?></strong></div></div>
                                <div class="col-md-4"><div class="section-mini-card"><span>Stored method</span><strong><?php echo e($tenant->auto_billing_method ? ucfirst(str_replace('_', ' ', $tenant->auto_billing_method)) : '-'); ?></strong></div></div>
                                <div class="col-12 d-flex justify-content-end">
                                    <button class="btn btn-primary">Save automatic billing</button>
                                </div>
                            </form>
                        <?php else: ?>
                            <div class="text-muted small">Only tenant admins, managers, and accountants can update automatic renewal.</div>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="tab-pane fade" id="payments-pane" role="tabpanel" tabindex="0">
                    <div class="section-pane-head">
                        <div><span>Payments</span><h3>Subscription payment history</h3></div>
                    </div>
                    <div class="table-responsive">
                        <table class="table section-table mb-0">
                            <thead><tr><th>Reference</th><th>Plan</th><th>Amount</th><th>Channel</th><th>Status</th><th>Date</th></tr></thead>
                            <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $payments; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $payment): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($payment->reference); ?></td>
                                    <td><?php echo e($payment->plan?->name); ?></td>
                                    <td><?php echo e($money($payment->amount)); ?></td>
                                    <td>
                                        <span class="badge <?php echo e($payment->is_auto_billed ? 'bg-info-subtle text-info border border-info-subtle' : 'bg-light text-dark border'); ?>">
                                            <?php echo e($payment->is_auto_billed ? 'Auto' : ucfirst($payment->payment_channel ?? 'manual')); ?>

                                        </span>
                                    </td>
                                    <td><?php echo e(ucfirst($payment->status)); ?></td>
                                    <td><?php echo e(app_date($payment->created_at)); ?></td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr><td colspan="6" class="text-center text-muted py-4">No payments yet.</td></tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="p-3"><?php echo e($payments->links()); ?></div>
                </div>

                <div class="tab-pane fade" id="invoices-pane" role="tabpanel" tabindex="0">
                    <div class="section-pane-head">
                        <div><span>Invoices</span><h3>Recent subscription invoices</h3></div>
                    </div>
                    <div class="table-responsive">
                        <table class="table section-table mb-0">
                            <thead><tr><th>Invoice</th><th>Amount</th><th>Status</th><th>Paid</th></tr></thead>
                            <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $invoices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $invoice): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr><td><?php echo e($invoice->invoice_number); ?></td><td><?php echo e($money($invoice->amount)); ?></td><td><?php echo e(ucfirst($invoice->status)); ?></td><td><?php echo e(app_date($invoice->paid_at) ?? '-'); ?></td></tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr><td colspan="4" class="text-center text-muted py-4">No invoices yet.</td></tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
document.querySelectorAll('[data-subscription-payment-form]').forEach((form) => {
    const planSelect = form.querySelector('[data-plan-select]');
    const cycleSelect = form.querySelector('[data-cycle-select]');
    const previewAmount = form.querySelector('[data-plan-preview-amount]');
    const previewCycle = form.querySelector('[data-plan-preview-cycle]');

    if (! planSelect || ! cycleSelect) return;

    const updatePlanPreview = () => {
        const cycle = cycleSelect.value === 'yearly' ? 'yearly' : 'monthly';
        const suffix = cycle === 'yearly' ? '/yr' : '/mo';

        [...planSelect.options].forEach((option) => {
            const planName = option.dataset.planName || option.textContent;
            const label = cycle === 'yearly' ? option.dataset.yearlyLabel : option.dataset.monthlyLabel;
            option.textContent = `${planName} - ${label || '-'}${suffix}`;
        });

        const selectedOption = planSelect.selectedOptions[0];
        const selectedLabel = cycle === 'yearly'
            ? selectedOption?.dataset.yearlyLabel
            : selectedOption?.dataset.monthlyLabel;

        if (previewAmount) previewAmount.textContent = selectedLabel || '-';
        if (previewCycle) previewCycle.textContent = `${cycle.charAt(0).toUpperCase() + cycle.slice(1)} subscription`;
    };

    planSelect.addEventListener('change', updatePlanPreview);
    cycleSelect.addEventListener('change', updatePlanPreview);
    updatePlanPreview();
});
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\Polite hp\Documents\Codex\2026-05-08\build-a-complete-production-ready-saas\microfinance-saas\resources\views\tenant\billing\index.blade.php ENDPATH**/ ?>