<?php

namespace App\Admin\Controllers\System;

use App\Http\Controllers\Controller;
use App\Models\Category;
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;

class CategoryController 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 Category);

        $grid->id('ID');
        $grid->category_name('名称');
        $grid->alias('别名');
        $grid->demand('需求');
        $grid->order('排序');
        $grid->created_at('添加时间');
        $grid->updated_at('更新时间');
        $grid->actions(function ($actions) use ($grid) {

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

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

        $grid->filter(function ($filter) {
            $data = CategoryGroups::all(['name','alias']);
            foreach ($data as $key => $val) {
                $res[$key]['name'] = $val['name'];
                $res[$key]['alias'] = $val['alias'];
            }
            $filter->equal('alias', '名称')->select(array_column($res, 'name', 'alias'));
        });

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

        return $grid;
    }

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

        $show->id('ID');
        $show->category_name('名称');
        $show->demand('需求');
        $show->alias('别名');
        $show->order('排序');
        $show->created_at('添加时间');
        $show->updated_at('更新时间');

        return $show;
    }

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

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

        return $form;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function editForm($id)
    {
        $form = new Form(new Category);
        $category = Category::find($id);
        $form->text('demand', '需求')->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $form->select('alias', '别名')->options(
            CategoryGroups::List()->pluck('alias', 'alias')
        )->load('category_name', route('admin.sys.cateTrade'))->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $form->select('category_name', '名称')->options(CategoryGroups::where('alias', $category->alias)->pluck('name','name'))->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $form->number('order', '排序')->min(0)->default(0);
        return $form;
    }

    protected function createForm()
    {
        $form = new Form(new Category);
        $form->text('demand', '需求')->rules([
            'required',
        ])->help('月薪分类:1000~1500/月,10000以上/月')->setWidth(3)->setMustMark();
        $form->select('alias', '别名')->options(
            CategoryGroups::List()->pluck('alias', 'alias')
        )->load('category_name', route('admin.sys.cateTrade'))->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $form->select('category_name', '名称')->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $form->number('order', '排序')->min(0)->default(0);
        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);
    }
}