<?php

namespace Modules\User\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Modules\User\Models\Account;
use Modules\User\Models\AccountRule;

class RuleController extends AdminController
{

    protected $title = '账变规则管理';

    public function grid(): Grid
    {
        $grid = new Grid(new AccountRule());
        $grid->disableTools();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->column('id', '#ID#');
        $grid->column('title', '规则名称');
        $grid->column('name', '规则关键字');
        $grid->column('type', '规则账户')->using(Account::TYPES);
        $grid->column('variable', '固定额度');
        $grid->column('created_at', '创建时间');
        $grid->column('updated_at', '更新时间');

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new AccountRule());
        $form->text('title', '规则名称')->required();
        if ($form->isCreating()) {
            $form->text('name', '规则关键字')->required();
        } else {
            $form->text('name', '规则关键字')
                 ->readonly()
                 ->required();
        }
        $form->select('type', '规则账户')
             ->options(Account::TYPES)
             ->required();
        $form->number('variable', '固定额度')->default(0);
        $states = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('deductions', '立即扣款')->states($states);
        $form->textarea('remark', '描述')->required();

        return $form;
    }

}