<?php

namespace App\Admin\Controllers\Order;

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

class UserConsumeRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(UserConsumeRecord::with(['user.info']), 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('user.info.platform', '所属平台')
                ->using(config('global.platform'))
                ->label([1 => 'primary', 2 => 'success', 3 => 'info']);
            $grid->column('type')
                ->using(config('global.consume_type'))
                ->label(['success', 'info', 'primary']);
            $grid->column('before')->label('info');
            $grid->column('change')->display(function () {
                return $this->change > 0 ? '+' . $this->change : $this->change;
            })->label('danger');
            $grid->column('current')->label('success');
            $grid->column('remark');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('user.nickname', '昵称')->width(3);
                $filter->equal('user.info.platform', '所属平台')->select(config('global.platform'))->width(3);
                $filter->equal('type', '类型')->select(config('global.consume_type'))->width(3);
                $filter->between('created_at', '创建时间')->datetime()->width(3);
            });

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

    /**
     * Make a show builder.
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserConsumeRecord(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('type');
            $show->field('before');
            $show->field('change');
            $show->field('current');
            $show->field('remark');
            $show->field('order_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserConsumeRecord(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('type');
            $form->text('before');
            $form->text('change');
            $form->text('current');
            $form->text('remark');
            $form->text('order_id');

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