@extends('layouts.app')

@section('title')
     {{ $pageInfo['title'] }}    
@endsection

@section('admin-content')
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>{{ $pageInfo['title'] }}</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">{{ __('Dashboard') }}</a></li>
                        <li class="breadcrumb-item active"><a href="{{ $pageInfo['path'] }}">{{ $pageInfo['title'] }}</a>
                        </li>
                    </ol>
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="header-title float-left">{{ $pageInfo['sub_title'] }}</h4>
                            <p class="float-right mb-2">
                                @if (Auth::user()->can('role.create'))
                                    <a class="btn btn-primary text-white" href="{{ route('admin.roles.create') }}">Create
                                        New Role</a>
                                @endif
                            </p>
                            <div class="clearfix"></div>
                            <div class="dataTables_wrapper dt-bootstrap4">
                                @include('backend.layouts.partials.messages')
                                <div class="">
                                    <table id="dataTable"
                                        class="table table-bordered table-striped table-head-fixed dataTable dtr-inline"
                                        style="width:100%">
                                        <thead class="bg-light text-capitalize">
                                            <tr>
                                                <th width="5%">{{ __('Sl') }}</th>
                                                <th width="10%">{{ __('Role Name') }}</th>
                                                <th width="30%">{{ __('Permission List') }}</th>
                                                <th width="10%">{{ __('Action') }}</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach ($roles as $role)
                                                <tr>
                                                    <td>{{ $loop->index + 1 }}</td>
                                                    <td>{{ $role->name }}</td>
                                                    <td>
                                                        @foreach ($role->permissions as $permission)
                                                            <span class="badge badge-info mr-1">
                                                                {{ $permission->name }}
                                                            </span>
                                                        @endforeach
                                                    </td>
                                                    <td>
                                                        @if (auth()->user()->can('admin.edit'))
                                                            <a class="btn btn-success btn-sm text-white"
                                                                href="{{ route('admin.roles.edit', $role->id) }}">Edit</a>
                                                        @endif
                                                        @if (auth()->user()->can('admin.delete'))
                                                            <a class="btn btn-danger text-white btn-sm" href="javascript:void(0);"
                                                                onclick="event.preventDefault(); if(confirm('Are you sure you want to delete?')) { document.getElementById('delete-form-{{ $role->id }}').submit(); }">
                                                                {{ __('Delete') }}
                                                            </a>

                                                            <form id="delete-form-{{ $role->id }}"
                                                                action="{{ route('admin.roles.destroy', $role->id) }}"
                                                                method="POST" style="display: none;">
                                                                @method('DELETE')
                                                                @csrf
                                                            </form>
                                                        @endif
                                                        <a class="btn btn-primary btn-sm text-white"
                                                            href="{{ route('auth.menurole.index', $role->id) }}">Assign
                                                            Menus</a>

                                                    </td>
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </section>
@endsection

@section('scripts')
    <script>
        $(document).ready(function () {
            if ($('#dataTable').length) {
                $('#dataTable').DataTable({
                    responsive: true,
                    dom: 'Bfrtip',
                    buttons: [
                        'excel', 'pdf', 'print'
                    ]
                });
            }
        });
    </script>
@endsection
