

<?php $__env->startSection('content'); ?>
<div class="container">
    <div class="container-header">
        <h1><?php echo e($pageTitle); ?></h1>
    </div>

    <div class="card table-card">
        <div class="table-header">
            <form method="GET" action="<?php echo e(route('backend.users.index')); ?>" class="table-search-form">
                <div class="table-search">
                    <input type="text" name="search" class="search-input" placeholder="Search users..." value="<?php echo e($search ?? ''); ?>">
                    <i class="las la-search"></i>
                </div>
                <button type="submit" class="btn btn-secondary search-btn">Search</button>
                <?php if(!empty($search)): ?>
                    <a href="<?php echo e(route('backend.users.index')); ?>" class="btn btn-outline">Clear</a>
                <?php endif; ?>
            </form>
            <button class="btn btn-primary" id="add-user"><i class="las la-plus"></i>Add user</button>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th><?php echo e(__('SL')); ?></th>
                        <th><?php echo e(__('Full name')); ?></th>
                        <th><?php echo e(__('Phone')); ?></th>
                        <th><?php echo e(__('Email')); ?></th>
                        <th><?php echo e(__('Country')); ?></th>
                        <th><?php echo e(__('Status')); ?></th>
                        <th><?php echo e(__('Action')); ?></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($users->firstItem() + $loop->index); ?></td>
                        <td><?php echo e($user->fname); ?> <?php echo e($user->lname); ?></td>
                        <td><?php echo e($user->phone); ?></td>
                        <td><?php echo e($user->email); ?></td>
                        <td><?php echo e($user->country ?? 'N/A'); ?></td>
                        <td>
                            <div class="toggle-switch">
                                <input type="checkbox" id="toggle-<?php echo e($user->id); ?>" <?php echo e($user->status == 1 ? 'checked' : ''); ?>>
                                <label for="toggle-<?php echo e($user->id); ?>" class="toggle-label">
                                    <span class="toggle-text"><?php echo e($user->status == 1 ? 'On' : 'Off'); ?></span>
                                </label>
                            </div>
                        </td>
                        <td>
                            <div class="table-actions">
                                <a href="<?php echo e(route('backend.users.login-as-user', $user->id)); ?>" target="_blank" class="table-btn"><i class="las la-long-arrow-alt-right"></i></a> 
                                <a href="<?php echo e(route('backend.users.details', $user->id)); ?>" class="table-btn"><i class="las la-pen"></i></a>
                                <a href="" class="table-btn"><i class="las la-trash"></i></a>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="7" class="text-center"><?php echo e(__('No data found')); ?></td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <?php echo e($users->links('vendor.pagination.default')); ?>

    </div>
</div>

<div class="modal" id="add-user-modal">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">Add user</h3>
            <button class="close-modal"><i class="las la-times"></i></button>
        </div>
        <form action="<?php echo e(route('backend.users.add-user')); ?>" method="POST">
            <?php echo csrf_field(); ?>
            <div class="modal-body">
                <div class="form-group mb-25">
                    <label class="form-label">First Name<span class="required">*</span></label>
                    <input type="text" class="form-control" name="fname" placeholder="Enter first name">
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Last Name<span class="required">*</span></label>
                    <input type="text" class="form-control" name="lname" placeholder="Enter last name">
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Email<span class="required">*</span></label>
                    <input type="text" class="form-control" name="email" placeholder="Enter email">
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Password<span class="required">*</span></label>
                    <input type="password" class="form-control" name="password" placeholder="Enter password">
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="submit">Add user</button>
            </div>
        </form>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('script'); ?>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const toggleSwitches = document.querySelectorAll('input[type="checkbox"][id^="toggle-"]');

            toggleSwitches.forEach(toggle =>  {
                toggle.addEventListener('change', function() {
                    const userId = this.id.replace('toggle-', '');
                    const status = this.checked ? 1 : 0;
                    const toggleText = this.nextElementSibling.querySelector('.toggle-text');

                    toggleText.textContent = status ? 'On' : 'Off';

                    fetch('<?php echo e(route('backend.users.toggle-status')); ?>', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
                        },
                        body: JSON.stringify({
                            id: userId,
                            status: status
                        })
                    })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            iziToast.success({
                                message: 'Status updated successfully',
                                position: 'topRight'
                            })
                        } else {
                            this.checked = !this.checked;
                            toggleText.textContent = this.checked ? 'On' : 'Off';
                            iziToast.error({
                                message: 'Failed to update status',
                                position: 'topRight'
                            })
                        }
                    })
                    .catch(error =>{
                        console.error('Error:', error);
                        this.checked = !this.checked;
                        toggleText.textContent = this.checked ? 'On' : 'Off';
                        iziToast.error({
                            message: 'Failed to update status',
                            position: 'topRight'
                        })
                    } )
                })
            })
        })
    </script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('backend.layout.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\HP\Desktop\nbverify\mine\resources\views/backend/users/index.blade.php ENDPATH**/ ?>