<?php

require "config.php";

$books = [];

$library = __DIR__ . "/library";

$folders = scandir($library);

foreach ($folders as $folder) {

    if ($folder == "." || $folder == "..") {
        continue;
    }

    if (!is_dir($library . "/" . $folder)) {
        continue;
    }

    $metaFile = $library . "/" . $folder . "/metadata.json";

    if (file_exists($metaFile)) {

        $meta = json_decode(file_get_contents($metaFile), true);

    } else {

        $meta = [
            "title" => ucfirst(str_replace("_", " ", $folder)),
            "category" => "",
            "year" => "",
            "author" => "",
            "description" => ""
        ];

    }

    $meta["id"] = $folder;

    $cover = "library/$folder/cover.webp";

    if (!file_exists(__DIR__ . "/" . $cover)) {

        $cover = "assets/images/no-cover.webp";

    }

    $meta["cover"] = $cover;

    $books[] = $meta;

}

?>
<!DOCTYPE html>

<html lang="id">

<head>

<meta charset="UTF-8">

<title><?= SITE_NAME ?></title>

<link rel="stylesheet" href="assets/css/style.css">

</head>

<body>

<header>

<h1><?= SITE_NAME ?></h1>

<input type="text" id="search" placeholder="Cari Dokumen...">

</header>

<div class="container">

<?php foreach($books as $book){ ?>

<div class="card">

<a href="book/<?=$book['id']?>">

<img src="<?=$book['cover']?>">

<h3><?=$book['title']?></h3>

</a>

</div>

<?php } ?>

</div>

<script src="assets/js/app.js"></script>

</body>

</html>