

<?php $__env->startSection('title', 'Manage Merchant'); ?>
<?php $__env->startSection('page-title', $agent->displayName()); ?>
<?php $__env->startSection('page-subtitle', 'Merchant profile, assigned tenants, and subscription commissions'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $statusClass = match ($agent->status) {
        \App\Models\Agent::STATUS_ACTIVE => 'text-bg-success',
        \App\Models\Agent::STATUS_PENDING => 'text-bg-warning',
        \App\Models\Agent::STATUS_SUSPENDED => 'text-bg-secondary',
        default => 'text-bg-light',
    };
    $kycClass = match ($agent->kyc_status) {
        \App\Models\Agent::KYC_VERIFIED => 'text-bg-success',
        \App\Models\Agent::KYC_SUBMITTED => 'text-bg-info',
        \App\Models\Agent::KYC_REJECTED => 'text-bg-danger',
        default => 'text-bg-warning',
    };
?>

<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-4">
    <div>
        <span class="badge <?php echo e($statusClass); ?>"><?php echo e($agent->statusLabel()); ?></span>
        <span class="badge text-bg-info"><?php echo e($agent->merchantTypeLabel()); ?></span>
        <span class="badge text-bg-light"><?php echo e(\App\Models\Agent::SELF_ONBOARDING_COMMISSION_RATE); ?>% self-recruited</span>
        <span class="badge text-bg-light"><?php echo e(\App\Models\Agent::ASSIGNED_ONBOARDING_COMMISSION_RATE); ?>% assigned</span>
        <span class="badge text-bg-light"><?php echo e(\App\Models\Agent::REFERRAL_COMMISSION_RATE); ?>% referral</span>
        <span class="badge <?php echo e($kycClass); ?>">KYC: <?php echo e($agent->kycStatusLabel()); ?></span>
        <span class="badge text-bg-light"><?php echo e($agent->code); ?></span>
    </div>
    <div class="d-flex flex-wrap gap-2">
        <a href="<?php echo e(route('super.agents.index')); ?>" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left me-1"></i> Merchants</a>
        <a href="<?php echo e($agent->referralUrl()); ?>" target="_blank" rel="noopener" class="btn btn-outline-primary btn-sm"><i class="bi bi-globe2 me-1"></i> Landing page</a>
        <a href="<?php echo e($agent->tenantSignupUrl()); ?>" target="_blank" rel="noopener" class="btn btn-primary btn-sm"><i class="bi bi-link-45deg me-1"></i> Onboarding link</a>
    </div>
</div>

<div class="row g-3 mb-4">
    <?php $__currentLoopData = [
        ['Assigned tenants', $agent->tenants->count(), 'bi-buildings', 'text-info'],
        ['Pending commission', number_format($commissionStats['pending'], 2), 'bi-wallet2', 'text-danger'],
        ['Paid commission', number_format($commissionStats['paid'], 2), 'bi-check2-circle', 'text-success'],
        ['Total commission', number_format($commissionStats['total'], 2), 'bi-cash-coin', 'text-primary'],
    ]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label, $value, $icon, $color]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="col-md-6 col-xl-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body d-flex justify-content-between">
                    <div>
                        <div class="text-muted small"><?php echo e($label); ?></div>
                        <div class="h4 mb-0"><?php echo e($value); ?></div>
                    </div>
                    <i class="bi <?php echo e($icon); ?> fs-3 <?php echo e($color); ?>"></i>
                </div>
            </div>
        </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
</div>

