<?php $__env->startSection('main-body'); ?>
<div class="container-fluid">
    <div class="page-header">
        <div class="row">
            <div class="col-sm-12">
                <h3>Users Management</h3>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="/">Dashboard</a></li>
                    <li class="breadcrumb-item active"><a href="/users">Users</a></li>
                </ol>
                <?php if($loggedUser->can('create-users')): ?>
                <span class="pull-right"><?php echo Html::decode(link_to_Route('users.create', '<i class="fa fa-plus"></i> Create User', [], ['class' => 'btn btn-primary'])); ?></span>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-styling" id="user-list">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Email Address</th>
                                    <th>Role</th>
                                    <th>Verified</th>
                                    <th>Status</th>
                                    <th class="text-center">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td>
                                        <?php if(empty($user->userProfile)): ?>
                                            <?php echo $user->username; ?>

                                        <?php else: ?>
                                            <?php echo $user->userProfile->first_name.' '.$user->userProfile->last_name; ?>

                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo $user->email; ?></td>
                                    <td><?php echo $user->role->role; ?></td>
                                    <td>
                                        <h6>
                                        <?php if($user->verified): ?>
                                            <span class="badge badge-primary">Yes</span>
                                        <?php else: ?>
                                            <span class="badge badge-secondary">No</span>
                                        <?php endif; ?>
                                        </h6>
                                    </td>
                                    <td>
                                        <label class="switch">
                                            <?php if($loggedUser->can('update-users')): ?>
                                                <?php if($user->status === 'Active'): ?>
                                                <input type="checkbox" checked="" id="<?php echo e($user->id); ?>" onchange="updateStatus(this)"><span class="switch-state"></span>
                                                <?php else: ?>
                                                <input type="checkbox" id="<?php echo e($user->id); ?>" onchange="updateStatus(this)"><span class="switch-state"></span>
                                                <?php endif; ?>
                                            <?php else: ?>
                                                <?php if($user->status === 'Active'): ?>
                                                <input type="checkbox" checked="" id="<?php echo e($user->id); ?>" disabled><span class="switch-state"></span>
                                                <?php else: ?>
                                                <input type="checkbox" id="<?php echo e($user->id); ?>" disabled><span class="switch-state"></span>
                                                <?php endif; ?>
                                            <?php endif; ?>

                                        </label>
                                    </td>
                                    <td class="text-center">
                                        <div class="d-none d-md-block">
                                            <?php echo Form::open(array('route'=>array('users.destroy',$user->id),'class'=>'d-flex justify-content-around' ,'method'=>'DELETE','id'=>'user-remove-form-'.$user->id)); ?>

                                            <?php if($loggedUser->can('update-users')): ?>
                                            <?php echo Html::decode(link_to_Route('users.edit','<i class="fa fa-pencil"></i> Edit', $user->id, array('class' => 'm-auto text-dark ', 'type' => 'button'))); ?>

                                            <?php endif; ?>
                                            <?php if($loggedUser->can('view-users')): ?>
                                            <a href="#" class="m-auto text-dark" type="button" id="<?php echo e($user->id); ?>" onclick="viewDetails(this)"><i class="fa fa-eye"></i> View</a>
                                            <?php endif; ?>
                                            <?php if($loggedUser->can('delete-users')): ?>
                                            <a href="#" class="m-auto text-dark" type="button" id="<?php echo e($user->id); ?>" onclick="userDelete(this)"><i class="fa fa-trash"></i> Delete</a>
                                            <?php endif; ?>
                                            <?php echo Form::close(); ?>

                                        </div>
                                        <div class="btn-group d-md-none">
                                            <button class="btn bg-transparent text-dark dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false"></button>
                                            <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                                <?php echo Form::open(array('route'=>array('users.destroy',$user->id),'class'=>'d-flex justify-content-around align-items-center','method'=>'DELETE','id'=>'user-remove-form-'.$user->id)); ?>

                                                <?php if($loggedUser->can('update-users')): ?>
                                                <?php echo Html::decode(link_to_Route('users.edit','<i class="fa fa-pencil"></i>', $user->id, array('class' => 'm-auto text-dark ', 'type' => 'button'))); ?>

                                                <?php endif; ?>
                                                <?php if($loggedUser->can('view-users')): ?>
                                                <a href="#" class="m-auto text-dark" type="button" id="<?php echo e($user->id); ?>" onclick="viewDetails(this)"><i class="fa fa-eye"></i></a>
                                                <?php endif; ?>
                                                <?php if($loggedUser->can('delete-users')): ?>
                                                <a href="#" class="m-auto text-dark" type="button" id="<?php echo e($user->id); ?>" onclick="userDelete(this)"><i class="fa fa-trash"></i></a>
                                                <?php endif; ?>
                                                <?php echo Form::close(); ?>

                                            </ul>
                                        </div>    
                                    </td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php if($loggedUser->can('update-roles')): ?>
