<?php

namespace Modules\Mall\Http\Controllers\Admin;

use App\Admin\Traits\WithUploads;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Modules\Linker\Traits\WithLinker;
use Modules\Mall\Models\Banner;

class BannerController extends AdminController
{

    use WithUploads,
        WithShop,
        WithLinker;

    public $title = "轮播图管理";

    protected function grid(): Grid
    {
        $grid = new Grid(new Banner);

        $grid->filter(function (Grid\Filter $filter) {
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('title', '轮播标题');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('status', '状态')->radio([
                    0 => '禁用',
                    1 => '正常',
                ]);
            });
        });

        $grid->model()->with(['shop']);

        $grid->column('id', '#ID#');
        $grid->column('shop.name', '所属店铺');
        $grid->column('cover', '封面图片')->image('', 100, 100);
        $grid->column('展示位置')->display(function () {
            $data = [];
            foreach ($this->position as $position) {
                $data[] = $this->position_map[$position];
            }

            return $data;
        })->label();
        $grid->column('title', '轮播标题');
        $grid->column('status', '状态')->bool();
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    protected function form(): Form
    {
        $form = new Form((new Banner)->disableModelCaching());

        $this->shop($form)->help('不选择，默认为平台轮播');
        $form->text('title', '标题')
             ->required();
        $form->multipleSelect('position', '展示位置')
             ->options($form->model()->position_map)
             ->required();
        $this->cover($form);
        $this->withUrl($form);
        $form->switch('status', '显示')->default(1);

        return $form;
    }

}
