<?php

namespace App\Admin\Controllers;

use App\Models\Banners;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class BannersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Banner')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('轮播图')
            ->description('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->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Banner')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Banners);

        $grid->id('ID')->sortable();
        $grid->name('名称');
        $grid->image('轮播图')->display(function ($value) {
            if($value) return "<image style='max-height: 150px;' src='/uploads/$value' />";
        });
        $grid->url('跳转地址')->link();
        $grid->start_at('开始时间');
        $grid->end_at('结束时间');
        $grid->column('created_at', '创建时间')->hide();
        $grid->column('updated_at', '更新时间')->hide();

        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
//    protected function detail($id)
//    {
//        $show = new Show(Banners::findOrFail($id));
//
//        $show->id('Id');
//        $show->name('Name');
//        $show->image('Image');
//        $show->start_at('Start at');
//        $show->end_at('End at');
//        $show->created_at('Created at');
//        $show->updated_at('Updated at');
//
//        return $show;
//    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Banners);

        $form->text('name', '活动名称')->rules('required');
        $form->image('image', '活动图片')->uniqueName()->rules('required|image');
        $form->url('url', '跳转链接')->default('');


        $form->datetime('start_at', '开始时间')->format('YYYY-MM-DD HH:mm:ss')->rules('required');
        $form->datetime('end_at', '结束时间')->format('YYYY-MM-DD HH:mm:ss')->rules('required');
        $states = [
        		'on' => [
        				'value' => 1,
        				'text' => '开启',
        				'color' => 'success'
        		],
        		'off' => [
        				'value' => 0,
        				'text' => '关闭',
        				'color' => 'danger'
        		]
        ];
        
        $form->switch ( 'status', '状态' )->states ( $states );
        return $form;
    }
}