<div class="modal fade" id="statusModal" tabindex="-1" role="dialog" aria-labelledby="confirmation" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <div class="row-9" style="padding: 20px">
                    <div class="form-group">
                        <h5 id="switch-title"></h5>
                    </div>
                    <div class="form-group">
                    <p id="status-body"></p>
                    <input type="hidden" id="userUpdate" value="">
                    <input type="hidden" id="userStatus" value="">
                    </div>
                    <div class="form-group pull-right">
                        <button class="btn btn-secondary" type="button" onclick="revertSwitch()">Back</button>
                        <button class="btn btn-primary" type="button" id="proceed-btn" onclick="proceedUpdateStatus()">Proceed</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>
<?php echo $__env->make('modals.confirmation', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('page-script'); ?>

function updateStatus(elem) {

    let isActive = $('#'+elem.id).prop('checked');
    let message = "";
    let title = "";

    $('#status-body').html('');

    if(isActive) {
        title = "Grant user's access?";
        message = "Are you sure you want this user to have access to the system?";
    } else {
        title = "Disable user's access?";
        message = "This user will no longer be able to use this system";
    }

    let newStatus = isActive == true ? 'Active' : 'Inactive';

    $('#userUpdate').val(elem.id);
    $('#userStatus').val(newStatus);
    $('#status-body').html(message);
    $('#switch-title').html(title);

    $('#statusModal').modal({
        backdrop: 'static',
        keyboard: false
    });

    $('#statusModal').modal('show');
}

function proceedUpdateStatus() {

    $.ajax({
        url: '/users/update-status/'+$('#userUpdate').val(),
        method: 'PATCH',
        data: { status: $('#userStatus').val() },
        success: function(result){

            console.log('updated status')
            $('#statusModal').modal('hide');

            $.notify({
                title:'Success!',
                message: 'User status has been updated'
                },
                {
                    type:'primary',
                    allow_dismiss:false,
                    newest_on_top:false ,
                    mouse_over:false,
                    showProgressbar:true,
                    spacing:10,
                    timer:2000,
                    placement:{
                        from:'bottom',
                        align:'right'
                    },
                    offset:{
                        x:30,
                        y:30
                    },
                    delay:1000 ,
                    z_index:10000,
                    animate:{
                        enter:'animated bounce',
                        exit:'animated bounce'
                    }
                });
        }
    });
}

function revertSwitch() {

    let status = $('#userStatus').val() == 'Active' ? false : true;
    $('#'+$('#userUpdate').val()).prop('checked', status);

    $('#statusModal').modal('hide');
}

function userDelete(elem) {

    let userId = elem.value > 0 ? elem.value : elem.id;

    $('#confirmation-title').html("Delete");
    $('#confirmation-body').html("Are you sure you want to delete this user? This process cannot be undone." );

    $('#submit-btn').html('Delete');
    $('#submit-btn').removeClass('btn-primary');
    $('#submit-btn').addClass('btn-danger');
    $('#submit-btn').val(userId);

    $('#detailsModal').modal('hide');

    $('#confirmationModal').modal('show');
}

function submitForm(elem) {
    let = userId = elem.value;
    $('#user-remove-form-'+userId).submit();
}

function viewDetails(elem) {
    window.location.replace('/users/'+elem.id+'/details');
}




<?php echo $__env->make('notifications.index', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /var/www/html/qa/desxpert-admin/resources/views/users/index.blade.php ENDPATH**/ ?>