<?php

namespace App\Admin\Controllers\System;

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

        $grid->id('ID');

        $grid->badword('关键词')->width(150);
        $grid->replace('替换词')->width(150);
        $states = [
            'on'  => ['value' => 1, 'text' => '开启', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $grid->status('状态')->switch($states);

        $grid->created_at('添加时间');
        $grid->updated_at('更新时间');
        $grid->actions(function ($actions) use ($grid) {
            if (Admin::user()->can('system_badword_edit')) {
                $actions->disableEdit(false);
            }
            if (Admin::user()->can('system_badword_delete')) {
                $actions->disableDelete(false);
            }
        });

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

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

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

        $show->id('ID');
        $show->badword('关键词');
        $show->replace('替换词');
        $show->status('状态')->as(function ($status) {
            if ($status==1) {
                return '开启';
            } else {
                return '关闭';
            }
        });
        return $show;
    }

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

        $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 Badword);
        $form->text('badword', '关键词')->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $form->text('replace', '替换词')->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $states = [
            'on'  => ['value' => 1, 'text' => '开启', 'color' => 'success'],
            'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
        ];
        $form->switch('status', '状态')->states($states);

        $form->saving(function (Form $form) {
            if ($form->status=='on') {
                $form->status=1;
            } elseif ($form->status=='off') {
                $form->status=2;
            }
        });
        return $form;
    }

    protected function createForm()
    {
        $form = new Form(new Badword);
        $form->text('badword', '关键词')->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $form->text('replace', '替换词')->rules([
            'required',
        ])->setWidth(3)->setMustMark();
        $states = [
            'on'  => ['value' => 1, 'text' => '开启', 'color' => 'success'],
            'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
        ];
        $form->switch('status', '状态')->states($states);
        $form->saving(function (Form $form) {
            if ($form->status=='on') {
                $form->status=1;
            } elseif ($form->status=='off') {
                $form->status=2;
            }
        });
        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);
    }
}