<?php

namespace App\Distributor\Controllers;

use App\Distributor\Repositories\SiteBanner;
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 SiteBannerController extends AdminDistController
{
    public function title()
    {
        return admin_trans('admin.banner_list');
    }
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->title())
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SiteBanner(), function (Grid $grid) {
            $grid->column('id')->display(function () {
                return $this->_index+1;
            });
            $grid->column('title')->width('25%');
            $grid->column('subtitle')->width('25%');
            $grid->column('image_url',admin_trans_label('images'))->display(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            });
            $grid->column('position');
            $grid->column('order');
            $grid->column('show')->switch();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('title')->width(2);
                $filter->equal('position')->select(SiteBanner::getPositionOptions())->width(2);
            });

            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->model()->where('dist_id', getDistributorId())->orderBy('order', 'desc')->orderBy('id', 'desc');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SiteBanner(), function (Form $form) {
            $form->text('title')->required();
            $form->text('subtitle');
            $form->image("image_url", admin_trans_label('images'))
                ->retainable()//禁止删OSS图
                ->autoUpload()
                ->uniqueName()
                ->accept(config('distributor.upload.oss_image.accept'))
                ->maxSize(config('distributor.upload.oss_image.max_size'))
                ->dir(config("distributor.upload.directory.image").'/banner/'.date("Ymd"));
            $form->text('banner_url');
            $form->number('position')->default(1);
            $form->number('order')->default(0);
            $form->switch('show')->default(1);
            $form->hidden('dist_id');
            $form->saving(function (Form $form) {
                $form->dist_id =getDistributorId();//保存时直接写死dist_id
            });
        });
    }
}
