<?php

namespace App\Admin\Controllers\Episode;

use App\Admin\Actions\Grid\BatchEditEpisode;
use App\Models\Episode;
use App\Models\EpisodesCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class EpisodeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(Episode::with(['category', 'userEpisodesRecords']), function (Grid $grid) {
            // $grid->model()->orderByDesc('id');
            $grid->model()->orderBy('sort');
            $grid->column('id')->sortable();
            $grid->column('sort')->editable()->sortable();
            $grid->column('category.name', '分类');
            $grid->column('platform')->display(function () {
                $html     = '';
                $platform = config('global.platform');
                $colors   = [1 => '#21b978', 2 => '#586cb1', 3 => '#3085d6'];
                if (is_array($this->platform)) {
                    foreach ($this->platform as $item) {
                        $html .= '<span class="label" style="background:' . $colors[$item] . '; margin-right: 5px;">' . $platform[$item] . '</span>';
                    }
                } else {
                    $html .= '<span class="label" style="background:' . $colors[$this->platform] . '">' . $platform[$this->platform] . '</span>';
                }

                return $html;
            });
            $grid->column('name')->editable();
            $grid->column('episodes_price')->editable();
            $grid->column('sale_count', '已售出')->display(function () {
                return $this->userEpisodesRecords->count();
            })->label('primary');
            $grid->column('status')->using(config('global.episode_status'))->label(['gray', 'primary'])->sortable();
            $grid->column('is_opend')->using(config('global.episode_opend'))->label(['gray', 'primary'])->sortable();
            $grid->column('is_vip_watch')->using(config('global.bool_status'))->label(['gray', 'primary'])->sortable();
            $grid->column('', '短剧上传')->display(function () {
                $url = admin_url('/episodes/batch/' . $this->id . '/upload');

                return '<a href="' . $url . '"><i class="fa fa-upload"></i> 上传剧集</a>';
            });
            $grid->column('created_at');

            $grid->column('剧集管理', '规格')->display(function () {
                $url = '/admin/episodes/' . $this->id . '/lists';

                return "<a href='$url'>剧集管理</a>";
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('name')->width(2);
                $filter->equal('category_id')->select(function () {
                    return EpisodesCategory::select(['id', 'name'])->get()->pluck('name', 'id')->toArray();
                })->width(2);
                $filter->like('platform')->select(config('global.platform'))->width(2);
                $filter->equal('status')->select(function () {
                    return ['持续更新中', '已完结'];
                })->width(2);
                $filter->equal('is_opend')->select(config('global.episode_opend'))->width(2);
                $filter->between('created_at', '添加时间')->datetime()->width(4);
            });

            $grid->batchActions([new BatchEditEpisode()]);

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

    /**
     * Make a show builder.
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, Episode::with(['category', 'userEpisodesRecords']), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('category.name', '分类');
            $show->field('cover_img')->image();
            $show->field('status')->using(config('global.episode_status'));
            $show->field('is_opend')->using(config('global.episode_opend'));
            $show->field('sort');
            $show->field('is_vip_watch')->using(config('global.bool_status'));
            $show->field('free_episodes');
            $show->field('paid_episodes');
            $show->field('episodes_price');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Episode(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $categoryModel = app(\App\Models\EpisodesCategory::class);
            $cates         = $categoryModel::select(['id', 'name'])->get()->toArray();
            $form->select('category_id')
                ->options(array_column($cates, 'name', 'id'))
                ->required();

            $form->checkbox('platform')
                ->options(config('global.platform'))
                ->default(1);

            $form->image('cover_img', '封面图(宽高比: 0.68:1)')->saveFullUrl()
                ->uniqueName()->autoUpload()
                ->autoSave(false)
                ->removable(false)
                ->width(4)->required();
            $form->number('sort');
            $form->radio('status')->options(config('global.episode_status'))->default(1);
            $form->switch('is_opend')->default(1);
            $form->switch('is_vip_watch')->default(1);
            //            $form->number('free_episodes')->required();
            //            $form->number('paid_episodes')->required();
            $form->number('episodes_price')->required();
            $form->number('favorite_num');
            $form->number('collect_num');
            $form->number('buy_num');
            $form->number('play_num');
            $form->number('virtual_share');

            $form->saved(function (Form $form) {
                if ($form->isCreating()) {
                    return $form
                        ->response()
                        ->success(trans('admin.save_succeeded'))
                        ->redirect("/episodes/{$form->getKey()}/lists/create");
                }
            });
            //            $form->display('created_at');
            //            $form->display('updated_at');

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