<?php

namespace App\Admin\Controllers\Episode;

/* 设置内存 保证上传到 OSS */
ini_set('memory_limit', '512M');

use App\Admin\Actions\Grid\BatchEditEpisodeList;
use App\Models\Episode;
use App\Models\EpisodesList;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Http\Controllers\AdminController;

class EpisodesListController extends AdminController
{
    /* @var Episode $episode */
    private $episode;

    protected function title()
    {
        return $this->episode->name;
    }

    protected function content(Content $content, $episodeId = 0)
    {
        $this->episode = Episode::find($episodeId);

        return $content
            ->translation($this->translation())
            ->breadcrumb(
                ['text' => $this->episode->name, 'url' => "/episodes/{$this->episode->id}/lists"],
                ['text' => '剧集上传'],
            )
            ->title($this->title())
            ->description('剧集上传');
    }

    public function index(Content $content, $episodeId = 0)
    {
        return $this->content($content, $episodeId)
            ->body($this->grid());
    }

    public function create(Content $content, $episodeId = 0)
    {
        return $this->content($content, $episodeId)
            ->body($this->form());
    }

    public function update($episodeId, $id = 0)
    {
        $this->episode = Episode::find($episodeId);

        return $this->form()->update($id);
    }

    public function store($episodeId = 0)
    {
        $this->episode = Episode::find($episodeId);

        return $this->form()->store();
    }

    public function edit($episodeId, Content $content, $id = 0)
    {
        return $this->content($content, $episodeId)
            ->body($this->form()->edit($id));
    }

    public function destroy($episodeId, $id = 0)
    {
        $this->episode = Episode::find($episodeId);
        parent::destroy($id);

        return $this->form()->destroy($id);
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(EpisodesList::with(['episode'])->where('episodes_id', $this->episode->id), function (Grid $grid) {
            $grid->model()->orderBy('sort');
            // $grid->column('id');
            $grid->column('sort')->display(function () {
                return '第' . $this->sort . '集';
            })->editable(true);
            $grid->column('episode.name', '剧集名称');
            $grid->column('is_free')
                ->using(config('global.episode_free'))
                ->label(['success', 'primary'])->sortable();
            $grid->column('origin_price')->editable();
            $grid->column('sale_price')->editable();
            $grid->column('url')->display(function () {
                return '<a href="' . $this->url . '">视频链接</a>';
            });
            $url = admin_url('/episodes/batch/' . $this->episode->id . '/upload');

            $grid->tools('<a href="' . $url . '" class="btn btn-primary btn-outline">
<i class="feather icon-upload"></i><span class="d-none d-sm-inline">&nbsp;&nbsp;批量上传</span></a>');

            $grid->hideColumns(['id']);
            $grid->batchActions([new BatchEditEpisodeList()]);
            $grid->disableViewButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new EpisodesList(), function (Form $form) {
            $sort = EpisodesList::where('episodes_id', $this->episode->id)->max('sort');
            $form->hidden('id');
            $form->hidden('episodes_id')->value($this->episode->id);
            $form->number('sort')->default($sort + 1)->min(1);
            $form->radio('is_free')
                ->options(config('global.episode_free'))
                ->when(0, function (Form $form) {
                    $form->number('sale_price')->min(1);
                })->default(1);
            $form->file('url')
                ->chunkSize(4096)
                ->maxSize(1024 * 1024)
                ->saveFullUrl()
                ->mimeTypes('video/*')
                ->uniqueName()->autoUpload()
                ->autoSave(false)
                ->removable(false)
                ->width(4)->required();

            $form->display('created_at');

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