<div class="row g-4">
    <div class="col-xl-4">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-body">
                <h2 class="h5 mb-3">Profile</h2>
                <form method="POST" action="<?php echo e(route('super.agents.update', $agent)); ?>" class="row g-3">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('PATCH'); ?>
                    <div class="col-12">
                        <label class="form-label">Full name</label>
                        <input name="name" value="<?php echo e(old('name', $agent->user->name)); ?>" class="form-control" required>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Business name</label>
                        <input name="business_name" value="<?php echo e(old('business_name', $agent->business_name)); ?>" class="form-control">
                    </div>
                    <div class="col-md-7">
                        <label class="form-label">Email</label>
                        <input name="email" type="email" value="<?php echo e(old('email', $agent->user->email)); ?>" class="form-control" required>
                    </div>
                    <div class="col-md-5">
                        <label class="form-label">Phone</label>
                        <input name="phone" value="<?php echo e(old('phone', $agent->user->phone)); ?>" class="form-control">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Country</label>
                        <input name="country" value="<?php echo e(old('country', $agent->country ?: 'Nigeria')); ?>" class="form-control" required>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">State</label>
                        <input name="state" value="<?php echo e(old('state', $agent->state)); ?>" class="form-control" required>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">City / Town</label>
                        <input name="city" value="<?php echo e(old('city', $agent->city)); ?>" class="form-control" required>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Full address</label>
                        <input name="residential_address" value="<?php echo e(old('residential_address', $agent->residential_address)); ?>" class="form-control" required>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Merchant type</label>
                        <select name="merchant_type" class="form-select" required>
                            <?php $__currentLoopData = $merchantTypes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($key); ?>" <?php if(old('merchant_type', $agent->merchant_type) === $key): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Self-recruited %</label>
                        <input name="onboarding_commission_rate" type="number" min="0" max="<?php echo e(\App\Models\Agent::MAX_COMMISSION_RATE); ?>" step="0.01" value="<?php echo e(old('onboarding_commission_rate', \App\Models\Agent::SELF_ONBOARDING_COMMISSION_RATE)); ?>" class="form-control" readonly required>
                        <div class="form-text">Starts at <?php echo e(\App\Models\Agent::SELF_ONBOARDING_COMMISSION_RATE); ?>% and grows by merchant level.</div>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Referral %</label>
                        <input name="referral_commission_rate" type="number" min="0" max="<?php echo e(\App\Models\Agent::MAX_COMMISSION_RATE); ?>" step="0.01" value="<?php echo e(old('referral_commission_rate', \App\Models\Agent::REFERRAL_COMMISSION_RATE)); ?>" class="form-control" readonly required>
                        <div class="form-text">Assigned onboarding is fixed at <?php echo e(\App\Models\Agent::ASSIGNED_ONBOARDING_COMMISSION_RATE); ?>%.</div>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Status</label>
                        <select name="status" class="form-select">
                            <?php $__currentLoopData = $agentStatuses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($key); ?>" <?php if(old('status', $agent->status) === $key): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-12">
                        <label class="form-label">KYC status</label>
                        <select name="kyc_status" class="form-select" required>
                            <?php $__currentLoopData = \App\Models\Agent::KYC_STATUSES; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($key); ?>" <?php if(old('kyc_status', $agent->kyc_status ?? \App\Models\Agent::KYC_NOT_SUBMITTED) === $key): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <div class="form-text">Set to verified after reviewing the submitted merchant details and documents.</div>
                    </div>
                    <div class="col-12">
                        <label class="form-label">KYC rejection reason</label>
                        <textarea name="kyc_rejected_reason" class="form-control" rows="2" placeholder="Only required when rejecting KYC"><?php echo e(old('kyc_rejected_reason', $agent->kyc_rejected_reason)); ?></textarea>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">New password</label>
                        <input name="password" type="password" class="form-control" placeholder="Leave blank">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Confirm</label>
                        <input name="password_confirmation" type="password" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Bank name</label>
                        <input name="bank_name" value="<?php echo e(old('bank_name', $agent->bank_name)); ?>" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Account number</label>
                        <input name="account_number" value="<?php echo e(old('account_number', $agent->account_number)); ?>" class="form-control">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Account name</label>
                        <input name="account_name" value="<?php echo e(old('account_name', $agent->account_name)); ?>" class="form-control">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Notes</label>
                        <textarea name="notes" class="form-control" rows="3"><?php echo e(old('notes', $agent->notes)); ?></textarea>
                    </div>
                    <div class="col-12">
                        <button class="btn btn-primary w-100"><i class="bi bi-save me-1"></i> Save changes</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <h2 class="h5 mb-3">Assign tenants</h2>
                <form method="POST" action="<?php echo e(route('super.agents.tenants.assign', $agent)); ?>">
                    <?php echo csrf_field(); ?>
                    <label class="form-label">Select tenant(s)</label>
                    <select name="tenant_ids[]" class="form-select" multiple size="8" required>
                        <?php $__currentLoopData = $availableTenants; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tenant): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($tenant->id); ?>">
                                <?php echo e($tenant->name); ?><?php echo e($tenant->agent_id && $tenant->agent_id !== $agent->id ? ' - currently '.$tenant->agent?->displayName() : ''); ?>

                            </option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                    <label class="form-label mt-3">Commission type for selected tenant(s)</label>
                    <select name="commission_type" class="form-select" required>
                        <option value="<?php echo e(\App\Models\Agent::MERCHANT_ASSIGNED_ONBOARDING); ?>">Assigned onboarding commission (<?php echo e(\App\Models\Agent::ASSIGNED_ONBOARDING_COMMISSION_RATE); ?>%)</option>
                        <option value="<?php echo e(\App\Models\Agent::MERCHANT_REFERRAL); ?>">Referral commission (<?php echo e(\App\Models\Agent::REFERRAL_COMMISSION_RATE); ?>%)</option>
                    </select>
                    <div class="form-text">Selecting a tenant already assigned to another merchant will move it to this merchant.</div>
                    <button class="btn btn-outline-primary w-100 mt-3"><i class="bi bi-person-plus me-1"></i> Assign selected</button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-xl-8">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-body">
                <div class="d-flex flex-column flex-lg-row justify-content-between gap-2 mb-3">
                    <div>
                        <h2 class="h5 mb-1">Merchant KYC details</h2>
                        <div class="text-muted small">Submitted merchant verification details for super admin review.</div>
                    </div>
                    <span class="badge <?php echo e($kycClass); ?> align-self-start"><?php echo e($agent->kycStatusLabel()); ?></span>
                </div>
                <div class="row g-3">
                    <?php $__currentLoopData = [
                        ['Date of birth', app_date($agent->date_of_birth)],
                        ['Country', $agent->country ?: 'Nigeria'],
                        ['Full address', $agent->residential_address],
                        ['State / City/Town', trim(($agent->state ?: '-').' / '.($agent->city ?: '-'))],
                        ['Coverage area', $agent->coverage_area],
                        ['ID type', $agent->id_type ? str($agent->id_type)->replace('_', ' ')->headline() : null],
                        ['ID number', $agent->id_number],
                        ['BVN', $agent->bvn],
                        ['NIN', $agent->nin],
                        ['Business reg. no.', $agent->business_registration_number],
                        ['Experience', filled($agent->years_experience) ? $agent->years_experience.' year(s)' : null],
                        ['Referee', trim(($agent->referee_name ?: '-').' / '.($agent->referee_phone ?: '-'))],
                        ['Submitted', $agent->kyc_submitted_at?->diffForHumans()],
                    ]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label, $value]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="col-md-6 col-xl-4">
                            <div class="border rounded p-3 h-100">
                                <div class="text-muted small"><?php echo e($label); ?></div>
                                <div class="fw-semibold"><?php echo e(filled($value) ? $value : '-'); ?></div>
                            </div>
                        </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    <div class="col-md-6">
                        <div class="border rounded p-3 h-100">
                            <div class="text-muted small">Passport photo</div>
                            <?php if($agent->passport_photo_path): ?>
                                <a href="<?php echo e(asset('uploads/'.$agent->passport_photo_path)); ?>" target="_blank" rel="noopener">View uploaded photo</a>
                            <?php else: ?>
                                <div class="fw-semibold">-</div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="border rounded p-3 h-100">
                            <div class="text-muted small">ID document</div>
                            <?php if($agent->id_document_path): ?>
                                <a href="<?php echo e(asset('uploads/'.$agent->id_document_path)); ?>" target="_blank" rel="noopener">View uploaded ID</a>
                            <?php else: ?>
                                <div class="fw-semibold">-</div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-4">
            <div class="card-body">
                <h2 class="h5 mb-3">Merchant links</h2>
                <div class="row g-2">
                    <div class="col-lg-4">
                        <label class="form-label small text-muted">Merchant landing page</label>
                        <input class="form-control" value="<?php echo e($agent->referralUrl()); ?>" readonly onclick="this.select()">
                    </div>
                    <div class="col-lg-4">
                        <label class="form-label small text-muted">Onboarding registration</label>
                        <input class="form-control" value="<?php echo e($agent->tenantSignupUrl()); ?>" readonly onclick="this.select()">
                    </div>
                    <div class="col-lg-4">
                        <label class="form-label small text-muted">Referral-only registration</label>
                        <input class="form-control" value="<?php echo e($agent->referralSignupUrl()); ?>" readonly onclick="this.select()">
                    </div>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white fw-semibold">Assigned tenants</div>
            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead><tr><th>Company</th><th>Plan</th><th>Status</th><th>Commission</th><th>Assigned</th><th></th></tr></thead>
                    <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $agent->tenants; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tenant): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr>
                            <td>
                                <div class="fw-semibold"><?php echo e($tenant->name); ?></div>
                                <small class="text-muted"><?php echo e($tenant->email); ?> · <?php echo e($tenant->phone ?: '-'); ?></small>
                            </td>
                            <td><?php echo e($tenant->plan?->name ?? '-'); ?></td>
                            <td><span class="badge text-bg-light"><?php echo e($tenant->isOnTrial() ? 'Trial' : ucfirst($tenant->status)); ?></span></td>
                            <td>
                                <div><?php echo e(\App\Models\Agent::commissionTypeLabel($tenant->agent_commission_type)); ?></div>
                                <small class="text-muted"><?php echo e(\App\Models\Agent::baseCommissionRateFor($tenant->agent_commission_type)); ?>% base</small>
                            </td>
                            <td><?php echo e(app_date($tenant->agent_assigned_at) ?? '-'); ?></td>
                            <td class="text-end">
                                <form method="POST" action="<?php echo e(route('super.agents.tenants.unassign', [$agent, $tenant])); ?>">
                                    <?php echo csrf_field(); ?>
                                    <?php echo method_field('DELETE'); ?>
                                    <button class="btn btn-sm btn-outline-danger">Remove</button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr><td colspan="6" class="text-center text-muted py-4">No tenants assigned to this merchant yet.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white fw-semibold">Commissions</div>
            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead><tr><th>Tenant</th><th>Payment</th><th>Type</th><th>Rate</th><th>Commission</th><th>Status</th><th></th></tr></thead>
                    <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $agent->commissions->sortByDesc('earned_at'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $commission): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr>
                            <td>
                                <div class="fw-semibold"><?php echo e($commission->tenant?->name ?? 'Tenant'); ?></div>
                                <small class="text-muted"><?php echo e($commission->plan?->name ?? '-'); ?></small>
                            </td>
                            <td>
                                <div><?php echo e(number_format($commission->commissionable_amount, 2)); ?></div>
                                <small class="text-muted"><?php echo e(ucfirst($commission->billing_cycle)); ?> · <?php echo e(app_date($commission->earned_at)); ?></small>
                            </td>
                            <td><?php echo e(\App\Models\Agent::commissionTypeLabel($commission->commission_type)); ?></td>
                            <td><?php echo e($commission->commission_rate); ?>%</td>
                            <td><?php echo e(number_format($commission->commission_amount, 2)); ?></td>
                            <td>
                                <span class="badge <?php echo e($commission->status === \App\Models\AgentCommission::STATUS_PAID ? 'text-bg-success' : 'text-bg-warning'); ?>">
                                    <?php echo e($commission->statusLabel()); ?>

                                </span>
                            </td>
                            <td class="text-end">
                                <?php if($commission->status !== \App\Models\AgentCommission::STATUS_PAID): ?>
                                    <form method="POST" action="<?php echo e(route('super.agents.commissions.paid', [$agent, $commission])); ?>">
                                        <?php echo csrf_field(); ?>
                                        <?php echo method_field('PATCH'); ?>
                                        <button class="btn btn-sm btn-outline-success">Mark paid</button>
                                    </form>
                                <?php else: ?>
                                    <span class="small text-muted"><?php echo e(app_date($commission->paid_at)); ?></span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr><td colspan="7" class="text-center text-muted py-4">No commissions recorded yet.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?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\super\agents\show.blade.php ENDPATH**/ ?>