<?php

namespace App\Admin\Controllers\Tool;

use App\Http\Controllers\Controller;
use App\Models\Tpl;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class TplController 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 Tpl);

        $grid->id('ID');

        $grid->name('模板名称');
        $grid->images('图片')->image('', 100, 100);
        $grid->display('是否显示')->display(function ($display) {
            return $display ? "<i class='fa fa-check' style='color:green'></i>" : "<i class='fa fa-close' style='color:red'></i>";
        });
        $grid->tpl_type('类型')->display(function ($tpl_type) {
            switch ($tpl_type) {
                case 1:
                    $tpl_type = '企业';
                    break;
                case 2:
                    $tpl_type = '个人';
                    break;
                default:
                    $tpl_type = '未定义';
                    break;
            }
            return $tpl_type;
        });
        $grid->price('积分');
        $grid->created_at('添加时间');
        $grid->updated_at('更新时间');
        $grid->actions(function ($actions) {
            $actions->disableView();
            if ($actions->row['default']==1) {
                $actions->disableDelete();
            }
        });
        return $grid;
    }

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

        $show->id('ID');
        $show->name('模板名称');
        $show->tpl_type('类型')->as(function ($tpl_type) {
            switch ($tpl_type) {
                case 1:
                    $tpl_type = '企业';
                    break;
                case 2:
                    $tpl_type = '个人';
                    break;
                default:
                    $tpl_type = '未定义';
                    break;
            }
            return $tpl_type;
        });
        $show->display('是否显示')->as(function ($display) {
            return $display ? "显示" : "隐藏";
        });
        $show->images()->image();
        $show->dir('路径');
        $show->created_at('添加时间');
        $show->updated_at('更新时间');

        return $show;
    }

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

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

        return $form;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function editForm()
    {
        $form = new Form(new Tpl);
        $form->display('name', '模板名称')->rules('required');
        $form->display('default', '默认');
        $form->number('price', '售价')->min(0);
        $form->radio('tpl_type', '类型')->options(['1'=>'企业','2'=>'个人'])->default(1);
        $form->radio('display', '是否显示')->options(['1'=>'显示','0'=>'不显示'])->default(0);
        $form->image('images', '图片')->uniqueName();
        $form->text('dir', '路径');
        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        return $form;
    }

    protected function createForm()
    {

        $form = new Form(new Tpl);
        $form->text('name', '模板名称')->rules('required');
        $form->radio('default', '默认')->options([0=>'否'])->default(0);
        $form->number('price', '售价')->min(0);
        $form->radio('tpl_type', '类型')->options(['1'=>'企业','2'=>'个人'])->default(1);
        $form->radio('display', '是否显示')->options(['1'=>'显示','0'=>'不显示'])->default(0);
        $form->image('images', '图片')->uniqueName();
        $form->text('dir', '路径');
        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        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);
    }
}