<?php
session_start();

/** require autoloading to manage namespaces */
require __DIR__ . '/vendor/autoload.php';

use PhotoPrismUpload\API\PhotoPrism;
use PhotoPrismUpload\Entities\Album;

?>
<html>
    <head>
        <title>Collagen Abstimmung</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="vote.js"></script>
        <link href="vote.css" rel="stylesheet">
    </head>
    <body>
<?php

$footer = '</body></html><footer style="position: fixed;bottom: 0;left: 0;">'
.'<a href="/git/phlaym/photoprismupload">Ich bin Open Source</a></footer>';

/** @var array $config configuration options */
$config = require(__DIR__ . '/config.php');

/** @var PhotoPrism $api API object to interface with PhotoPrism */
$api = new PhotoPrism($config);

/** @var Album[] $albums List of PhotoPrism albums */
$albums = [];
try {
    $api->login();
} catch (\Exception $e) {
    die('Fehler: ' . $e->getMessage().$footer.'</body></html>');
}

/** @var string $token Tokens for which album(s) are visible in the dropdown */
$token = $_GET['token'];

/** @var int $page Page number of photos to load */
$page = intval($_GET['page'] ?? 1);

/** @var int $count Number of pictures to load */
$count = intval($_GET['count'] ?? 50);

/** @var int $offset Tokens for which album(s) are visible in the dropdown */
$offset = ($page - 1) * $count;

/** @var string $album_url URL path to the selected album */
$album_url = '/';

$page_sizes = array_values(array_unique([10, 20, 50, 100, $count]));
sort($page_sizes);
$link_class = $page === 1 ? 'pageLink disabled' : 'pageLink';

try {
    $album = $api->getAlbumByToken($token);
    if ($album === null) {
        die('Album nicht gefunden' . $footer . '</body></html>');
    }
    $photos = $api->getAlbumPhotos($album, $count, $offset);
    ?>
    <div>
        <input type="text" placeholder="Name" name="name" id="nameInput" style="margin: 0 0 0 8px">
        <button onclick="loadVotes()">OK</button>
        <a class="<?=$link_class?>" href="vote-gallery.php?token=<?=$token?>&count=<?=$count?>&page=1"><<</a>
        <a class="<?=$link_class?>" href="vote-gallery.php?token=<?=$token?>&count=<?=$count?>&page=<?=$page-1?>"><</a>
        Seite <?=$page?>
        <a class="pageLink" href="vote-gallery.php?token=<?=$token?>&count=<?=$count?>&page=<?=$page+1?>">></a>
        <select>
            <?php
            foreach ($page_sizes as $current_page_size) {
                $selected_string = $current_page_size === $count ? 'selected' : '';
                echo '<option '.$selected_string.'>'.$current_page_size.'</option>';
            }
            ?>
        </select> pro Seite
    </div>
    <?php
    foreach ($photos as $photo) {
        $thumb = $photo->getThumbnailUrl();?>
        <div class="photowrapper">
            <img src="<?= $api->api_url.$thumb?>" id="<?= $photo->uid?>">
            <div class="votewrapper">
                <button value="1" onclick="vote(this)" class="voteButton" data-uid="<?= $photo->uid?>" disabled>
                    👍 Dafür
                </button>
                <button value="0" onclick="vote(this)" class="voteButton" data-uid="<?= $photo->uid?>" disabled>
                    😶 Neutral
                </button>
                <button value="-1" onclick="vote(this)" class="voteButton" data-uid="<?= $photo->uid?>" disabled>
                    👎 Dagegen
                </button>
            </div>
        </div>
        <?php
    }
} catch (\Exception $e) {
    die('Fehler: ' . $footer . $e->getMessage() . '</body></html>');
}
?>
<div>
    <a class="<?=$link_class?>" href="vote-gallery.php?token=<?=$token?>&count=<?=$count?>&page=1"><<</a>
    <a class="<?=$link_class?>" href="vote-gallery.php?token=<?=$token?>&count=<?=$count?>&page=<?=$page-1?>"><</a>
    Seite <?=$page?>
    <a class="pageLink" href="vote-gallery.php?token=<?=$token?>&count=<?=$count?>&page=<?=$page+1?>">></a>
</div>