<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\AppearanceImport;
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
{
    public function title()
    {
        return admin_trans( 'admin.template_list');
    }
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->title())
            ->description('')
            ->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->paginate(config('admin.per_page'));
            $grid->column('id')->sortable();
            // 标题
            $grid->column('title');
            // 文件夹
            $grid->column('folder')->help('Folder names under the directory:/resources/appearance');
            // 封面图
            $grid->column('cover_image')->display(function ($image) {
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            });
            // 排序
            $grid->column('order');
            //是否导入
            $grid->column('imported')->using(config('dictionary.whether'))->label([
                0 => 'default',
                1 => 'success',
            ]);
            //是否启用
            $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', )->select(admin_trans_array(config('dictionary.enabled')))->width(2);
            });
            // 操作
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->append(new AppearanceImport());
            });
            $grid->model()->orderBy('order', 'desc')->orderBy('id', 'desc');
        });
    }

    /**
     * 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('folder');
            $show->field('cover_image')->as(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            })->unescape();
            $show->field('describe');
            $show->field('order');
            $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DistAppearance(), function (Form $form) {
            //$form->display('id');
            $form->text('title');
            $form->text('folder');
            $form->image("cover_image")
                ->retainable()//禁止删OSS图
                ->autoUpload()
                ->uniqueName()
                ->accept(config('admin.upload.oss_image.accept'))
                ->maxSize(config('admin.upload.oss_image.max_size'))
                ->dir(config("admin.upload.directory.image").'/appearance/'.date("Ymd"));//
            $form->textarea('describe');
            $form->number('order')
                ->default(0)
                ->rules('numeric');
            $form->switch('enabled')->default(0)->readOnly();
            $form->saving(function (Form $form) {
                if ($form->isCreating() && $form->input('enabled') == 1) {
                    return $form->response()->error('Please create the template first, then import it, and finally enable it.');
                }
                if (!$form->isCreating()) {
                    if ($form->input('enabled') == 1 && $form->model()->imported == 0) {
                        return $form->response()->error('Please select the import option first, then enable it.');
                    }
                }
            });
            $form->disableDeleteButton();
        });
    }
}