<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\DistAppearance;
use App\Libraries\CommonHelper;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;

class DistAppearanceController extends AdminController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header('Appearance')
            ->description('all')
            ->breadcrumb(['text'=>'list','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new DistAppearance(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('cover_image')->display(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            });
            $grid->column('order')->orderable();
            $grid->column('enabled')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            // 过滤器
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('title')->width(2);
                $filter->equal('enabled', 'enabled')->select(config('dictionary.enabled'))->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new DistAppearance(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('cover_image')->as(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            })->unescape();
            $show->field('describe');
            $show->field('enabled')->using(config('dictionary.enabled'));
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DistAppearance(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->image("cover_image")
                ->autoUpload()
                ->uniqueName()
                ->accept(config('admin.upload.oss_image.accept'))
                ->maxSize(config('admin.upload.oss_image.max_size'))
                ->dir('images/appearance/'.date("Ymd"));//
            $form->textarea('describe');
            $form->switch('enabled')->default(1);
        });
    }
}
