<?php

namespace App\Admin\Controllers;

use App\Models\Showroom;
use App\Models\ShowroomCase;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ShowroomCaseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(ShowroomCase::with('showroom'), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('showroom_id','展厅名称')->display(function (){
                return $this->showroom->name;
            });
            $grid->column('name');
            $grid->column('videos')->display(function (){
                $html = '';
                foreach ($this->videos as $key => $video){
                    $html .= "<video src='{$video}' width='200' controls>";
                }
                return $html;
            });
            $grid->column('images')->display(function (){
                $html = '';
                foreach ($this->images as $image){
                    $html .= '<img data-action="preview-img" src="'.$image.'"
style="max-width:80px;max-height:80px;cursor:pointer"
class="img img-thumbnail">';
                }
                return $html;
            });
            $grid->column('sort')->editable();
            $grid->column('is_opened')->switch();
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('name')->width(2);
                $filter->equal('showroom_id','展厅')->select(function (){
                    return Showroom::select(['id','name'])->get()->pluck('name','id')->toArray();
                })->width(2);

            });

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

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ShowroomCase(), function (Show $show) {
            $show->field('id');
            $show->field('showroom_id');
            $show->field('name');
            $show->field('videos');
            $show->field('images');
            $show->field('sort');
            $show->field('is_opened');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ShowroomCase(), function (Form $form) {
            $form->display('id');
            $cates = Showroom::select(['id','name'])
                ->where('is_opened',1)->get()->toArray();
            $form->select('showroom_id')
                ->options(array_column($cates,'name','id'))
                ->required();;
            $form->text('name')->required();
            $form->multipleFile('videos')->saveFullUrl()
                ->mimeTypes('video/*')
                ->chunkSize(4096)
                ->maxSize(1024 * 1024)
                ->uniqueName()
                ->autoUpload()
                ->autoSave(false)
                ->removable(false)
                ->width(4);
            $form->multipleImage('images')->saveFullUrl()
                ->uniqueName()->autoUpload()
                ->autoSave(false)
                ->removable(false)
                ->width(4);
            $form->number('sort');
            $form->radio('is_opened')
                ->options(config('global.bool_status'))
                ->default(1);

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

        });
    }
}
