<?php
use App\Models\Permission;
?>
<div class="modal-body">
  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  <div class="text-center mb-6">
    <h4 class="role-title mb-2">Crea nuovo ruolo</h4>
    <p>Seleziona i permessi da associare al ruolo</p>
  </div>
  <!-- Add role form -->
  <form id="addRoleForm" class="row g-6" action="{{ route('role.createRole') }}" method="post">
    @csrf
    <div class="col-12">
      <label class="form-label" for="modalRoleName">Nome del ruolo</label>
      <input type="text" id="modalRoleName" name="modalRoleName" class="form-control" placeholder="Inserisci il nome del ruolo" tabindex="-1" value=""/>
    </div>
    <div class="col-12">
      <h5 class="mb-6">Role Permissions</h5>
      <!-- Permission table -->
      <div class="table-responsive">
        <table class="table table-flush-spacing mb-0">
          <tbody>
            <tr>
              <td class="text-nowrap fw-medium text-heading">Accesso amministratore <i class="bx bx-info-circle" data-bs-toggle="tooltip" data-bs-placement="top" title="Permetti l'accesso a tutte le funzioni del sistema"></i></td>
              <td>
                <div class="d-flex justify-content-end">
                  <div class="form-check mb-0">
                    <input class="form-check-input" type="checkbox" id="selectAll" />
                    <label class="form-check-label" for="selectAll">
                      Seleziona tutto
                    </label>
                  </div>
                </div>
              </td>
            </tr>
            @foreach(Permission::select('group')->distinct()->orderBy('group', 'ASC')->get() as $groupItem)
            <tr>
              <td class="text-nowrap fw-medium text-heading">{{ $groupItem->group }}</td>
              <td>
                <div class="d-flex justify-content-end">
                  @foreach(Permission::where('group', $groupItem->group)->orderBy('name', 'ASC')->get() as $permission)
                  <div class="form-check mb-0 me-4 me-lg-12">
                    <input class="form-check-input" type="checkbox" name="permesso[]" value="{{$permission->id}}"/>
                    <label class="form-check-label" for="userManagementRead">
                      {{$permission->display_name}}
                    </label>
                  </div>
                  @endforeach
                </div>
              </td>
            </tr>
            @endforeach
          </tbody>
        </table>
      </div>
      <!-- Permission table -->
    </div>
    <div class="col-12 text-center">
      <button type="submit" class="btn btn-primary me-3">Salva</button>
      <button type="reset" class="btn btn-label-secondary" data-bs-dismiss="modal" aria-label="Close">Annulla</button>
    </div>
  </form>
  <!--/ Add role form -->
</div>

<script>
$('#selectAll').on('change', function(){
  $('.form-check-input').prop('checked', $(this).is(':checked'));
});
</script>
