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

$redmineUser = session('redmine_user');
$project_ids = json_decode(Auth::user()->progetti);

?>

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
@include('testata_pdf')
@include('footer_pdf')
<body>

  <?php
  $timeEntries = Http::get(config('redmine.url').'/time_entries.json', [
    'key' => $redmineUser->api_key,
    'from' => $from,
    'to' => $to,
    'project_id' => $project_id,
    'limit' => 100,
    'cf_'.Config::getValue(Config::CAMPO_FATTURARE) => 'Sì|Fatturato', // Custom fields "fatturare"
  ]);

  $arrayIssues = [];
  try{
    foreach($timeEntries->object()->time_entries as $timeEntry){
      $arrayIssues[] = $timeEntry->issue->id;
    }

  }catch(\Exception $e){
  }
  $arrayIssues = array_unique($arrayIssues);

  $collectionIssue = new Collection();
  foreach($arrayIssues as $issue_id){
    $issueResponse = Http::get(config('redmine.url').'/issues/'.$issue_id.'.json?include=custom_fields', [
      'key' => $redmineUser->api_key,
    ]);

    $issue = $issueResponse->object()->issue;
    $sedeCliente = "";
    try{
      foreach($issue->custom_fields as $customField){
        if(substr($customField->name, 0, 4) == 'Sede'){
          $sedeCliente = explode('(', $customField->value)[0];
          break;
        }else{
          $sedeCliente = "";
        }
      }
    }catch(\Exception $e){
      $sedeCliente = "";
    }

    $collectionIssue->push(['id' => $issue->id, 'subject' => $issue->subject, 'sede_cliente' => $sedeCliente]);
  }

  $collectionIssue = $collectionIssue->sortBy('sede_cliente');

  ?>



  <div style="width: 100%;">
    <div style="width: 100%; padding-top: 0px; height: 100px; margin-bottom: 15px;" >
      <div style="width: 25%; float: left;">
        <img src="{{ public_path('assets/logo_bgw.png') }}" style="height: 100%"/>
      </div>
    </div>

    <div style="width: 100%;">
      <?php
      $p = Http::get(config('redmine.url').'/projects/'.$project_id.'.json', [
        'key' => $redmineUser->api_key,
      ]);
      ?>
      <h1>Report {{ $p->object()->project->name }}</h1>
    </div>

    <div style="width: 100%; margin-bottom: 30px">
      <?php
      $sedeCliente = "";
      $sedeClienteCorrente = null;
      foreach($collectionIssue as $issue){
        $sedeCliente = $issue['sede_cliente'];
        if($sedeClienteCorrente !== $sedeCliente){
          if($sedeClienteCorrente !== null){
            echo "</table>";
          }
          $sedeClienteCorrente = $sedeCliente;
          echo "<table class='table' style='width: 100%; margin-bottom: 40px;'>
          <thead>
          <tr>
          <th style=''>Segnalazione";
          if($sedeCliente != '' && $sedeCliente != null){
            echo "- Sede: ".$sedeCliente;
          }

          echo "</th>
          <th style='width: 60px;'>Data</th>
          <th style='width: 60px;'>On Site</th>
          <th style='width: 60px;'>Remota</th>
          </tr>
          </thead>";
        }

        // get tempi
        $tempoImpiegatoOnSite = Issue::getTempoImpiegato($issue['id'], [Config::getValue(Config::ATTIVITA_ON_SITE), Config::TECNICO_AGGIUNTIVO], $from, $to);
        $tempoImpiegatoRemota = Issue::getTempoImpiegato($issue['id'], Config::getValue(Config::ATTIVITA_REMOTA), $from, $to);

        $index=0;

        // dd($tempoImpiegatoOnSite);
        foreach($tempoImpiegatoOnSite as $tempo){
          echo "<tr>";
          if($index == 0){
            echo "<td rowspan=".($tempoImpiegatoOnSite->count()+$tempoImpiegatoRemota->count()).">#".$issue['id']." - ".$issue['subject']."</td>";
            $index++;
          }
          echo "<td>".Carbon::parse($tempo->spent_on)->format('d/m/Y')."</td>";
          echo "<td>".$tempo->hours."h</td>";
          echo "<td></td>";
          echo "</tr>";
        }

        // $index=0;
        foreach($tempoImpiegatoRemota as $tempo){
          echo "<tr>";
          if($index == 0){
            echo "<td rowspan=".($tempoImpiegatoOnSite->count()+$tempoImpiegatoRemota->count()).">#".$issue['id']." - ".$issue['subject']."</td>";
            $index++;
          }
          echo "<td>".Carbon::parse($tempo->spent_on)->format('d/m/Y')."</td>";
          echo "<td></td>";
          echo "<td>".$tempo->hours."h</td>";
          echo "</tr>";
        }
      }



      ?>
    <!-- </table> -->

  </div>

</div>

<!-- </table> -->


</body>
</html>
