<?php

namespace Modules\Mall\Http\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Modules\Mall\Events\RefundCompleted;
use Modules\Mall\Http\Controllers\Admin\Action\Order\RefundAudit;
use Modules\Mall\Http\Controllers\Admin\Action\Order\RefundReturns;
use Modules\Mall\Http\Controllers\Admin\Action\Order\RefundSign;
use Modules\Mall\Models\Refund;

class RefundController extends AdminController
{

    protected $title = '退款订单';

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

        $grid->disableCreateButton();

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();

            if ($actions->row->can('agree')) {
                $actions->add(new RefundAudit());
            }

            if ($actions->row->can('sign')) {
                $actions->add(new RefundSign());
            }

            if ($actions->row->can('completed')) {
                $actions->add(new RefundReturns());
            }

        });

        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();

            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->where(function ($query) {
                    $query->whereHas('order', function ($query) {
                        $query->where('order_no', 'like', "%{$this->input}%");
                    });
                }, '订单编号');

                $filter->equal('state', '状态')->select(Refund::STATUS_MAP);

            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->where(function ($query) {
                    $query->whereHas('user', function ($query) {
                        $query->whereHas('info', function ($query) {
                            $query->where('nickname', 'like', "%{$this->input}%");
                        });
                    });
                }, '下单用户');
                $filter->where(function ($query) {
                    $query->whereHas('user', function ($query) {
                        $query->where('username', $this->input);
                    });
                }, '手机号');
            });
        });

        $grid->model()
             ->with(['shop', 'order', 'user.info'])
             ->withCount('versions');

        $grid->column('id');
        $grid->column('shop.name', '所属店铺');
        $grid->column('order.order_no', '订单编号')->display(function () {
            return sprintf('<a href="%s">%s</a>', route('admin.mall.orders.show', $this->order),
                $this->order->order_no);
        });

        $grid->column('user.username', '下单用户');
        $grid->column('refund_total', '退款金额');
        $grid->column('actual_total', '实退金额');

        $grid->column('状态')
             ->display(function () {
                 return $this->status_text;
             })
             ->label();
        $grid->column('remark', '备注');

        $grid->column('versions_count', '操作日志')->link(function () {
            return route('admin.mall.versions', [
                'model' => get_class($this),
                'key'   => $this->id,
            ]);
        }, '_self');

        $grid->column('created_at', '申请时间');

        return $grid;
    }

}