<?php

namespace App\Admin\Controllers;

use App\Models\SystemAvatar;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Redis;

class SystemAvatarController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SystemAvatar(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('img')->image('',80);
            $grid->column('sex')->using([
                0 => trans('user.fields.unknown'),
                1 => trans('user.fields.man'),
                2 => trans('user.fields.woman')
            ])->label([
                'gray',
                'primary',
                'danger'
            ]);
            $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 SystemAvatar(), function (Show $show) {
            $show->field('id');
            $show->field('img')->image('',100);
            $show->select('sex')->options([
                1 => trans('user.fields.man'),
                2 => trans('user.fields.woman')
            ]);
            $show->field('updated_at');
            $show->field('created_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SystemAvatar(), function (Form $form) {
            $form->display('id');
            $form->image('img')->autoUpload()->uniqueName()->saveFullUrl();
            $form->select('sex')->options([
                1 => trans('user.fields.man'),
                2 => trans('user.fields.woman')
            ]);
            $form->saved(function(Form $form){
                Redis::del('system_avatar');
            });
            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
        });
    }
}
