<?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
{
    public function title()
    {
        return admin_trans( 'admin.video_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(BaseVideo::with(['baseVideoCategory']), function (Grid $grid) {
            //默认分页条数
            $grid->paginate(config('admin.per_page'));
            $grid->column('id')->display(function () {
                return $this->_index+1;
            });
            $grid->column('title')->width('25%');
            $grid->column('base_video_category.name',admin_trans_label('category_name'));
            $grid->column('cover_image')->display(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            });
            $grid->column('order');
            $grid->column('enabled')->switch();
            $grid->column('created_at')->sortable();
            $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',admin_trans_label('category'))->select(BaseVideoCategory::selectOptions())->width(2);
                $filter->equal('enabled', admin_trans_label('enabled'))->select(admin_trans_array(config('dictionary.enabled')))->width(2);
            });
            //排序
            $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, BaseVideo::with(['baseVideoCategory']), function (Show $show) {
            $show->field('title');
            $show->field('base_video_category.name',admin_trans_field('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'=>admin_trans_label('video_url'),
                    'content'=>$content
                ]);
            });
            $show->field('video_url',admin_trans_label('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('order');
            $show->field('enabled')->using(admin_trans_array(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');
            $help = admin_trans_label('video_help');
            $inputFormat = admin_trans_label('input_format');
            $form->text('title')->required();
            $form->select('category_id', admin_trans_field('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(config("admin.upload.directory.image").'/video/'.date("Ymd"));//
            $form->text("video_url")
                ->required()
                ->help('<a href="/help/youtube_url/index.html" target="_blank">'.$help. ' </a>, '.$inputFormat.' : https://www.youtube.com/embed/xxxxxxxx');
            $form->editor('remark');
            $form->number('order')
                ->default(0)
                ->rules('numeric');
            $form->switch('enabled')->default(1);
            $form->saving(function (Form $form) {
                //替换youtube URL
                if (strpos($form->video_url, '<iframe') !== false) {
                    // 使用正则表达式提取 src 属性的值
                    if (preg_match('/<iframe[^>]+src="([^"]+)"/', $form->video_url, $matches)) {
                        $src = $matches[1];
                        $form->video_url = $src;
                    }
                }
            });
        });
    }

}