@php
$configData = Helper::appClasses();
@endphp

@extends('layouts/layoutMaster')

@section('title', 'Configurazioni notifiche')

@section('pageTitle')
<div class="d-flex flex-column">
  <h4 class="mb-1">Configurazioni notifiche</h4>
</div>
@endsection

@section('content')
@if(session('success'))
<div class="alert alert-success">
  {{ session('success') }}
</div>
@endif

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

<div class="row justify-content-center">
  <div class="col-xxl mb-4 mt-2">
    <div class="card">
      <div class="card-body">
        <form method="POST" action="{{ route('admin.config_notifiche.update') }}">
          @csrf

          @foreach($configList as $event => $meta)
          @php
            $config = $configNotifiche[$event];
            $isAttivo = old("config.$event.is_attivo", $config->is_attivo) ? true : false;
            $selectedUsers = old("config.$event.users", $config->users ?? []);
          @endphp
          <div class="border rounded p-3 mb-3">
            <div class="d-flex align-items-start justify-content-between">
              <div>
                <h6 class="mb-1">{{ $meta['label'] }}</h6>
                <small class="text-muted">{{ $meta['descrizione'] }}</small>
              </div>
              <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox"
                  id="config_{{ $event }}_attivo"
                  name="config[{{ $event }}][is_attivo]"
                  value="1"
                  @checked($isAttivo)
                >
                <label class="form-check-label" for="config_{{ $event }}_attivo">Attiva</label>
              </div>
            </div>

            @if($meta['has_users'])
            <div class="mt-3">
              <label class="form-label">Destinatari</label>
              <div class="border rounded p-2" style="max-height: 260px; overflow-y: auto;">
                @foreach($users as $user)
                <div class="form-check">
                  <input class="form-check-input" type="checkbox"
                    id="config_{{ $event }}_user_{{ $user->id }}"
                    name="config[{{ $event }}][users][]"
                    value="{{ $user->id }}"
                    @checked(in_array($user->id, $selectedUsers ?? []))
                  >
                  <label class="form-check-label" for="config_{{ $event }}_user_{{ $user->id }}">
                    @if($user->is_gruppo)
                    <i class="fas fa-users"></i> {{ $user->label_gruppo }}
                    @else
                    <i class="fas fa-user"></i> {{ $user->full_name }}
                    @endif
                  </label>
                </div>
                @endforeach
              </div>
              <div class="form-text">Seleziona uno o più utenti che riceveranno la notifica.</div>
            </div>
            @else
            <div class="mt-3">
              <small class="text-muted">Destinatari gestiti automaticamente dall'evento.</small>
            </div>
            @endif
          </div>
          @endforeach

          <div class="d-flex justify-content-end">
            <button type="submit" class="btn btn-primary">Salva configurazioni</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
@endsection
