<?php
error_reporting(E_ALL);
require('../config.php');

require('../CrewbrainApi.php');

use chillerlan\QRCode\{
    QRCode,
    QROptions
};

require('../vendor/autoload.php');

$options = new QROptions([
    'imageBase64' => false,
    'qrCodeHeight' => 75,
    'qrCodeWidth' => 75,
    'version' => -1,
    'quietzoneSize' => 1
        ]);


$CrewBrain = new CrewBrain();



$result = $CrewBrain->requestCrewBrainApi('api/tasklist/' . CrewBrain_TaskListID . '/tasks');


$data_output = json_decode($result);


foreach ($data_output as $aufgabe) {



    if ($aufgabe->Geloescht == null) {

        $AufgabeDetail = json_decode($CrewBrain->requestCrewBrainApi('api/task/'.$aufgabe->ID));
        
        if ($aufgabe->Zieldatum == null) {
            echo "<tr>";
        } else {

            $date = new DateTime($aufgabe->Zieldatum);
            $date->setTime(0, 0, 0);
            $today = new DateTime();
            $today->setTime(0, 0, 0);
            if ($date == $today) {

                echo "<tr class='text-dark bg-warning'>";
            } else if ($date < $today) {
                echo "<tr class=' bg-danger'>";
            } else {
                echo "<tr>";
            }
        }
        if (Enable_QR_Code_CrewBrainAufgaben) {
            echo "<td>" . '<div style="width: 5vb;">' . (new QRCode($options))->render(CrewBrain_Connectionprotocol . '://' . CrewBrain_Server . '/aufgaben/' . CrewBrain_TaskListID . '/aufgabe/' . $aufgabe->ID) . "</div></td>";
        } else {
            echo "<td>" . $aufgabe->ID . "</td>";
        }
        echo "<td>";
      
      $i=0;
        foreach($AufgabeDetail->Responsibles as $bearbeiter){
            echo $bearbeiter->Name;
            if($i>0){
                echo ", ";
            }
            $i++;
        }
       echo "</td>";
        echo "<td>" . $aufgabe->Titel . "</td>";
        if ($aufgabe->Zieldatum != null) {
            echo "<td>" . date_format(new \DateTime($aufgabe->Zieldatum), 'd.m.Y') . "</td>";
        } else {
            echo "<td>nA</td>";
        }


        switch ($aufgabe->Prioritaet) {
            case -1:
                echo '<td> <span class="badge badge-primary">Niedrig</span></td>';
                break;
            case 0:
                echo '<td> <span class="badge badge-success">Normal</span></td>';
                break;
            case 1:
                echo '<td> <span class="badge badge-warning">Höher</span></td>';
                break;
            case 2:
                echo '<td> <span class="badge badge-danger">Hoch</span></td>';
                break;
        }
        echo "</tr>";
    }
}

function getTimeFromSeconds(string $timestring) {

    $hours = floor($timestring / 3600);
    $mins = floor($timestring / 60 % 60);
    $secs = floor($timestring % 60);

    $timeFormat = sprintf('%02d:%02d', $hours, $mins);

    return $timeFormat;
}

?>