<?php

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

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

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

# Check permissions
$API = new API();
if (!$API->checkPermissions('admin-vendors', '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-microchip"></i> ' . __('vendors'));
$pageNavbar->AddHTMLButton('<a href="?add="><button class="btn btn-primary"><i class="fas fa-plus text-success"></i>' . __('add_vendor') . '</button></a>');

# Retrieve Information for the page
$API_vendors = new API_vendors();
$vendors_data = $API_vendors->getVendors();

# Set breadcrumb data

# Start page output
$pageNavbar->outPutNavbar();

?>
<div class="row g-3 align-items-stretch">
    <?php
    foreach ($vendors_data as $vendor_data) { ?>
        <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 $vendor_data['vendor_image'] ?>" alt="Card image cap" style="height: 200px; object-fit: scale-down;">
                <div class="card-body">
                    <h3 class="card-title">
                        <?php
                        echo $vendor_data['vendor_name'];
                        if ($vendor_data['vendor_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 $vendor_data['vendor_description'] ?></p>
                </div>
                <div class="card-footer pb-3 end-0 justify-content-end">
                    <?php if ($API->checkPermissions('admin-vendors', 'RW', true)) { ?>
                        <a href="?edit=<?php echo $vendor_data['vendor_uuid'] ?>" class="btn btn-primary btn-sm">
                            <i class="fas fa-edit"></i> <?php echo __('edit') ?>
                        </a>
                    <?php } ?>
                    <?php if ($API->checkPermissions('admin-vendors', 'RO', true)) { ?>
                        <a href="#" class="btn btn-info btn-sm" data-bs-toggle="modal" data-bs-target="#infoModal<?php echo $vendor_data['vendor_name'] ?>">
                            <i class="fas fa-info-circle"></i> <?php echo __('info') ?>
                        </a>
                    <?php } ?>
                </div>

                <div class="modal fade" id="infoModal<?php echo $vendor_data['vendor_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 $vendor_data['vendor_uuid'] ?></td>
                                    </tr>
                                    <tr>
                                        <td><?php echo __('create_date') ?>:</td>
                                        <td><?php showTime($vendor_data['vendor_create_timestamp']); ?></td>
                                    </tr>
                                    <tr>
                                        <td><?php echo __('last_modified_date') ?>:</td>
                                        <td><?php showTime($vendor_data['vendor_modified_timestamp']); ?></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php } ?>
</div>