<?php $__env->startSection('main-body'); ?>
<div class="container-fluid">
    <div class="page-header">
        <div class="row">
            <div class="col-sm-12">
                <h3>Roles Management</h3>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="/">Dashboard</a></li>
                    <li class="breadcrumb-item active"><a href="/roles">Roles</a></li>
                </ol>
                <?php if($loggedUser->can('create-roles')): ?>
                <span class="pull-right"><?php echo Html::decode(link_to_Route('roles.create', '<i class="fa fa-plus"></i> Create Role', [], ['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="role-list">
                            <thead>
                                <tr>
                                    <th>Role Name</th>
                                    <th>Description</th>
                                    <th class="text-center">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td><?php echo $role->role; ?></td>
                                    <td style="word-wrap: break-word"><?php echo $role->description; ?></td>
                                    <td class="text-center">
                                        <div class="d-none d-md-block">
                                            <?php echo Form::open(array('route'=>array('roles.destroy',$role->id),'class'=>'d-flex justify-content-around align-items-center','method'=>'DELETE','id'=>'role-remove-form-'.$role->id)); ?>

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

                                            <?php endif; ?>
                                            <?php if($loggedUser->can('view-roles')): ?>
                                            <a href="#" class="m-auto text-dark" type="button" id="<?php echo e($role->id); ?>" onclick="viewDetails(this)"><i class="fa fa-eye"></i><span class="d-none d-md-inline"> View</span></a>
                                            <?php endif; ?>
                                            <?php if($loggedUser->can('delete-roles')): ?>
                                            <a href="#" class="m-auto text-dark" type="button" id="<?php echo e($role->id); ?>" onclick="roleDelete(this)"><i class="fa fa-trash"></i><span class="d-none d-md-inline"> Delete</span></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('roles.destroy',$role->id),'class'=>'d-flex justify-content-around align-items-center','method'=>'DELETE','id'=>'role-remove-form-'.$role->id)); ?>

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

                                                <?php endif; ?>
                                                <?php if($loggedUser->can('view-roles')): ?>
                                                <a href="#" class="m-auto text-dark" type="button" id="<?php echo e($role->id); ?>" onclick="viewDetails(this)"><i class="fa fa-eye"></i></a>
                                                <?php endif; ?>
                                                <?php if($loggedUser->can('delete-roles')): ?>
                                                <a href="#" class="m-auto text-dark" type="button" id="<?php echo e($role->id); ?>" onclick="roleDelete(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>
<div class="modal fade" id="detailsModal" tabindex="-1" role="dialog" aria-labelledby="confirmation" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-body" style="padding-left: 50px; padding-top:50px;">
                <div class="row">
                    <h4>Role Details</h4>
                </div>
                <div class="row p-t-30">
                    <div class="col-lg-12">
                        <div class="form-group">
                            <label>Role Name: <span id="role-name-d"></span></label>
                        </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="form-group">
                            <label>Description: <span id="description-d"></span></label>
                        </div>
                    </div>
                    <label>Permissions:</label>
                    <div class="row" id="permission-d">

                    </div>
                </div>
                <div class="form-group m-t-30">
                    <span class="pull-right">
                        <?php if($loggedUser->can('update-roles')): ?>
                        <button type="button" class="btn btn-primary" id="detailsEditBtn" onclick="roleEdit(this)">Edit</button>
                        <?php endif; ?>
                        <?php if($loggedUser->can('delete-roles')): ?>
                        <button type="button" class="btn btn-danger" id="detailsDeleteBtn" onclick="roleDelete(this)">Delete</button>
                        <?php endif; ?>
                        <button type="button" class="btn btn-light" data-bs-dismiss="modal">Close</button>
                    </span>
                </div>
            </div>
        </div>
    </div>
</div>
<?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 roleDelete(elem) {

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

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

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

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

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

function submitForm(elem) {
    let = roleId = elem.value;
    $.ajax({
        url: '/users/'+roleId+'/get-by-role',
        method: 'GET',
        success: function(result){

            if (result.length > 0) {
                $('#info-title').html("Unsuccessful");
                $('#info-body').html("Unable delete role. This role are used by one or more users");

                $('#confirmationModal').modal('hide');
                $('#infoModal').modal('show');
            } else {
                $('#role-remove-form-'+roleId).submit();
            }
        }
    });
}

function viewDetails(elem) {

    $('#permission-d').html(``);
    let modules = ``;
    $.ajax({
        url: '/roles/'+elem.id+'/get-role-details',
        method: 'GET',
        success: function(result){

            $('#role-name-d').html(result.role);
            $('#description-d').html(result.description);

            $('#detailsEditBtn').val(result.id);
            $('#detailsDeleteBtn').val(result.id);

            $.each(result.permissionDetails, function(key, value) {
                let permissions = ``;
                $.each(value, function(key, item) {
                    permissions += `

                            <li>${item.permission}</li>

                    `
                });
                modules += `
                    <div class="col-lg-6">
                        <div class="form-group">
                            <b>${key}</b>
                            <p>${permissions}</p>
                        </div>
                    </div>
                `
            });

            $('#permission-d').html(modules);

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

function roleEdit(elem) {
    window.location.replace('/roles/'+elem.value+'/edit');
}

<?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/roles/index.blade.php ENDPATH**/ ?>