<?php

namespace App\Admin\Controllers\Order;

use App\Models\RechargeCombo;
use App\Models\UserRechargeRecord;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserRechargeRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(UserRechargeRecord::with(['user.info', 'combo', 'pay']), function (Grid $grid) {
            $grid->model()->where('status', 1)->orderByDesc('created_at');
            $grid->column('id')->sortable();
            $grid->column('pay_id', '订单号');
            $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('user.info.platform', '所属平台')
                ->using(config('global.platform'))
                ->label([1 => 'primary', 2 => 'success', 3 => 'info']);
            $grid->column('price')->label('danger');
            $grid->column('combo.name', '套餐名称');
            $grid->column('gold')->label('info');
            $grid->column('gift')->label('primary');
            // $grid->column('pay_id');
            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('user.info.platform', '所属平台')
                    ->select(config('global.platform'))
                    ->width(3);
                $filter->between('created_at', '下单时间')->date()->width(4);
                $filter->equal('pay_id', '订单号')->width(4);

                $combos = RechargeCombo::get()->pluck('name', 'id');
                $filter->equal('combo_id')
                    ->select($combos)
                    ->width(3);
            });

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

    /**
     * Make a show builder.
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserRechargeRecord(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('combo_id');
            $show->field('price');
            $show->field('gift');
            $show->field('pay_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserRechargeRecord(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('combo_id');
            $form->text('price');
            $form->text('gift');
            $form->text('pay_id');

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