<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\BaseProductCategory;
use App\Admin\Repositories\BaseVideo;
use App\Admin\Repositories\BaseVideoCategory;
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;

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

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(BaseVideo::with(['baseVideoCategory']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('base_video_category.name','Category Name');
            $grid->column('cover_image')->display(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            });
            $grid->column('order')->orderable();
            $grid->column('is_pinned')->switch();
            $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('sku')->width(2);
                $filter->like('title')->width(2);
                $filter->equal('category_id','Category')->select(BaseVideoCategory::selectOptions())->width(2);
                $filter->equal('enabled', 'Enabled')->select(config('dictionary.enabled'))->width(2);
            });
            //排序
            $grid->model()->orderBy("is_pinned",'desc')->orderBy("order",'desc');
            //按钮
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, BaseVideo::with(['baseVideoCategory']), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('base_video_category.name','Category Name');
            $show->field('cover_image')->as(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,150);
            })->unescape();
            $show->html(function () {
                $content = $this->video_url;
                return view('admin::show.field', [
                    'wrapped'=>true,
                    'escape'=>false,
                    'width'=>['label' => '2','field'=>'8'],
                    'label'=>'Video Url',
                    'content'=>$content
                ]);
            });
            $show->field('video_url','Video player')->as(function ($value) {
                $html = '
                <iframe width="560" height="315" src="'.$value.'"
                        title="YouTube video player"
                        frameborder="0"
                        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
                        allowfullscreen>
                 </iframe>';
                return $html;
            })->unescape();
            $show->field('remark')->unescape();
            $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 BaseVideo(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->select('category_id', 'Category Name')
                ->options(BaseVideoCategory::selectOptions())
                ->required();
            $form->image("cover_image")
                ->autoUpload()
                ->uniqueName()
                ->accept(config('admin.upload.oss_image.accept'))
                ->maxSize(config('admin.upload.oss_image.max_size'))
                ->dir('images/video/'.date("Ymd"));//
            $form->url("video_url")->required();
            $form->editor('remark');
            $form->switch('is_pinned')->default(0);
            $form->switch('enabled')->default(1);
        });
    }

}
