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

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

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

        return \kernel\widgets\IconBtn\IconBtnDeleteWidget::create(['url' => $url])->run();
    }
    else {
        $url = "$url/install/?id=" . $row['id'];

        return \kernel\widgets\IconBtn\IconBtnInstallWidget::create(['url' => $url])->run();
    }
});

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

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

    return false;
});

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

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