<?php

namespace App\Admin\Controllers\WeChatProgram\UserManagement;

use App\Models\Banner;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class BannerController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '轮播图列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Banner());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('image', __('图片'))->lightbox(['width' =>'', 'height' => 30]);
        $grid->column('type','类型')->using([1=>'热门推荐',2=>'普通推荐']);
        $grid->column('url', __('链接'));
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];
        $grid->column('status','状态')->switch($states);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));
        $grid->filter(function($filter){

            // Remove the default id filter
            $filter->disableIdFilter();

            // Add a column filter
            $type = [''=>'全部'];
            $type = array_merge($type,Banner::$_post_type);
            $filter->equal('type', '类别')->select(Banner::$_post_type);

        });

        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('image', __('图片'))->image('',100,100);
        $show->field('url', __('链接'));
        $show->field('status', __('状态'));
        $show->field('updated_at', __('创建时间'));
        $show->field('created_at', __('更新时间'));

        return $show;
    }

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

        $form->select('type' ,__('分类'))->options(Banner::$_post_type)->default('1');
        $form->image('image', __('图片'))->rules('required' ,['required'=>'请选择图片！']);
        $form->text('url', __('链接'));
        $states = [
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
        ];
        $form->switch('status','状态')->states($states);

        return $form;
    }
}
