<?php

namespace App\Admin\Controllers;

use App\Models\PaymentConfig;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class PaymentConfigController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PaymentConfig(), function (Grid $grid) {
            $grid->model()->orderByDesc('sort');
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('amount');
            $grid->column('diamond');
            $grid->column('state')->switch();
            $grid->column('is_new','是否新用户专享')->using([0 => '不是新用户专享', 1 => '新用户专享'])
                ->dot(
                    [
                        0 => 'danger',
                        1 => 'success',
                    ],
                    'danger' // 第二个参数为默认值
                );
            $grid->column('sort');
            $grid->column('award_count','推荐用户奖励次数');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['title', 'id'])->placeholder('搜索...');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->between('created_at')->datetime();
            });
            $grid->disableDeleteButton();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PaymentConfig(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('amount');
            $show->field('diamond');
            $show->field('state');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PaymentConfig(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('amount')->rules('numeric')->saving(function ($item){
                return $item * 100;
            });
            $form->text('diamond');
            $form->switch('state');
            $form->select('is_new','是否新用户专享')->options([0 => '不是新用户专享', 1 => '新用户专享']);
            $form->number('sort');
            $form->number('award_count','推荐用户奖励次数')->default(0);
            $form->disableDeleteButton();
            $form->display('created_at');
            $form->display('updated_at');
            $form->disableViewButton();
        });
    }
}
