<?php

namespace App\Admin\Controllers\Order;

use App\Models\Episode;
use App\Models\UserEpisodesRecord;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserEpisodesRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(UserEpisodesRecord::with(['user','episodes']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function () {
                $str  = "";
                $str .= "<div style='margin-right:10px;display: flex;align-items: center'>";
                $str .= '<img data-action="preview-img" src="' . $this->user->avatar . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
                $str .= '<div>';
                $str .= '<p style="margin-bottom: 5px">' . $this->user->nickname . '</p>';
                $str .= '<p style="margin-bottom: 0px">' . $this->user->mobile . '</p>';
                $str .= "</div>";
                $str .= "</div>";
                return $str;
            });
            $grid->column('episodes.name','短剧名称')->label('info');
            $grid->column('list_id')->label('primary');
            $grid->column('price');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('user_id','用户ID')->width(3);
                $filter->like('user.nickname','用户昵称')->width(3);
                $filter->equal('episodes_id','手机号')->width(3);
                $filter->equal('episodes_id')->select(function (){
                    return Episode::select(['id','name'])->get()->pluck('name','id')->toArray();
                })->width(3);
                $filter->equal('user.mobile','手机号')->width(3);
                $filter->between('price')->width(3);
                $filter->between('created_at')->date()->width(4);

            });

            $grid->disableDeleteButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserEpisodesRecord(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('episodes_id');
            $show->field('list_id');
            $show->field('price');
            $show->field('discount');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserEpisodesRecord(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('episodes_id');
            $form->text('list_id');
            $form->text('price');
            $form->text('discount');

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