<x-app-layout>
  <x-slot name="header">
    <h2 class="font-semibold text-xl text-gray-800 leading-tight">
      <i class="fas fa-users-cog"></i> Ruoli e Permessi
    </h2>
  </x-slot>

  <style>
  thead input{
    width: 100%;
  }
  thead select{
    width: 100%;
  }
  </style>

    <div class="row justify-content-center">
      <div class="col-xxl-4 col-xl-4 mb-4">
        <div class="card h-100">
          <div class="card-body h-100  justify-content-center">
            @if(Session::has('flash_message'))
            <div class="alert alert-success">
              {{ Session::get('flash_message') }}
            </div>
            @endif

            {{$dataTable_role->table()}}

          </div>
        </div>
      </div>

      <div class="col-xxl-8 col-xl-8 mb-4">
        <div class="card h-100">
          <div class="card-body h-100 " id="div_permessi">

          </div>
        </div>
      </div>


    </div>

</x-app-layout>



{{$dataTable_role->scripts()}}
<script>


$(document).ready(function(){
  $.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': '{{csrf_token()}}'
    }
  });



  // Editor Inline
  // $("#dataTable_role").on('dblclick', 'tbody td', function (e) {
  //   if("{{ !Auth::user()->can('edit-roles-permissions') }}") return;
  //   window.LaravelDataTables["dataTable_role-editor"].inline(this, {
  //     onBlur: 'submit',
  //     submit: 'allIfChanged'
  //   });
  // });

  // Editor edit
  $("#dataTable_role").on('click', 'a#editor_edit', function (e) {
    e.preventDefault();
    if("{{ !Auth::user()->can('edit-roles-permissions') }}") return;
    window.LaravelDataTables["dataTable_role-editor"].edit( $(this).closest('tr'), {
      title: 'Modifica',
      buttons: 'Aggiorna'
    });
  });

  // Editor delete
  $("#dataTable_role").on('click', 'a#editor_delete', function (e) {
    e.preventDefault();
    if("{{ !Auth::user()->can('edit-roles-permissions') }}") return;
    window.LaravelDataTables["dataTable_role-editor"].remove( $(this).closest('tr'), {
      title: 'Cancella record',
      message: 'Sei sicuro di voler eliminare il record selezionato?',
      buttons: 'Cancella record'
    } );
  } );


});

function initComplete(){
  $('button').removeClass('dt-button');
  $("#dataTable_role").attr('width', '100%');
}

function load_permessi(role_id){
  $("#div_permessi").load("{!! url('permissions?role_id="+role_id+"') !!}");
}

function load_utenti(role_id){
  $("#div_permessi").load("{!! url('roles/user?role_id="+role_id+"') !!}");
}

function convertToSlug(text){
  return text
  .toLowerCase()
  .replace(/ /g,'-')
  .replace(/[^\w-]+/g,'')
  ;
}


</script>
