<?php

namespace Modules\Payment\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Modules\Payment\Models\Payment;

class IndexController extends AdminController
{

    protected $title = '支付订单';

    public function grid(): Grid
    {
        $grid = new Grid(new Payment());

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

        $grid->quickSearch('trade_id')->placeholder('交易单号');

        $grid->filter(function (Grid\Filter $filter) {
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->like('trade_id', '交易单号');
                $filter->like('transaction_id', '交易单号');
                $filter->like('user.username', '下单用户');
                $filter->between('created_at', '下单时间')->datetime();

            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('driver', '支付通道')->select(Payment::DRIVER_MAP);
                $filter->equal('state', '支付状态')->select(Payment::STATUS_MAP);
                $filter->between('paid_at', '支付时间')->datetime();
            });
        });

        $grid->column('trade_id', '交易单号');
        $grid->column('transaction_id', '支付单号');
        $grid->column('user.username', '下单用户');
        $grid->column('driver', '支付通道')
             ->using(Payment::DRIVER_MAP)
             ->label(Payment::DRIVER_LABEL_MAP);
        $grid->column('gateway', '支付渠道');
        $grid->column('state', '支付状态')
             ->using(Payment::STATUS_MAP)
             ->label(Payment::STATUS_LABEL_MAP);
        $grid->column('total', '应付金额');
        $grid->column('paid_at', '支付时间');
        $grid->column('退款状态')->display(function () {
            return $this->refund_status_text;
        })->link(function () {
            return route('admin.payment.refunds.index', ['payment[trade_id]' => $this->trade_id]);
        }, '_self');
        $grid->column('created_at', '创建时间');

        return $grid;
    }

}