<?php

namespace App\Admin\Actions\User;

use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Callback extends RowAction
{

    public $name = '设置回调地址';

    public function handle(Model $model, Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'callback' => 'required|url',
        ], [
            'callback.required' => '缺少回调地址',
            'callback.url'      => '回调地址不正确，不是有效的url地址',
        ]);

        if ($validator->fails()) {
            return $this->response()->error($validator->errors()->first());
        }

        $model->callback = $request->callback;

        if ($model->save()) {
            return $this->response()->success('设置成功')->refresh();
        }

        return $this->response()->error('设置失败')->refresh();

    }

    public function form(Model $model)
    {
        $this->text('callback', '回调地址')->default($model->callback)->required();
    }

}
