<?php

namespace App\Admin\Controllers\Episode;

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()->orderByDesc('sort');
            $grid->column('id')->sortable();
            $grid->column('sort')->editable()->sortable();
            $grid->column('category.name','分类');
            $grid->column('name');
            $grid->column('episodes_price')->editable();
            $grid->column('sale_count','已售出')->display(function (){
                return $this->userEpisodesRecords->count();
            })->label('primary');
            $grid->column('status')->using(['持续更新中','已完结'])->label(['gray', 'primary'])->sortable();
            $grid->column('created_at');

            $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->equal('status')->select(function (){
                    return ['持续更新中','已完结'];
                })->width(2);
                $filter->equal('is_opend')->select(function (){
                    return ['已下架','已上架'];
                })->width(2);
                $filter->between('created_at','添加时间')->datetime()->width(4);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @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(['持续更新中','已完结']);
            $show->field('is_opend')->using(['已下架','已上架']);
            $show->field('sort');
            $show->field('is_vip_watch')->using(['否','是']);
            $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->image('cover_img')->saveFullUrl()
                ->uniqueName()->autoUpload()
                ->autoSave(false)
                ->removable(false)
                ->width(4)->required();
            $form->switch('status')->default(1);
            $form->switch('is_opend')->default(1);
            $form->number('sort');
            $form->switch('is_vip_watch')->default(1);
            $form->number('free_episodes')->required();
            $form->number('paid_episodes')->required();
            $form->number('episodes_price')->required();

            $form->display('created_at');
            $form->display('updated_at');

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