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

use kernel\IGTabel\btn\ActionButton;
use kernel\widgets\ActionButtonWidget;

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

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

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


$table->addAction(function ($row, $url) use ($moduleService) {
    $slug = $row['slug'];
    if ($moduleService->isActive($slug)) {
        return ActionButton::create([
            'btn_type' => "warning",
            'label' => "<i class='fa-regular fa-circle-xmark'></i>",
            'url' => "$url/deactivate/?slug=$slug",
        ]);

    } else {
        ActionButtonWidget::create([
            'label' => "<i class='fa-regular fa-circle-check'></i>",
            'url' => "$url/deactivate/?slug=$slug",
            'btn_type' => "success"
        ])->run();

//        return ActionButton::create([
//            'btn_type' => "success",
//            'label' => "<i class='fa-regular fa-circle-check'></i>",
//            'url' => "$url/deactivate/?slug=$slug",
//        ]);
    }
});

$table->addAction(function ($row, $url) use ($moduleService) {
    $slug = $row['slug'];
//    return "<a class='btn btn-primary' href='$url/view/?slug=$slug' style='margin: 3px; width: 150px;' >Просмотр</a>";

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

$table->addAction(function ($row, $url) use ($moduleService){
    $slug = $row['slug'];
    if (!$moduleService->isKernelModule($slug)){
        if (!$moduleService->isLastVersion($slug)) {
            $label = "Обновить";
            $btn_type = "info";
            return "<a class='btn btn-$btn_type' href='$url/update/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
        }
    }

    return false;
});

$table->addAction(function ($row) use ($moduleService){
    $slug = $row['slug'];
    if (!$moduleService->isKernelModule($slug)){
//        $label = "Удалить";
//        $btn_type = "danger";
//        return "<a class='btn btn-$btn_type' href='admin/module_shop_client/delete/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";

        return ActionButton::create([
            'btn_type' => "danger",
            'label' => "<i class='fa-regular fa-trash-can'></i>",
            'url' => "admin/module_shop_client/delete/?slug=$slug",
        ]);
    }
    return false;
});

if ($moduleService->isActive('module_shop_client')) {
    \kernel\widgets\ModuleTabsWidget::create()->run();
}

$table->create();
$table->render();
