<?php

namespace App\Admin\Controllers\System;

use App\Http\Controllers\Controller;
use App\Models\Hotword;
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 HotwordController 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()->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 Hotword);
        $grid->model()->orderBy('list_order', 'DESC')->orderBy('w_hot', 'DESC');
        $grid->id('ID');
        $grid->w_word('关键词')->width(150);
        $grid->type('搜索类型')->width(150)->display(function () {
            if ($this->type == '2') {
                return '企业搜索';
            } elseif ($this->type == '3') {
                return '个人简历搜索';
            } else {
                return '职位搜索';
            }
        });
        $grid->w_hot('搜索次数')->width(200);
        $grid->list_order('显示顺序')->editable()->width(150);
        $grid->created_at('添加时间');
        $grid->updated_at('更新时间');
        $grid->actions(function ($actions) use ($grid) {
            if (Admin::user()->can('system_hotword_edit')) {
                $actions->disableEdit(false);
            }
            if (Admin::user()->can('system_hotword_delete')) {
                $actions->disableDelete(false);
            }
            $actions->disableView();
        });

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

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

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

        $show->id('ID');
        $show->w_word('关键词');
        $show->type('搜索类型')->as(function ($type) {
            if ($type == 2) {
                return '企业搜索';
            } elseif ($type == 3) {
                return '个人简历搜索';
            } else {
                return '职位搜索';
            }
        });
        $show->w_hot('搜索次数');
        $show->list_order('显示顺序');
        return $show;
    }

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

        $form->display('ID');
        $form->display('Created at');
        $form->display('Updated at');

        return $form;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function editForm()
    {
        $form = new Form(new Hotword);
        $form->text('w_word', '关键词')->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        //搜索类型
        $type_arr = ['1'=>'职位搜索','2'=>'企业搜索','3'=>'个人简历搜索'];
        $form->select('type', '搜索类型')
            ->options($type_arr)
            ->rules('required', array('required'=>'请选择搜索类型。'))
            ->setWidth(3)
            ->setMustMark();
        $form->number('w_hot', '搜索次数')->min(0)->default(0);
        $form->number('list_order', '显示排序')->min(0)->default(0)->help('(越大越靠前)');
        return $form;
    }

    protected function createForm()
    {
        $form = new Form(new Hotword);
        $form->text('w_word', '关键词')->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        //搜索类型
        $type_arr = ['1'=>'职位搜索','2'=>'企业搜索','3'=>'个人简历搜索'];
        $form->select('type', '搜索类型')
             ->options($type_arr)
             ->default(key($type_arr))
             ->rules('required', array('required'=>'请选择搜索类型。'))
             ->setWidth(3)
             ->setMustMark();

        $form->number('w_hot', '搜索次数')->min(0)->default(0);
        $form->number('list_order', '显示排序')->min(0)->default(0)->help('(越大越靠前)');
       
        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()->update($id);
    }
}