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

$redmineUser = session('redmine_user');
$collectionProject = new Collection();
$index = 0;
$totalProjects = 0;


$response = Http::get(config('redmine.url').'/projects.json', [
  'key' => $redmineUser->api_key,
  'name' => '~Assistenza',
  'limit' => 1
]);

$totalProjects = ceil($response->object()->total_count);

foreach(range(0, $totalProjects) as $index){
  $projectResponseAdmin = Http::get(config('redmine.url').'/projects.json', [
    'key' => $redmineUser->api_key,
    'name' => '~Assistenza',
    'limit' => 100,
    'offset' => 100*$index
  ]);

  foreach($projectResponseAdmin->object()->projects as $project){
    $collectionProject->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-chart-bar"></i> Genera report segnalazioni
    </h2>
  </x-slot>

  {!! Form::open(['route' => 'issue.admin_report', 'id' => 'report_form']) !!}
  <div class="row justify-content-center mb-4">
    <div class="col-xl-6 col-lg-6 col-md px-md-3 px-2">
      <div class="card">
        <div class="card-body">

          @if(Session::has('flash_message'))
          <div class="row justify-content-center mb-4">
            <div class="col">
              <div class="card alert alert-success text-center">
                {{ Session::get('flash_message') }}
              </div>
            </div>
          </div>
          @endif

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


          <div class="form-row">
            <div class="form-group col-md">
              {!! Form::label('data_inizio', 'Data inizio') !!}
              {!! Form::text('data_inizio', Carbon::now()->startOfMonth()->format('d/m/Y'), ['class' => 'form-control']) !!}
            </div>

            <div class="form-group col-md">
              {!! Form::label('data_fine', 'Data fine') !!}
              {!! Form::text('data_fine', Carbon::now()->endOfMonth()->format('d/m/Y'), ['class' => 'form-control']) !!}
            </div>
          </div>

          <div class="form-row">
            <div class="form-group col-md">
              <button type="button" onclick="submitForm()" class="btn btn-primary">Genera</button>
            </div>
          </div>



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

    <div class="col-xl-6 col-lg-6 col-md px-md-3 px-2">
      <div class="card">
        <div class="card-body">
          @foreach($collectionProject as $project)
          {!! Form::checkbox('progetto['.$project->id.']', $project->name, false, ['class' => '']) !!} {{ $project->name }}<br>
          @endforeach
          </div>
          </div>
          </div>
  </div>
  {!! Form::close() !!}



</x-app-layout>

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

  $('#data_inizio').datetimepicker({
    locale: 'it',
    format: 'DD/MM/YYYY',
    // inline: true,
    sideBySide: true,
    icons: {
      time: "fa fa-clock-o",
      date: "fa fa-calendar",
      up: "fa fa-arrow-up",
      down: "fa fa-arrow-down",
      previous: "fa fa-chevron-left",
      next: "fa fa-chevron-right",
      today: "fa fa-clock-o",
      clear: "fa fa-trash-o"
    }
  });

  $('#data_fine').datetimepicker({
    locale: 'it',
    format: 'DD/MM/YYYY',
    // inline: true,
    sideBySide: true,
    icons: {
      time: "fa fa-clock-o",
      date: "fa fa-calendar",
      up: "fa fa-arrow-up",
      down: "fa fa-arrow-down",
      previous: "fa fa-chevron-left",
      next: "fa fa-chevron-right",
      today: "fa fa-clock-o",
      clear: "fa fa-trash-o"
    }
  });

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


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

function submitForm(){
  $('#report_form').submit();
  $("input[name='progetto[]']:checked").each(function (){
    $(this).prop( "checked", false);
  });
}
</script>
