<?php

namespace App\Admin\Controllers\Platform;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Exception;
use Modules\User\Http\Controllers\Admin\Actions\Pay;
use Modules\User\Http\Controllers\Admin\Actions\Refund;
use Modules\User\Models\Identity;
use Modules\User\Models\Order;

class VipOrderController extends AdminController
{

    protected $title = '体验官退款';

    /**
     * Notes: 升级订单
     *
     * @Author: 玄尘
     * @Date  : 2021/6/7 15:19
     * @return Grid
     */
    public function grid(): Grid
    {
        try {
            trait_exists('Modules\Payment\Traits\WithPayments');
            $grid = new Grid(new Order());
            $grid->model()
                ->whereHas('identity', function ($q) {
                    $q->ty();
                })
                ->whereIn('state',[Order::STATE_SUCCESS,Order::STATE_REFUND])
                ->latest();

            $grid->disableCreateButton();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableDelete();
                $actions->disableView();
                if ($actions->row->canRefund()) {
                    $actions->add(new Refund());
                }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->column(1 / 2, function (Grid\Filter $filter) {
                    $filter->like('user.username', '用户名');
                });

                $filter->column(1 / 2, function (Grid\Filter $filter) {
                    $filter->equal('state', '状态')->select(Order::STATES);
                });

            });

            $grid->column('id', '用户ID');
            $grid->column('升级用户')->display(function () {
                return $this->user->username."({$this->user->info->nickname})";
            });
            $grid->column('identity.name', '身份');
            $grid->column('price', '金额');
            $grid->column('state', '状态')->using(Order::STATES)->label();
            $grid->column('type', '类型')->using(Order::TYPES)->label();
            $grid->column('created_at', '升级时间');

            return $grid;
        } catch (Exception $exception) {
            dd('Payment 模块不存在，无法加载订单数据');
        }
    }

    public function form(): Form
    {
        $form = new Form(new Order());

        $form->decimal('price', '金额')->required();

        return $form;
    }

}
