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

use kernel\Flash;
use kernel\widgets\IconBtn\IconBtnActivateWidget;
use kernel\widgets\IconBtn\IconBtnDeactivateWidget;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnUpdateWidget;
use kernel\widgets\IconBtn\IconBtnViewWidget;
use kernel\widgets\ModuleTabsWidget;

$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)) {
        $url = "$url/deactivate/?slug=". $row['slug'];

        return IconBtnDeactivateWidget::create(['url' => $url])->run();
    } else {
        $url = "$url/activate/?slug=". $row['slug'];

        return IconBtnActivateWidget::create(['url' => $url])->run();
    }
});


$table->addAction(function ($row, $url) use ($moduleService) {
    $url = "$url/view/?slug=". $row['slug'];

    return IconBtnViewWidget::create(['url' => $url])->run();
});

$table->addAction(function ($row, $url) use ($moduleService){
    if ($moduleService->isShopModule($row['slug'])){
        if (!$moduleService->isLastVersion($row['slug'])) {
            $url = "$url/update/?slug=". $row['slug'];

            return IconBtnUpdateWidget::create(['url' => $url])->run();
        }
    }

    return false;
});

$table->addAction(function ($row) use ($moduleService){
    $slug = $row['slug'];
    if (!$moduleService->isKernelModule($slug)) {
        $url = "admin/module_shop_client/delete/?slug=" . $row['slug'];

        return IconBtnDeleteWidget::create(['url' => $url])->run();
    }
    return false;
});

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

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