<?php

use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use itguild\forms\builders\SelectBuilder;
use kernel\CollectionTableRenderer;

\kernel\App::$hook->add(
    'user_view',
    function (\kernel\modules\user\models\User $user) {
        $userWithStage = \kernel\app_modules\user_stage\models\User::find($user->id);
        $table = new CollectionTableRenderer($userWithStage->stages);

        $table->setColumns([
            'id' => 'ID',
            'title' => 'Название',
        ]);

        $table->addValueProcessor('title', function($value, $model, $field) {
            $style = \kernel\app_modules\user_stage\services\UserStageService::getStageStyle($model);

            return "<div $style>$value</div>";
        });

        $table->addCustomColumn('status', 'Статус', function ($stage){
            $text = \kernel\app_modules\user_stage\services\UserStageService::getStageStatusText($stage);
            $style = \kernel\app_modules\user_stage\services\UserStageService::getStageStyle($stage);

            return "<div $style>$text</div>";
        });

        $table->addFilter('status', SelectBuilder::build('status', [
            'class' => 'form-control',
        ])->setOptions(['Завершен', 'Открыт', 'Закрыт'])->create()->fetch());

        $table->addCustomColumn('action', 'Действия', function ($stage) use ($user){
            $btn = '<a href="/admin/user_stage/set_stage/' . $stage->id . '/2/' . $user->id . '" class="btn btn-sm btn-primary">Открыть</a> ';
            $btn .= '<a href="/admin/user_stage/set_stage/' . $stage->id . '/1/' . $user->id . '" class="btn btn-sm btn-primary">Закрыть</a> ';
            $btn .= '<a href="/admin/user_stage/set_stage/' . $stage->id . '/3/' . $user->id . '" class="btn btn-sm btn-primary">Завершить</a>';

            return $btn;
        });

        return $table->fetch();
    }
);