<?php
use App\Models\Issue;
use App\Models\Config;
use Carbon\Carbon;
use Illuminate\Support\Collection;

$progettiUser = json_decode(Auth::user()->progetti);

$redmineUser = session('redmine_user');
$progettiCollection = new Collection();

$projectResponse = Http::get(config('redmine.url').'/projects.json', [
  'key' => $redmineUser->api_key,
  'id' => implode('|', $progettiUser)
]);

// dd($projectResponse->object()->projects);

foreach($projectResponse->object()->projects as $project){
  $progettiCollection->push($project);
}


?>

<style>
</style>

<x-app-layout>
  <x-slot name="header">
    <h2 class="font-semibold text-xl text-gray-800 leading-tight">
      <i class="fas fa-ticket-alt"></i> Nuova segnalazione
    </h2>
  </x-slot>

  <div class="row justify-content-center mb-4">
    <div class="col-xl-10 col-lg-12 col-md px-md-3 px-2">
      <div class="card">
        <div class="card-body">

          <x-auth-validation-errors class="mb-4" :errors="$errors" />

          {!! Form::open(['route' => 'issue.crea', 'files' => true]) !!}

          <div class="form-row">
          <div class="form-group col-md">
            {!! Form::label('tipo_richiesta', 'Tipologia di richiesta') !!}
            {!! Form::select('tipo_richiesta', Issue::tipologiaRichieste(), old('tipo_richiesta'), ['class' => 'form-control']) !!}
          </div>

          <div class="form-group col-md">
            {!! Form::label('referente', 'Referente per la problematica') !!}
            {!! Form::text('referente', old('referente'), ['class' => 'form-control']) !!}
          </div>

          <div class="form-group col-md">
            {!! Form::label('telefono', 'Numero di telefono') !!}
            {!! Form::text('telefono', old('telefono'), ['class' => 'form-control']) !!}
          </div>
        </div>

          <div class="form-row">
            <div class="form-group col-md">
              {!! Form::label('subject', 'Oggetto') !!}
              {!! Form::text('subject', old('subject'), ['class' => 'form-control']) !!}
            </div>
          </div>

          <div class="form-row">
            @if($progettiCollection->count() > 1)
              <div class="form-group col-md-4">
                {!! Form::label('project_id', 'Progetto') !!}
                {!! Form::select('project_id', $progettiCollection->pluck('name', 'id'), old('project_id'), ['class' => 'form-control', 'onchange' => 'load_custom_fields(this)']) !!}
              </div>
            @else
            {!! Form::hidden('project_id', $progettiCollection->first()->id) !!}
            @endif

            <div class="form-group col-md-8">
              <div class="form-row" id="custom_fields">
              </div>
            </div>

          </div>

          <div class="form-row">
            <div class="form-group col-md">
              {!! Form::label('description', 'Descrizione accurata della problematica') !!}
              {!! Form::textarea('description', old('description'), ['class' => 'form-control']) !!}
            </div>
          </div>

          <div class="form-row">
            <div class="form-group col-md">
              {!! Form::label('allegato', 'Allegato (max. 10Mb)') !!}
              {!! Form::file('allegato', ['class' => 'form-control']) !!}
            </div>
          </div>

          <div class="form-row">
            <div class="form-group col-md">
              {!! Form::checkbox('accetto_condizioni', 1, 0, []) !!}
              {!! Form::label('accetto_condizioni', 'Con la presente accetto che la richiesta di supporto verrà fatturata come da vostro contratto attivo.', ['style' => 'display: inline']) !!}
            </div>
          </div>

          <div class="form-row">
            <div class="form-group col-md">
              <button type="submit" class="btn btn-primary">Salva</button>
            </div>
          </div>

          {!! Form::close() !!}

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



</x-app-layout>

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

  load_custom_fields("[name='project_id']");
});


function load_custom_fields(select){
  $('#custom_fields').load("{{ route('issue.load_custom_fields') }}?project_id="+$(select).val());
}
</script>
