

<?php $__env->startSection('title', 'Tenants'); ?>
<?php $__env->startSection('page-title', 'Tenants'); ?>
<?php $__env->startSection('page-subtitle', 'Monitor active, trial, new, expired, and suspended companies'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $filters = [
        'all' => ['label' => 'All tenants', 'icon' => 'bi-buildings'],
        'active' => ['label' => 'Active tenants', 'icon' => 'bi-building-check'],
        'trial' => ['label' => 'Trial tenants', 'icon' => 'bi-hourglass-split'],
        'new' => ['label' => 'New tenants', 'icon' => 'bi-stars'],
        'expired' => ['label' => 'Expired tenants', 'icon' => 'bi-exclamation-triangle'],
        'suspended' => ['label' => 'Suspended tenants', 'icon' => 'bi-pause-circle'],
    ];
    $cardQuery = array_filter(request()->only(['q', 'plan_id']), fn ($value) => filled($value));
?>

<div class="row g-3 mb-4">
    <?php $__currentLoopData = $filters; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $filter): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="col-sm-6 col-xl-2">
            <a href="<?php echo e(route('super.tenants.index', $key === 'all' ? $cardQuery : array_merge($cardQuery, ['view' => $key]))); ?>"
               class="card border-0 shadow-sm h-100 text-decoration-none <?php echo e($view === $key ? 'bg-primary text-white' : 'text-reset'); ?>">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start gap-2">
                        <div>
                            <div class="<?php echo e($view === $key ? 'text-white-50' : 'text-muted'); ?> small"><?php echo e($filter['label']); ?></div>
                            <div class="h4 mb-0"><?php echo e($tenantCounts[$key] ?? 0); ?></div>
                        </div>
                        <i class="bi <?php echo e($filter['icon']); ?> fs-4 <?php echo e($view === $key ? 'text-white' : 'text-primary'); ?>"></i>
                    </div>
                </div>
            </a>
        </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-body">
        <div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
            <div>
                <h2 class="h5 mb-1"><?php echo e($filters[$view]['label'] ?? 'All tenants'); ?></h2>
                <div class="text-muted small">Search by company, owner, email, phone, tenant slug, or filter by plan and tenant state.</div>
            </div>
            <a href="<?php echo e(route('super.dashboard')); ?>" class="btn btn-sm btn-outline-secondary">
                <i class="bi bi-speedometer2 me-1"></i> Dashboard
            </a>
        </div>

        <form method="GET" action="<?php echo e(route('super.tenants.index')); ?>" class="row g-2 align-items-end mb-4" data-auto-filter-form>
            <div class="col-lg-4">
                <label class="form-label">Search tenants</label>
                <input name="q" value="<?php echo e($search); ?>" class="form-control" placeholder="Company, owner, email, phone..." data-auto-filter-search autocomplete="off">
            </div>
            <div class="col-md-4 col-lg-3">
                <label class="form-label">Tenant state</label>
                <select name="view" class="form-select" data-auto-filter-select>
                    <?php $__currentLoopData = $filters; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $filter): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($key); ?>" <?php if($view === $key): echo 'selected'; endif; ?>><?php echo e($filter['label']); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            <div class="col-md-4 col-lg-3">
                <label class="form-label">Plan</label>
                <select name="plan_id" class="form-select" data-auto-filter-select>
                    <option value="">All plans</option>
                    <?php $__currentLoopData = $plans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $plan): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($plan->id); ?>" <?php if((string) $planId === (string) $plan->id): echo 'selected'; endif; ?>><?php echo e($plan->name); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            <div class="col-md-4 col-lg-2 d-flex flex-column gap-2">
                <div class="small text-muted">Filters apply automatically.</div>
                <?php if($search !== '' || $planId || $view !== 'all'): ?>
                    <a href="<?php echo e(route('super.tenants.index')); ?>" class="btn btn-outline-secondary btn-sm">
                        <i class="bi bi-x-lg me-1"></i> Clear
                    </a>
                <?php endif; ?>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table align-middle">
                <thead><tr><th>Company</th><th>Owner</th><th>Contact</th><th>Plan</th><th>Tenant state</th><th>Plan/Trial ends</th><th></th></tr></thead>
                <tbody>
                <?php $__empty_1 = true; $__currentLoopData = $tenants; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tenant): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <?php
                        $expiresAt = $tenant->subscription_ends_at ?? $tenant->trial_ends_at;
                        $expiresLabel = $tenant->subscription_ends_at ? 'Subscription' : ($tenant->trial_ends_at ? 'Trial' : null);

                        if ($tenant->status === 'suspended') {
                            $stateLabel = 'Suspended';
                            $stateClass = 'text-bg-secondary';
                        } elseif ($tenant->isExpired()) {
                            $stateLabel = 'Expired';
                            $stateClass = 'text-bg-warning';
                        } elseif ($tenant->isOnTrial()) {
                            $stateLabel = 'Trial';
                            $stateClass = 'text-bg-info';
                        } elseif ($tenant->isActive()) {
                            $stateLabel = 'Active';
                            $stateClass = 'text-bg-success';
                        } else {
                            $stateLabel = ucfirst($tenant->status);
                            $stateClass = 'text-bg-light';
                        }
                    ?>
                    <tr>
                        <td>
                            <div class="fw-semibold"><?php echo e($tenant->name); ?></div>
                            <small class="text-muted"><?php echo e($tenant->slug); ?></small>
                        </td>
                        <td><?php echo e($tenant->owner_name); ?></td>
                        <td>
                            <div><?php echo e($tenant->email); ?></div>
                            <small class="text-muted"><?php echo e($tenant->phone ?: '-'); ?></small>
                        </td>
                        <td><?php echo e($tenant->plan?->name ?? 'None'); ?></td>
                        <td>
                            <span class="badge <?php echo e($stateClass); ?>"><?php echo e($stateLabel); ?></span>
                            <?php if($tenant->isOnTrial()): ?>
                                <div class="small text-muted mt-1"><?php echo e($tenant->trialDaysRemaining()); ?> trial days left</div>
                            <?php endif; ?>
                        </td>
                        <td>
                            <div><?php echo e(app_date($expiresAt) ?? '-'); ?></div>
                            <?php if($expiresLabel): ?>
                                <small class="text-muted"><?php echo e($expiresLabel); ?></small>
                            <?php endif; ?>
                        </td>
                        <td>
                            <div class="d-flex flex-wrap gap-2 justify-content-end">
                                <a href="<?php echo e(route('super.tenants.show', $tenant)); ?>" class="btn btn-sm btn-outline-primary">Manage</a>
                                <form method="POST" action="<?php echo e(route('super.tenants.impersonate', $tenant)); ?>">
                                    <?php echo csrf_field(); ?>
                                    <button class="btn btn-sm btn-dark" title="Enter tenant portal" aria-label="Enter tenant portal"><i class="bi bi-box-arrow-in-right"></i></button>
                                </form>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="7" class="text-center text-muted py-4">No tenants found for this search or filter.</td>
                    </tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php echo e($tenants->links()); ?>

    </div>
</div>

<script>
document.querySelectorAll('[data-auto-filter-form]').forEach((form) => {
    const search = form.querySelector('[data-auto-filter-search]');
    const selects = form.querySelectorAll('[data-auto-filter-select]');
    let timer = null;

    const submit = () => form.requestSubmit ? form.requestSubmit() : form.submit();

    if (search) {
        search.addEventListener('input', () => {
            clearTimeout(timer);
            timer = setTimeout(submit, 650);
        });
    }

    selects.forEach((select) => {
        select.addEventListener('change', submit);
    });
});
</script>
<?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\tenants\index.blade.php ENDPATH**/ ?>