<?php

namespace Modules\Mall\Http\Controllers\Admin\Action\Order;

use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Modules\Mall\Models\Refund;

class RefundReturns extends RowAction
{

    public $name = '退款';

    public function handle(Refund $refund): Response
    {
        try {
            $admin = Admin::user();

            if (!$refund->can('completed')) {
                return $this->response()->error('不可操作')->refresh();
            }

            $res = $refund->setOperator($admin)->returns();

            if ($res === true) {
                $refund->setOperator($admin)->complete();//设置完成

                return $this->response()->success('操作成功')->refresh();
            }

            return $this->response()->error('操作失败')->refresh();
        } catch (\Exception $exception) {
            return $this->response()->error($exception->getMessage())->refresh();
        }

    }

    public function dialog()
    {
        $this->confirm('您确定要打款吗');
    }

}
