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

$tempoImpiegatoOnSite = Issue::getTempoImpiegato($issue->id, Config::getValue(Config::ATTIVITA_ON_SITE));
$tempoImpiegatoRemota = Issue::getTempoImpiegato($issue->id, Config::getValue(Config::ATTIVITA_REMOTA));
?>

<style>
.divData {
  width: 100%;
  font-weight: bold;
  text-align: center;
  background-color: #DCDCDC;
  border-radius: 5px;
  padding: 5px;
  margin-bottom: 5px;
}
</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> Issue #{{ $issue->id }}
    </h2>
  </x-slot>

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

          <div class="form-row">
            <div class="form-group col-md border mr-2 p-2" >
              {!! Form::label('subject', 'Oggetto') !!}
              {!! $issue->subject !!}
            </div>

            <div class="form-group col-md border mx-2 p-2">
              {!! Form::label('author', 'Autore') !!}
              {!! $author !!}
            </div>

            <div class="form-group col-md border ml-2 p-2">
              {!! Form::label('created_on', 'Data') !!}
              {!! Carbon::parse($issue->created_on)->timezone(config('app.timezone'))->format('d/m/Y H:i') !!}
            </div>
          </div>

          <div class="form-row">
            <div class="form-group col-md border p-2">
              {!! Form::label('description', 'Descrizione della problematica') !!}
              {!! $issue->description !!}
            </div>
          </div>

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

  <div class="row justify-content-center mb-4">
    <div class="col-xl-6 col-md px-md-3 px-2">
      <div class="card">
        <div class="card-header"><label>Interventi di assistenza</label></div>
        <div class="card-body">
          @foreach($issue->journals as $journal)
          @if($journal->notes != '' && !$journal->private_notes)
          <div class="mb-2">
            <div class="divData">
              <i class="fas fa-calendar-day"></i> {{ Carbon::parse($journal->created_on)->timezone(config('app.timezone'))->format('d/m/Y H:i') }}
            </div>
            {!! $journal->notes !!}
          </div>
          @endif
          @endforeach
        </div>
      </div>
    </div>

    <div class="col-xl-3 col-md px-md-3 px-2">
      <div class="card">
        <div class="card-header"><label>Tempo impiegato</label></div>
        <div class="card-body">
          <?php
          $sum_ore_onsite = 0;
          ?>
          <div class="divData">
            On Site
          </div>
          @if($tempoImpiegatoOnSite->count() == 0)
          <i>Nessun tempo registrato</i>
          @else


          <ul>
            @foreach($tempoImpiegatoOnSite as $tempo)
            <li style="list-style-type: disc; margin: unset;">
              <b>{{ Carbon::parse($tempo->spent_on)->format('d/m/Y') }}: </b>{!! $tempo->hours !!}h
              <?php
              $sum_ore_onsite += $tempo->hours;
              ?>
            </li>
            @endforeach
          </ul>
          <b>Totale ore onsite: </b> {{ $sum_ore_onsite }}h
          @endif

          <div class="divData mt-3">
            Remota
          </div>

          <?php
          $sum_ore_remota = 0;
          ?>
          @if($tempoImpiegatoRemota->count() == 0)
          <i>Nessun tempo registrato</i>
          @else



          <ul>
            @foreach($tempoImpiegatoRemota as $tempo)
            <li style="list-style-type: disc; margin: unset;">
              <b>{{ Carbon::parse($tempo->spent_on)->format('d/m/Y') }}: </b>{!! $tempo->hours !!}h
              <?php
              $sum_ore_remota += $tempo->hours;
              ?>
            </li>
            @endforeach
          </ul>
          <b>Totale ore remote: </b> {{ $sum_ore_remota }}h
          @endif
        </div>
      </div>
    </div>

    <div class="col-xl-3 col-md px-md-3 px-2">
      <div class="card">
        <div class="card-header"><label>Allegati</label></div>
        <div class="card-body">
          <ul>
            @foreach($issue->attachments as $attachment)
            <li>
              <a href="{{ route('attachment.download', ['id' => $attachment->id]) }}?filename={{ $attachment->filename }}">{{ $attachment->filename }}</a>
            </li>
            @endforeach
          </ul>
        </div>
      </div>
    </div>
  </div>

</x-app-layout>
