
@extends('layout.master')

@push('plugin-styles')
@endpush

@section('content')
<div class="row">
    <div class="col s12">
        @include('componentes._breadcrumbs')
    </div>    
    
    <div class="col-lg-12 grid-margin stretch-card">
        <div class="card">
            @can('permission-create')
            
                <div class="card-body">
                
                
                    <h4 class="card-title">Adicionar Perfil/Regra</h4>
                    <p class="card-description"> </p>

                    @if ($errors->any())
                        <div class="alert alert-danger">
                            <ul>
                                @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div><br />
                    @endif

                    <div class="auto-form-wrapper">
                        <form action="{{route('rules.permission.store', $rules->id)}}" method="post">
                            {{ csrf_field() }}

                            <div class="row clearfix">  
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <select name="permission_id" id="permission_id" class="form-control">
                                            @php
                                                $module = ''; $first = 1;   
                                            @endphp 
                                            @if ($permissions)
                                                @foreach($permissions as $valor)
                                                    @php
                                                        if ($module != $valor->module) :
                                                        
                                                            $module = $valor->module;
                                                    
                                                            if($first != 1) :
                                                                echo '</optgroup>';
                                                            endif;
                                                            $first = 0;
                                                    @endphp                                                    
                                                        <optgroup label="{{$valor->module}}">
                                                    @php
                                                        endif;
                                                    @endphp
                                                    <option value="{{$valor->id}}">{{$valor->label}}</option>
                                                    
                                                @endforeach
                                                </optgroup>
                                            @endif    
                                            </select>
                                        </div>
                                    </div>
                                </div>  
                            </div>
                            <div class="row clearfix">  
                                <div class="col-sm-2">
                                    <button class="btn btn-primary submit-btn btn-block">Adicionar</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            @endcan    
            @can('permission-view')                                            
                <div class="card-body">
                    <h4 class="card-title">Lista de Permissões para: <span class="text-primary">{{$rules->name}}</span></h4>
                    <p class="card-description"> </p>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Módulo</th>
                                    <th>Nome</th>
                                    <th>Descrição</th>
                                    <th>Ação</th>
                                </tr>
                            </thead>
                            
                            <tbody>
                                @if (count($rules->permissions) > 0)
                                    @foreach($rules->permissions as $permission)
                                    <tr>
                                        <td>{{$permission->module}}</td>
                                        <td>{{$permission->name}}</td>
                                        <td>{{$permission->description}}</td>
                                        <td>
                                            @can('permission-delete')
                                                <form action="{{route('rules.permission.destroy', [$rules->id, $permission->id])}}" method="post">
                                                    {{ method_field('DELETE')}}
                                                    {{csrf_field()}}
                                                    <button title="Deletar" class="btn btn-icons btn-inverse-danger" href="#"><i class="mdi mdi-delete"></i></button>
                                                </form>
                                            @endcan
                                        </td>
                                    </tr>
                                    @endforeach
                                @else 
                                <tr>
                                    <td colspan="6">Nenhum registro encontrado.</td>
                                </tr>
                                @endif
                            </tbody>
                        </table>
                    </div>
                </div>
            @endcan
        </div>
    </div>
  
</div>
@endsection

@push('plugin-scripts')
@endpush

@push('custom-scripts')
@endpush