<?php
use App\Models\Config;
use App\Models\Issue;

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


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

</style>
<div class="container">

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

          {!! Form::open(['method' => 'POST', 'route' => ['config.save'], 'files' => true]) !!}
          @foreach(Config::groupBy('group')->pluck('group')->toarray() as $group)

          <h2 style="margin-top: 20px;">{{ $group }}</h2>
          @foreach(Config::where([['group', $group], ['azienda_id', session('azienda')->id]])->orderBy('order', 'ASC')->get() as $config)

          <div class="form-row">
            <div class="form-group col">
              {!! Form::hidden('key['.$config->id.']', $config->id) !!}
              {!! Form::label('key['.$config->id.']', $config->display_name) !!}
              @switch($config->type)
              @case('boolean')
              <br>
              {!! Form::radio('value['.$config->id.']', 1, $config->value==true, ['class' => '']) !!} SI
              {!! Form::radio('value['.$config->id.']', 0, $config->value==false, ['class' => '']) !!} NO
              @break

              @case('textarea')
              <br>
              {!! Form::textarea('value['.$config->id.']', $config->value, ['class' => 'form-control']) !!}
              @break

              @case('password')
              {!! Form::password('value['.$config->id.']', ['class' => 'form-control']) !!}
              @break

              @case('array_status')
              <br>
              <?php
              $checked = json_decode($config->value);
              foreach(Issue::getStati() as $status_id => $status_label){
                echo Form::checkbox('value['.$config->id.'][]', $status_id, $checked!=null?in_array($status_id, $checked):false, [])." ".$status_label."<br>";
              }
              ?>
              @break

              @case('select_status')
              {!! Form::select('value['.$config->id.']', Issue::getStati(), $config->value, ['class' => 'form-control']) !!}
              @break

              @case('activity')
              {!! Form::select('value['.$config->id.']', Issue::getAttivita(), $config->value, ['class' => 'form-control']) !!}
              @break

              @case('file')
              <div class="form-row">
                <div class="form-group col">
                  {!! Form::file('value['.$config->id.']', null, ['class' => 'form-control']) !!}
                </div>
                <div class="form-group col">
                  <p>Logo attuale</p>
                  @if($config->value == null)
                  <i>Nessun logo caricato!</i>
                  @else
                  <img src="{{ Storage::url($config->value) }}" style="width: 100%"/>
                  @endif
                </div>
              </div>
              @break

              @default
              {!! Form::text('value['.$config->id.']', $config->value, ['class' => 'form-control']) !!}
              @endswitch
            </div>
          </div>

          @endforeach
          @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>
    </div>

  </div>
</div>


</x-app-layout>

<script>


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

});



</script>
