<?php

namespace Modules\Configuration\Actions;

use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Exception;
use Illuminate\Http\Request;
use Modules\Configuration\Models\Configuration;

class UpdateType extends RowAction
{

    public $name = '改变类型';

    public function handle(Configuration $model, Request $request): Response
    {
        try {
            $model->type        = $request->type;
            $model->source      = [];
            $model->paramsValue = '';
            $model->save();

            return $this->response()->success('更改成功')->refresh();
        } catch (Exception $e) {
            return $this->response()->error($e->getMessage())->refresh();
        }
    }

    public function form()
    {
        $this->select('type', '配置类型')->options(Configuration::TYPES);
        $this->confirm('确认改变当前类型？(删除配置项与值)');
    }

}
