<?php

namespace App\Admin\Controllers\System;

use App\Http\Controllers\Controller;
use App\Models\CategoryGroups;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class CategoryGroupsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('其它分类')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('其它分类')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('其它分类')
            ->description('编辑')
            ->body($this->editForm($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('其它分类')
            ->description('创建')
            ->body($this->createForm());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CategoryGroups);

        $grid->id('ID');
        $grid->name('名称')->display(function () {
            return "<a href='".route('admin.sys.otherList.index',['alias'=>$this->alias])."'>$this->name</a>";
        })->width(200);
        $grid->alias('别名')->width(200);
        $states = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '不是', 'color' => 'default'],
        ];
        $grid->sys('是否系统分组')->switch($states);
        $grid->created_at('添加时间');
        $grid->updated_at('更新时间');
        $grid->actions(function ($actions) use ($grid) {

            if (Admin::user()->can('system_type_other_type_edit')) {
                $actions->disableEdit(false);
            }
            if (Admin::user()->can('system_type_other_type_delete')) {
                $actions->disableDelete(false);
            }
        });

        if (Admin::user()->can('system_type_other_type_delete')) {
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete(false);
                });
            });
            $grid->disableRowSelector(false);
        }


        if (Admin::user()->can('system_type_other_type_create')) {
            $grid->disableCreateButton(false);
        }

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CategoryGroups::findOrFail($id));

        $show->id('ID');

        $show->name('名称');
        $show->alias('别名');
        $show->sys('系统分组')->as(function ($sys) {
            return $sys ? '是' : '不是';
        });

        $show->created_at('添加时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CategoryGroups);

        $form->display('ID');
        $form->display('添加时间');
        $form->display('更新时间');

        return $form;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function editForm($id)
    {
        $form = new Form(new CategoryGroups);
        $form->text('name', '名称')->rules([
            'required',
        ])->setWidth(3);
        $form->text('alias', '别名')->rules([
            'required',
            Rule::unique('category_groups')->ignore($id),
        ])->setWidth(3);
        $form->radio('sys', '系统分组')->options([0=>'不是',1=>'是']);
        $form->saving(function (Form $form) {
            if ($form->sys=='on') {
                $form->sys=1;
            } elseif ($form->sys=='off') {
                $form->sys=0;
            }
        });
        return $form;
    }

    protected function createForm()
    {
        $form = new Form(new CategoryGroups);
        $form->text('name', '名称')->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $form->text('alias', '别名')->rules([
            'required',
            'unique:category_groups',
        ])->setWidth(3)->setMustMark();
        $form->radio('sys', '系统分组')->options([0=>'不是',1=>'是'])->default(1);

        return $form;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return mixed
     */
    public function store()
    {
        return $this->createForm()->store();
    }
    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
     */
    public function update($id)
    {
        return $this->editForm($id)->update($id);
    }
    public function tradeList(Request $request)
    {
        $q = $request->get('q');
        return CategoryGroups::where('alias', $q)->get(['name','name as text']);

    }
}