<?php
/**
 * @var array $modules_info
 * @var int $module_count
 * @var int $page_number
 * @var int $per_page
 * @var \kernel\services\ModuleService $moduleService
 * @var \kernel\services\KernelService $kernelService
 * @var \kernel\services\AdminThemeService $adminThemeService
 * @var array $filterValues
 */

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

$meta = [];
$meta['columns'] = [
    "name" => "Название",
    "author" => "Автор",
    "version" => "Версия",
    "type" => "Тип",
    "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->columns([
    'type' => [
        'filter' => [
            'class' => \kernel\filters\BootstrapSelectFilter::class,
            'params' => [
                'options' => [
                    'kernel' => 'kernel',
                    'entity' => 'entity',
                ],
                'prompt' => 'Не выбрано'
            ],
            'value' => $filterValues['type'] ?? ''
        ],
    ]
]);

$table->addAction(function ($row, $url) use ($moduleService) {
    if ($row['type'] === 'entity' || $row['type'] === 'additional_property') {
        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();
        }
    }

    return false;
});

$table->addAction(function ($row, $url) use ($moduleService) {
    if ($row['type'] === 'entity' || $row['type'] === 'additional_property') {
        $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;
});

$table->addAction(function ($row, $url) use ($kernelService) {
    if ($row['type'] === 'kernel') {
        if (!$kernelService->isLastVersion()) {
            $url = "$url/kernel/update_form/";

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

    return false;
});

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

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

    return false;
});

$table->afterPrint(function () {
    return \kernel\IGTabel\btn\PrimaryBtn::create('Сбросить все фильтры', '/admin/module_shop_client')->fetch();
});

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

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