<!-- 基本信息-->
<template>

  <el-form label-position="left" ref="form" label-width="100px" :model="form" :size="defaultFormItemSize" :rules="rules">
    <el-form-item label="场景:">
      <el-radio-group v-model="form.pluginManageDto.sceneType">
        <el-radio v-for="item in sceneType.getList()" :key="item.id" :label="item.id">{{item.name}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="名称:" prop="pluginManageDto.pluginName">
      <el-input v-model="form.pluginManageDto.pluginName" class="inputWidth" :size="defaultFormItemSize" minlength="2" maxlength="20"></el-input>
      <el-row> <span class="introduce">支持中英文、数字、下划线(_)，2-20个字符，不能以下划线为开头</span></el-row>
    </el-form-item>
    <el-form-item label="描述:">
      <el-input type="textarea" :rows="2" v-model="form.pluginManageDto.remark" class="inputWidth" :size="defaultFormItemSize" minlength="0" maxlength="200"></el-input>
    </el-form-item>
    <el-row type="flex" justify="end" class="dialog-btn-layer mt20">
      <el-button :size="defaultFormItemSize" :plain="true" @click="onCancel(false)">取消</el-button>
      <el-button type="primary" :size="defaultFormItemSize" @click="onSubmit">确定</el-button>
    </el-row>
  </el-form>
</template>
<script>
import { plugOrchestration } from '@/api/gptController.js'
export default {
  data() {
    return {
      taskList: [],
      runNameList: [],
      form: {
        pluginManageDto: {
          pluginName: '',
          remark: '',
          sceneType: 1
        }
      },
      rules: {
        'pluginManageDto.pluginName': [{ required: true, message: '请输入名称', trigger: 'blur' }]
      }
    }
  },
  props: ['isEdit', 'item'],
  components: {},

  computed: {},

  mounted() {
    this.intFrom()
  },
  methods: {
    intFrom() {
      this.form = { ...this.form, ...this.item }
      try {
        this.form.templateLabel = JSON.parse(this.item.templateLabel)
      } catch (error) {
        // console.log(error);
      }
    },
    onCancel(isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess)
      }
    },
    onSubmit() {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let params = {}
            params = { ...this.form }

            if (this.isEdit) {
              plugOrchestration
                .update(this, params)
                .then((res) => {
                  resolve(res)
                  this.$message.success('编辑成功')
                  this.onCancel(true)
                })
                .catch((e) => {
                  reject(e)
                })
            } else {
              plugOrchestration
                .add(this, params)
                .then((res) => {
                  resolve(res)
                  this.$message.success('添加成功')
                  this.onCancel(true)
                })
                .catch((e) => {
                  reject(e)
                })
            }
          } else {
            // reject();
          }
        })
      })
    }
  }
}
</script>
<style scoped>
.inputWidth {
  width: 600px;
}
.title {
  font-size: 20px;
  margin-bottom: 16px;
}
.introduce {
  font-size: 12px;
  color: #909399;
}
</style>
