

<?php $__env->startSection('title', 'Users'); ?>
<?php $__env->startSection('page-title', 'Users Management'); ?>

<?php $__env->startSection('content'); ?>
<div class="table-container">
    <!-- Search and Filters -->
    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
        <form action="<?php echo e(route('dashboard.users')); ?>" method="GET" class="d-flex gap-2 flex-wrap">
            <input type="text"
                name="search"
                class="form-control"
                placeholder="Search by name, email, or phone..."
                value="<?php echo e(request('search')); ?>"
                style="width: 300px;">
            <select name="status" class="form-select" style="width: 150px;">
                <option value="">All Status</option>
                <option value="active" <?php echo e(request('status') == 'active' ? 'selected' : ''); ?>>Active</option>
                <option value="inactive" <?php echo e(request('status') == 'inactive' ? 'selected' : ''); ?>>Inactive</option>
            </select>
            <?php if($clients->count() > 0): ?>
            <select name="client_id" class="form-select" style="width: 200px;">
                <option value="">All Organizations</option>
                <?php $__currentLoopData = $clients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $client): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <option value="<?php echo e($client->id); ?>" <?php echo e(request('client_id') == $client->id ? 'selected' : ''); ?>>
                    <?php echo e($client->name); ?>

                </option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
            <?php endif; ?>
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-search"></i>
            </button>
            <?php if(request()->hasAny(['search', 'status', 'client_id'])): ?>
            <a href="<?php echo e(route('dashboard.users')); ?>" class="btn btn-outline-secondary">
                <i class="bi bi-x-circle"></i>
            </a>
            <?php endif; ?>
        </form>

        <div class="d-flex align-items-center gap-3">
            <span class="text-muted">Total: <?php echo e($users->total()); ?> users</span>
            <?php if(auth()->guard('admin')->user()->hasPermission('users.create')): ?>
            <a href="<?php echo e(route('dashboard.users.create')); ?>" class="btn btn-success">
                <i class="bi bi-person-plus"></i> Add User
            </a>
            <?php endif; ?>
        </div>
    </div>

    <?php if(!request('client_id')): ?>
    
    <?php $__empty_1 = true; $__currentLoopData = $groupedUsers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $clientId => $orgUsers): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
    <?php
        $orgName = $clientId ? ($orgUsers->first()->client->name ?? 'Unknown Organization') : 'Unassigned Users';
        $orgActive = $clientId ? ($orgUsers->first()->client->is_active ?? false) : true;
    ?>
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-header bg-light d-flex justify-content-between align-items-center">
            <div class="d-flex align-items-center gap-2">
                <div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center" style="width: 32px; height: 32px; font-size: 0.8rem;">
                    <?php echo e(strtoupper(substr($orgName, 0, 1))); ?>

                </div>
                <div>
                    <h6 class="mb-0">
                        <i class="bi bi-building me-1"></i><?php echo e($orgName); ?>

                        <span class="badge bg-<?php echo e($orgActive ? 'success' : 'secondary'); ?> ms-2" style="font-size: 0.65rem;">
                            <?php echo e($orgActive ? 'Active' : 'Inactive'); ?>

                        </span>
                    </h6>
                    <small class="text-muted"><?php echo e($orgUsers->count()); ?> user(s)</small>
                </div>
            </div>
            <?php if($isSuperAdmin && $clientId): ?>
            <a href="<?php echo e(route('dashboard.clients.show', $clientId)); ?>" class="btn btn-sm btn-outline-primary">
                <i class="bi bi-eye me-1"></i>View Org
            </a>
            <?php endif; ?>
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Status</th>
                        <th>SMS</th>
                        <th>WhatsApp</th>
                        <th>Promotions</th>
                        <th>Last Active</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $orgUsers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <?php echo $__env->make('dashboard.partials.user-row', ['user' => $user], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
    <div class="text-center text-muted py-5">
        <i class="bi bi-people" style="font-size: 3rem;"></i>
        <p class="mt-3">No users found. <a href="<?php echo e(route('dashboard.users.create')); ?>">Create your first user</a></p>
    </div>
    <?php endif; ?>
    <?php else: ?>
    
    <div class="table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Phone</th>
                    <th>Organization</th>
                    <th>Status</th>
                    <th>SMS</th>
                    <th>WhatsApp</th>
                    <th>Promotions</th>
                    <th>Last Active</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php $__empty_1 = true; $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                <tr>
                    <td><?php echo e($user->id); ?></td>
                    <td>
                        <strong><?php echo e($user->name); ?></strong>
                        <br><small class="text-muted"><?php echo e($user->email); ?></small>
                    </td>
                    <td><?php echo e($user->phone); ?></td>
                    <td>
                        <?php if($user->client): ?>
                        <a href="<?php echo e(route('dashboard.clients.show', $user->client_id)); ?>" class="text-decoration-none">
                            <span class="badge bg-primary bg-opacity-10 text-primary">
                                <i class="bi bi-building me-1"></i><?php echo e($user->client->name); ?>

                            </span>
                        </a>
                        <?php else: ?>
                        <span class="badge bg-secondary">Unassigned</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if($user->is_active): ?>
                        <span class="badge bg-success">Active</span>
                        <?php else: ?>
                        <span class="badge bg-danger">Inactive</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if($user->sms_service_enabled): ?>
                        <span class="badge bg-success"><i class="bi bi-check"></i></span>
                        <?php else: ?>
                        <span class="badge bg-secondary"><i class="bi bi-x"></i></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if($user->settings && $user->settings->whatsapp_enabled): ?>
                        <span class="badge bg-success"><i class="bi bi-whatsapp"></i></span>
                        <?php else: ?>
                        <span class="badge bg-secondary"><i class="bi bi-x"></i></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if($user->promotions_enabled): ?>
                        <span class="badge bg-success"><i class="bi bi-check"></i></span>
                        <?php else: ?>
                        <span class="badge bg-secondary"><i class="bi bi-x"></i></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if($user->last_active_at): ?>
                        <?php echo e($user->last_active_at->diffForHumans()); ?>

                        <?php else: ?>
                        <span class="text-muted">Never</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div class="btn-group">
                            <?php if(auth()->guard('admin')->user()->hasPermission('users.view')): ?>
                            <a href="<?php echo e(route('dashboard.user-details', $user->id)); ?>"
                                class="btn btn-sm btn-outline-primary" title="View Details">
                                <i class="bi bi-eye"></i>
                            </a>
                            <?php endif; ?>
                            <?php if(auth()->guard('admin')->user()->hasPermission('users.edit')): ?>
                            <a href="<?php echo e(route('dashboard.users.edit', $user->id)); ?>"
                                class="btn btn-sm btn-outline-secondary" title="Edit">
                                <i class="bi bi-pencil"></i>
                            </a>
                            <?php endif; ?>
                            <?php if(auth()->guard('admin')->user()->hasPermission('users.toggle_status')): ?>
                            <form action="<?php echo e(route('dashboard.users.toggle-status', $user->id)); ?>" method="POST" class="d-inline">
                                <?php echo csrf_field(); ?>
                                <button type="submit" class="btn btn-sm btn-outline-<?php echo e($user->is_active ? 'warning' : 'success'); ?>"
                                    title="<?php echo e($user->is_active ? 'Deactivate' : 'Activate'); ?>">
                                    <i class="bi bi-<?php echo e($user->is_active ? 'pause' : 'play'); ?>"></i>
                                </button>
                            </form>
                            <?php endif; ?>
                            <?php if(auth()->guard('admin')->user()->hasPermission('users.delete')): ?>
                            <form action="<?php echo e(route('dashboard.users.delete', $user->id)); ?>" method="POST" class="d-inline">
                                <?php echo csrf_field(); ?>
                                <?php echo method_field('DELETE'); ?>
                                <button type="submit" class="btn btn-sm btn-outline-danger" title="Delete"
                                    data-confirm="Are you sure?"
                                    data-confirm-title="Delete User"
                                    data-confirm-btn="Delete"
                                    data-confirm-class="btn-danger">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </form>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                <tr>
                    <td colspan="10" class="text-center text-muted py-4">
                        No users found. <a href="<?php echo e(route('dashboard.users.create')); ?>">Create your first user</a>
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>

    <!-- Pagination -->
    <div class="d-flex justify-content-center mt-4">
        <?php echo e($users->withQueryString()->links()); ?>

    </div>
</div>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.dashboard', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH D:\xampp82\htdocs\AutoPromix\backend\resources\views/dashboard/users.blade.php ENDPATH**/ ?>