<?php

namespace Modules\Linker\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Modules\Linker\Models\Linker;

class IndexController extends AdminController
{

    protected       $title  = '链接管理';

    protected array $status = [
        'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
        'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
    ];

    public function grid(): Grid
    {
        $grid = new Grid(new Linker());

        $grid->column('title', '标题');
        $grid->column('type', '类型')->using(Linker::TYPES);
        $grid->column('url', '目标地址');
        $grid->column('params', '附加参数');
        $grid->column('status', '状态')->switch($this->status);
        $grid->column('created_at', '创建时间');
        $grid->column('updated_at', '更新时间');

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new Linker());
        $form->text('title', '链接标题')->required();
        $form->select('type', '链接类型')
             ->options(Linker::TYPES)
             ->required();
        $form->text('url', '目标地址')->required();
        $form->keyValue('params', '附加参数');
        $form->switch('status', '状态')
             ->states($this->status)
             ->default(1);

        return $form;
    }

}