<?php

/**
 * @var \Illuminate\Database\Eloquent\Collection $card
 * @var int $page_number
 * @var \kernel\CgView $view
 */

use kernel\app_modules\card\models\Card;
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\widgets\IconBtn\IconBtnCreateWidget;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnEditWidget;
use kernel\widgets\IconBtn\IconBtnViewWidget;

$view->setTitle("Список шаблонов card");
$view->setMeta([
    'description' => 'Список шаблонов card системы'
]);

//Для использования таблицы с моделью, необходимо создать таблицу в базе данных
$table = new ListEloquentTable(new EloquentDataProvider(\kernel\app_modules\card\models\CardTemplate::class, [
    'currentPage' => $page_number,
    'perPage' => 8,
    'params' => ["class" => "table table-bordered", "border" => "2"],
    'baseUrl' => "/admin/card_template"
]));


//$table = new \Itguild\Tables\ListJsonTable(json_encode(
//    [
//        'meta' => [
//            'total' => 0,
//            'totalWithFilters' => 0,
//            'columns' => [
//                'title',
//                'slug',
//                'status',
//            ],
//            'perPage' => 5,
//            'currentPage' => 1,
//            'baseUrl' => '/admin/some',
//            'params' => [
//                'class' => 'table table-bordered',
//                'border' => 2
//            ]
//        ],
//        'filters' => [],
//        'data' => [],
//    ]
//));

$table->beforePrint(function () {
    return IconBtnCreateWidget::create(['url' => '/admin/card_template/create'])->run();
});

$table->columns([
    'status' => [
        'value' => function ($data) {
            return \kernel\app_modules\card\models\CardTemplate::getStatus()[$data];
        }
    ],
    'path' => [
        'value' => function ($data) {
            return \kernel\helpers\Html::img($data, ['width' => '200px']);
        }
    ]
]);

$table->addAction(function($row) {
    return IconBtnViewWidget::create(['url' => '/admin/card_template/' . $row['id']])->run();
});
$table->addAction(function($row) {
    return IconBtnEditWidget::create(['url' => '/admin/card_template/update/' . $row['id']])->run();
});
$table->addAction(function($row) {
    return IconBtnDeleteWidget::create(['url' => '/admin/card_template/delete/' . $row['id']])->run();
});
$table->create();
$table->render();