<?php

namespace App\Admin\Controllers\Program;

use App\Models\Banner;
use App\Models\Episode;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BannerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(Banner::with(['episode']), function (Grid $grid) {
            $grid->column('id')->sortable();
            //            $grid->column('name')->editable();
            $grid->column('image')->image();
            $grid->column('episode_id', '跳转剧集')->display(function () {
                return $this->episode ? "<a href='/admin/episodes/index?name={$this->episode->name}'>{$this->episode->name}</a>" : '';
            });
            $grid->column('sort')->editable()->sortable();
            $grid->column('status')->switch('', true);

            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Banner(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('image');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Banner(), function (Form $form) {
            $form->display('id');
            //            $form->text('name');
            $form->image('image', '轮播图（宽高比:2.7:1）')
                ->saveFullUrl()
                ->uniqueName()->autoUpload()
                ->autoSave(false)
                ->removable(false)
                ->width(4);
            $episode = Episode::select(['id', 'name'])->get()->toArray();
            $form->select('episode_id', '绑定剧集')
                ->options(array_column($episode, 'name', 'id'))
                ->required();
            $form->number('sort');
            $form->switch('status')->default(true);

            $form->disableDeleteButton();
            $form->disableViewButton();
            $form->disableViewCheck();
            $form->disableEditingCheck();
            $form->disableCreatingCheck();
        });
    }
}
