<?php
$checked = json_decode($user->progetti);
?>
<div class="card">
  <div class="card-header">Seleziona i progetti visibili a {{ $user->full_name }}</div>
  <div class="card-body">

    <div class="form-row">
      <div class="form-group col">
        Filtra: {!! Form::text('project_filter', null) !!} <button type="button" onclick="filtra_progetti()" class="btn btn-primary btn-sm"><i class="far fa-save"></i>Filtra</button>
      </div>
    </div>

    {!! Form::open(['method' => 'POST', 'route' => ['user.progetti']]) !!}
    {!! Form::hidden('user_id', $user->id) !!}

    <!-- Prima i progetti con flag -->

    @foreach($progettiUser as $progetto)
    <div class="form-row">
      <div class="form-group col">
        {!! Form::checkbox('progetto[]', $progetto->id, true, ['class' => '']) !!} {{ $progetto->name }}
      </div>
    </div>
    @endforeach

    @foreach($progetti as $progetto)
    <div class="form-row">
      <div class="form-group col">
        {!! Form::checkbox('progetto[]', $progetto->id, false, ['class' => '']) !!} {{ $progetto->name }}
      </div>
    </div>
    @endforeach

    <div class="form-row">
      <div class="form-group col" style="padding-top: 28px;">
        <button type="submit" class="btn btn-primary"><i class="far fa-save"></i>Salva</button>
      </div>
    </div>

    {!! Form::close() !!}
  </div>
</div>

<script>
function filtra_progetti(){
  $('#user_detail').load("{{ route('user.progetti') }}?user_id={{ $user->id }}&filter="+$("[name='project_filter']").val()+"");
}
</script>
