<?php

use api\classes\API;
use bin\php\Classes\pageNavbar;

if (!defined('APP_INIT')) {
    exit;
}
# IDE Section

# Includes Section
require_once "{$_SERVER['DOCUMENT_ROOT']}/bin/php/Classes/pageNavbar.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API.php";

# Check permissions
$API = new API();
if (!$API->checkPermissions('admin-platforms', 'RO', true)) {
    echo 'error 401 unauthorized';
    exit;
}

# Page functions

# JS Scripts to load for this page

# PageClasses Setup
$pageNavbar = new pageNavbar(false, '<i class="fas fa-layer-group"></i> Platforms');
$pageNavbar->AddHTMLButton('<a href="?add="><button class="btn btn-primary"><i class="fas fa-plus text-success"></i>' . __('add_platform') . '</button></a>');

# Retrieve Information for the page
$data = $GLOBALS['conn']->query("SELECT * FROM autop_platforms");

# Set breadcrumb data

# Start page output
$pageNavbar->outPutNavbar();
?>
<div class="row g-3 align-items-stretch">
    <?php while ($row = $data->fetch_assoc()) { ?>
        <div class="col-md-4 px-2">
            <div class="card card-post card-round h-100">
                <img class="card-img-top img-fluid px-3" src="data:image/png;base64, <?php echo $row['platform_image'] ?>" alt="Card image cap" style="height: 200px; object-fit: scale-down;">
                <div class="card-body">
                    <h3 class="card-title">
                        <?php
                        echo $row['platform_name'];
                        if ($row['platform_enabled']) {
                            echo ' <i class="fas fa-toggle-on text-success"></i>';
                        } else {
                            echo ' <i class="fas fa-toggle-off text-danger"></i>';
                        }
                        ?>
                    </h3>
                    <p class="card-text"><?php echo $row['platform_description'] ?></p>
                </div>
                <div class="card-footer pb-3 end-0 justify-content-end">
                    <?php if ($API->checkPermissions('admin-platforms', 'RW', true)) { ?>
                        <a href="?edit=<?php echo $row['platform_uuid'] ?>" class="btn btn-primary btn-sm">
                            <i class="fas fa-edit"></i> <?php echo __('edit') ?>
                        </a>
                    <?php } ?>
                    <?php if ($API->checkPermissions('admin-platforms', 'RO', true)) { ?>
                        <a href="#" class="btn btn-info btn-sm" data-bs-toggle="modal" data-bs-target="#infoModal<?php echo $row['platform_name'] ?>">
                            <i class="fas fa-info-circle"></i> <?php echo __('info') ?>
                        </a>
                    <?php } ?>
                </div>

                <div class="modal fade" id="infoModal<?php echo $row['platform_name'] ?>" tabindex="-1" aria-labelledby="infoModalLabel" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered modal-lg">
                        <div class="modal-content bg-black2">
                            <div class="modal-header">
                                <h5 class="modal-title" id="infoModalLabel">
                                    <i class="fas fa-info-circle"></i> <?php echo __('information') ?>
                                </h5>
                                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                            </div>
                            <div class="modal-body">
                                <br>
                                <table class="table table-sm table-striped-bg-black">
                                    <tr>
                                        <td><?php echo __('uuid') ?>:</td>
                                        <td><?php echo $row['platform_uuid'] ?></td>
                                    </tr>
                                    <tr>
                                        <td><?php echo __('create_date') ?>:</td>
                                        <td><?php echo showTime($row['platform_create_timestamp']) ?></td>
                                    </tr>
                                    <tr>
                                        <td><?php echo __('last_modified_date') ?>:</td>
                                        <td><?php showTime($row['platform_modified_timestamp']) ?></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php } ?>
</div>