<?php

namespace App\Admin\Controllers\Order;

use App\Admin\Actions\Grid\UserRechargeRemark;
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->model()->orderByDesc('created_at');
            $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 . '" onerror="this.src=\'https://fourtiao.oss-cn-beijing.aliyuncs.com/zhangsiye/images/6b40343b27263be34cf3212bf44f74c3.png\'" 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('platform', '所属平台')->display(function () {
                return $this->user->info->platform;
            })->using(config('global.platform'))->label([1 => 'primary', 2 => 'success']);
            $grid->column('episodes.name', '短剧名称')->label('info');
            $grid->column('list_id')->label('primary');
            $grid->column('price')->label('danger');
            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new UserRechargeRemark());
            });

            $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')->select(function () {
                    return Episode::select(['id', 'name'])->get()->pluck('name', 'id')->toArray();
                })->width(3);
                $filter->equal('user.mobile', '手机号')->width(3);
                $filter->equal('user.info.platform', '所属平台')->select(config('global.platform'))->width(3);
                $filter->between('price')->width(3);
                $filter->between('created_at')->date()->width(4);
            });

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

    /**
     * Make a show builder.
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, UserEpisodesRecord::with(['user', 'episodes']), function (Show $show) {
            $show->field('id');
            $show->field('user.nickname', '用户昵称');
            $show->field('user.avatar', '用户头像')->image('', 200);
            $show->field('user.mobile', '用户电话');
            $show->field('episodes.name', '短剧名称');
            $show->field('list_id', '第几集')->label('primary');
            $show->field('price');
            $show->field('created_at');

            $show->disableEditButton();
        });
    }

    /**
     * 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');
        });
    }
}
