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

<?php $__env->startSection('content'); ?>
<!-- Top Bar: Search and Actions -->
<div class="mb-6 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
    <!-- Search Bar -->
    <div class="relative flex-1 max-w-md">
        <i class="fas fa-search absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 dark:text-gray-500 text-sm"></i>
        <input type="text" id="usersSearch" placeholder="Search here" 
               class="w-full pl-10 pr-4 py-2.5 bg-white dark:bg-gray-900 border border-gray-300 dark:border-gray-700 rounded-lg text-sm text-gray-900 dark:text-white placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-blue-500 dark:focus:ring-blue-400 focus:border-transparent">
    </div>
    
    <!-- Action Buttons -->
    <div class="flex items-center gap-3">
        <a href="<?php echo e(route('admin.users.create')); ?>" 
           class="inline-flex items-center px-4 py-2.5 bg-blue-600 hover:bg-blue-700 text-white text-sm font-medium rounded-lg transition-colors">
            <i class="fas fa-plus mr-2"></i>New User
        </a>
    </div>
</div>

<!-- DataTable -->
<div class="bg-white dark:bg-gray-950 border border-gray-200 dark:border-gray-800 rounded-lg overflow-hidden">
    <div class="overflow-x-auto">
        <table id="usersTable" class="min-w-full divide-y divide-gray-200 dark:divide-gray-800">
            <thead class="bg-gray-50 dark:bg-gray-900">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                        <input type="checkbox" id="selectAll" class="rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                    </th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-800">
                        Name <i class="fas fa-chevron-down ml-1 text-xs"></i>
                    </th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-800">
                        Email <i class="fas fa-chevron-down ml-1 text-xs"></i>
                    </th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-800">
                        Created <i class="fas fa-chevron-down ml-1 text-xs"></i>
                    </th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-800">
                        Status <i class="fas fa-chevron-down ml-1 text-xs"></i>
                    </th>
                    <th class="px-4 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                        <i class="fas fa-th ml-auto"></i>
                    </th>
                </tr>
            </thead>
            <tbody class="bg-white dark:bg-gray-950 divide-y divide-gray-200 dark:divide-gray-800"></tbody>
        </table>
    </div>
</div>

<!-- jQuery and DataTables JS -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.5.0/js/dataTables.responsive.min.js"></script>

<?php echo $__env->make('admin.partials.datatables-styles', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

<script>
$(document).ready(function() {
    var searchDelay;
    
    // Initialize DataTable
    var table = $('#usersTable').DataTable({
        responsive: true,
        processing: true,
        serverSide: true,
        searchDelay: 200,
        ajax: {
            url: "<?php echo e(route('admin.users.index')); ?>",
            type: 'GET',
            dataSrc: function(data) {
                console.log('DataTables Response:', data);
                return data.data || [];
            },
            error: function(xhr, error, thrown) {
                console.error('DataTables AJAX Error:', {xhr: xhr, error: error, thrown: thrown});
            }
        },
        columns: [
            { data: 'checkbox', orderable: false, searchable: false, 
              render: function(data) { return data || ''; } },
            { data: 'user', orderable: true,
              render: function(data) { return data || ''; } },
            { data: 'email', orderable: true,
              render: function(data) { return data || ''; } },
            { data: 'created_at', orderable: true,
              render: function(data) { return data || ''; } },
            { data: 'status', orderable: true, searchable: false,
              render: function(data) { return data || ''; } },
            { data: 'actions', orderable: false, searchable: false,
              render: function(data) { return data || ''; } }
        ],
        pageLength: 25,
        lengthMenu: [[25, 50, 100, 200], [25, 50, 100, 200]],
        order: [[3, 'desc']], // Sort by created_at by default
        columnDefs: [
            { orderable: false, targets: [0, 5] }, // Disable sorting on checkbox and actions columns
            { className: "text-left", targets: [1, 2, 3, 4] },
            { className: "text-right", targets: [5] }
        ],
        language: {
            lengthMenu: "Show _MENU_",
            info: "Showing _START_ to _END_ of _TOTAL_",
            infoEmpty: "Showing 0 to 0 of 0",
            infoFiltered: "(filtered from _MAX_ total)",
            paginate: {
                first: "«",
                last: "»",
                next: "›",
                previous: "‹"
            },
            emptyTable: "No users available",
            processing: "Loading..."
        },
        dom: '<"flex justify-between items-center mb-4"<"flex items-center gap-2"l><"flex items-center gap-2"f>>rt<"flex justify-between items-center mt-4"<"text-sm text-gray-600 dark:text-gray-400"i><"flex items-center gap-1"p>>',
        pagingType: "simple_numbers",
        drawCallback: function() {
            // Re-initialize dropdowns after table redraw
            $('[id^="dropdown-"]').addClass('hidden');
        }
    });
    
    // Search input with 200ms debounce
    $('#usersSearch').on('keyup', function() {
        clearTimeout(searchDelay);
        searchDelay = setTimeout(function() {
            table.search($('#usersSearch').val()).draw();
        }, 200);
    });

    // Select All checkbox
    $('#selectAll').on('click', function() {
        $('.row-checkbox').prop('checked', this.checked);
    });

    // Individual row checkboxes
    $(document).on('change', '.row-checkbox', function() {
        if ($('.row-checkbox:checked').length === $('.row-checkbox').length) {
            $('#selectAll').prop('checked', true);
        } else {
            $('#selectAll').prop('checked', false);
        }
    });
});

// Toggle dropdown menu
function toggleDropdown(id) {
    var dropdown = document.getElementById('dropdown-' + id);
    if (dropdown) {
        dropdown.classList.toggle('hidden');
    }
}

// Close dropdown when clicking outside
document.addEventListener('click', function(event) {
    if (!event.target.closest('button') && !event.target.closest('div[id^="dropdown-"]')) {
        document.querySelectorAll('[id^="dropdown-"]').forEach(function(el) {
            el.classList.add('hidden');
        });
    }
});
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('admin.layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /Users/mac/Documents/360monicaAPI/resources/views/admin/users/index.blade.php ENDPATH**/ ?>