<?php

namespace App\Admin\Controllers\Account;

use App\Models\AccountRule;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Validation\Rule;

class RuleController extends AdminController
{

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '账户规则';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AccountRule);
        $grid->tools(function (Grid\Tools $tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        $grid->quickSearch('title')->placeholder('规则名称');

        $grid->filter(function ($filter) {
            $filter->column(1 / 3, function ($filter) {
                $filter->like('title', '规则名称');
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->like('name', '调用标记');
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('type', '账户类型')->select([
                    'cash'  => '现金账户',
                    'score' => '积分账户',
                ]);
            });
        });

        $grid->column('id', '编号');
        $grid->column('title', '规则名称');
        $grid->column('name', '调用标记');
        $grid->column('type', '账户类型')->using(config('account.account_type'));
        $grid->column('variable', '增减变量');
        $grid->column('trigger', '频率')->display(function () {
            return $this->trigger_text;
        });
        $grid->column('deductions', '立即处理')->bool();
        $grid->column('remark', '备注');
        $grid->column('created_at', '创建时间');
        $grid->column('updated_at', '更新时间');

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        $form = new Form(new AccountRule);
        $form->text('title', '规则名称')
            ->rules('required|between:2,50');

        $form->text('name', '调用标记')
            ->creationRules('required|alpha_dash|between:2,50|unique:account_rules')
            ->updateRules('required|alpha_dash|between:2,50|unique:account_rules,name,{{id}}');

        $form->select('type', '账户类型')
            ->options(config('account.account_type'))
            ->rules(['required', Rule::in(array_keys(config('account.account_type')))]);

        $form->number('variable', '增减变量')
            ->value(0)
            ->rules('required|numeric');

        $form->number('trigger', '次数限制')->value(0)
            ->help('执行次数限制，小于0则终身一次，等于0不限制，大于0每日N次')
            ->rules('required|integer');

        $states = [
            'on'  => ['value' => 1, 'text' => '打开'],
            'off' => ['value' => 0, 'text' => '关闭'],
        ];

        $form->switch('deductions', '立即处理')->value(1)->states($states);

        $form->textarea('remark', '备注信息')->rules('nullable|max:255');

        return $form;
    }

}
