<?php
/**
 * @var array $modules_info
 * @var int $module_count
 * @var int $page_number
 * @var int $per_page
 * @var \kernel\services\ModuleService $moduleService
 */

use Itguild\Tables\ListJsonTable;
use kernel\widgets\ActionButtonWidget;

$meta = [];
$meta['columns'] = [
    "name" => "Название",
    "author" => "Автор",
    "version" => "Версия",
    "description" => "Описание",
    "installations" => "Установки",
    "views" => "Просмотры"
];
$meta['params'] = ["class" => "table table-bordered"];
$meta['perPage'] = $per_page;
$meta['baseUrl'] = "/admin/module_shop_client";
$meta['currentPage'] = $page_number;
$meta['total'] = $module_count;

$info_to_table['meta'] = $meta;
$info_to_table['data'] = $modules_info;

$table = new ListJsonTable(json_encode($info_to_table, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));

$table->addAction(function ($row, $url) use ($moduleService) {
    $id = $row['id'];

    return ActionButtonWidget::create([
        'btn_type' => "primary",
        'label' => "<i class='fa-regular fa-eye'></i>",
        'url' => "$url/view/$id",
    ])->run();
});

$table->addAction(function ($row, $url) use ($moduleService){
    $slug = $row['slug'];
    $id = $row['id'];
    if ($moduleService->isInstall($slug)){
        return ActionButtonWidget::create([
            'btn_type' => "danger",
            'label' => "<i class='fa-regular fa-trash-can'></i>",
            'url' => "$url/delete/?slug=$slug",
        ])->run();
    }
    else {
        return ActionButtonWidget::create([
            'btn_type' => "success",
            'label' => "<i class='fa-solid fa-download'></i>",
            'url' => "$url/install/?id=$id",
        ])->run();
    }
});

$table->addAction(function ($row, $url) use ($moduleService){
    $slug = $row['slug'];
    if ($moduleService->isInstall($slug)){
        if (!$moduleService->isLastVersion($slug)) {
            return ActionButtonWidget::create([
                'btn_type' => "info",
                'label' => "<i class='fa-regular fa-pen-to-square'></i>",
                'url' => "$url/update/?slug=$slug",
            ])->run();
        }
    }

    return false;
});


$table->create();

\kernel\widgets\ModuleTabsWidget::create()->run();
$table->render();
