<?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 Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class RefundAudit extends RowAction
{

    public $name = '审核';

    public function handle(Model $model, Request $request): Response
    {
        $admin = Admin::user();

        $state  = $request->state;
        $remark = $request->remark;
        $res    = false;

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

        if ($state == 'agree') {
            $res = $model->setOperator($admin)->agree($remark);
        }

        if ($state == 'refuse') {
            $res = $model->setOperator($admin)->refuse($remark);
        }

        if ($res === true) {
            return $this->response()->success('操作成功')->refresh();
        }

        return $this->response()->error('操作失败')->refresh();
    }

    public function form(Model $model)
    {
        $this->select('state', '状态')
             ->options([
                 'agree'  => '通过',
                 'refuse' => '驳回',
             ])
             ->required();

        $this->text('remark', '说明')
             ->default($express->number ?? '')
             ->required();
    }

}
