@extends('layouts.master')
@section('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>
                @if($loggedUser->can('create-roles'))
                <span class="pull-right">{!! Html::decode(link_to_Route('roles.create', '<i class="fa fa-plus"></i> Create Role', [], ['class' => 'btn btn-primary'])) !!}</span>
                @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>
                                @foreach($roles as $role)
                                <tr>
                                    <td>{!! $role->role !!}</td>
                                    <td style="word-wrap: break-word">{!! $role->description !!}</td>
                                    <td class="text-center">
                                        <div class="d-none d-md-block">
                                            {!! 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)) !!}
                                            @if($loggedUser->can('update-roles'))
                                            {!! 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')))!!}
                                            @endif
                                            @if($loggedUser->can('view-roles'))
                                            <a href="#" class="m-auto text-dark" type="button" id="{{$role->id}}" onclick="viewDetails(this)"><i class="fa fa-eye"></i><span class="d-none d-md-inline"> View</span></a>
                                            @endif
                                            @if($loggedUser->can('delete-roles'))
                                            <a href="#" class="m-auto text-dark" type="button" id="{{$role->id}}" onclick="roleDelete(this)"><i class="fa fa-trash"></i><span class="d-none d-md-inline"> Delete</span></a>
                                            @endif
                                            {!! 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">
                                                {!! 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)) !!}
                                                @if($loggedUser->can('update-roles'))
                                                {!! Html::decode(link_to_Route('roles.edit','<i class="fa fa-pencil"></i>', $role->id, array('class' => 'm-auto text-dark', 'type' => 'button')))!!}
                                                @endif
                                                @if($loggedUser->can('view-roles'))
                                                <a href="#" class="m-auto text-dark" type="button" id="{{$role->id}}" onclick="viewDetails(this)"><i class="fa fa-eye"></i></a>
                                                @endif
                                                @if($loggedUser->can('delete-roles'))
                                                <a href="#" class="m-auto text-dark" type="button" id="{{$role->id}}" onclick="roleDelete(this)"><i class="fa fa-trash"></i></a>
                                                @endif
                                                {!! Form::close() !!}
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                                @endforeach
                            </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">
                        @if($loggedUser->can('update-roles'))
                        <button type="button" class="btn btn-primary" id="detailsEditBtn" onclick="roleEdit(this)">Edit</button>
                        @endif
                        @if($loggedUser->can('delete-roles'))
                        <button type="button" class="btn btn-danger" id="detailsDeleteBtn" onclick="roleDelete(this)">Delete</button>
                        @endif
                        <button type="button" class="btn btn-light" data-bs-dismiss="modal">Close</button>
                    </span>
                </div>
            </div>
        </div>
    </div>
</div>
@include('modals.confirmation')
@endsection
@section('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');
}

@include('notifications.index')


@endsection
