<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SetAddress;
use App\Service\RedisKeyController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class SetAddressController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SetAddress(), function (Grid $grid) {
            // 新增按钮-禁用
            $grid->disableCreateButton();
            // 行操作--禁用
            $grid->disableActions();
            // 行选择器
            $grid->disableRowSelector();
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            $grid->column('id')->sortable();
            $grid->column('address')->editable();
            # $grid->column('created_at');
            # $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

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

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SetAddress(), function (Form $form) {
            $form->display('id');
            $form->text('address');
            $form->display('created_at');
            $form->display('updated_at');
            $form->saved(function (Form $from, $res) {
                # 更新缓存
                Redis::set(RedisKeyController::receipt_account, $from->address);
            });
        });
    }
}
