<!-- 配置-->
<template>

  <el-form label-position="left" ref="form" label-width="100px" :model="form" :size="defaultFormItemSize" :rules="rules">
    <el-row class="title">模型及参数</el-row>
    <el-form-item label="选择服务:">
      <el-select ref="serveCascader" v-model="form.pluginConfigDto.serviceId" placeholder="请选择">
        <el-option v-for="item in serveList" :key="item.createUserId" :label="item.serviceName" :value="item.versionName">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="选择应用:">
      <el-select ref="serveCascader" v-model="form.pluginConfigDto.apiId" placeholder="请选择">
        <el-option v-for="item in appList" :key="item.apiId" :label="item.name" :value="item.apiId">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="温度:">
      <el-slider :min="0" :max="1" :step="0.1" v-model="form.pluginConfigDto.temperature" show-input>
      </el-slider>
    </el-form-item>
    <el-form-item label="多样性:">
      <el-slider :min="0" :max="1" :step="0.1" v-model="form.pluginConfigDto.diversity" show-input>
      </el-slider>
    </el-form-item>
    <el-form-item label="重复惩罚:">
      <el-slider :min="0" :max="2" :step="0.1" v-model="form.pluginConfigDto.duplicatePenalty" show-input>
      </el-slider>
    </el-form-item>
    <el-form-item label="提示词:">
      <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入提示词" v-model="form.pluginConfigDto.prompt">
      </el-input>
    </el-form-item>
    <el-form-item label="知识库:">
    </el-form-item>
    <el-form-item label="开场白:">
      <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入开场白" v-model="form.pluginConfigDto.openWords">
      </el-input>
    </el-form-item>
    <el-form-item label="最长上下文:">
      <el-slider :min="0" :max="10" :step="1" v-model="form.pluginConfigDto.maxLengthContext" show-input>
      </el-slider>
    </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, ModelInstance } from '@/api/gptController.js'
export default {
  data() {
    return {
      serveList: [],
      appList: [],
      form: {
        pluginConfigDto: {
          apiId: undefined,
          diversity: undefined,
          duplicatePenalty: undefined,
          knowledgeIds: [],
          maxLengthContext: undefined,
          openWords: undefined,
          pluginKnowledgeId: undefined,
          prompt: undefined,
          remark: undefined,
          serviceId: undefined
        }
      },
      rules: {
        'pluginConfigDto.pluginName': [{ required: true, message: '请输入名称', trigger: 'blur' }]
      }
    }
  },
  props: ['isEdit', 'item'],
  components: {},

  computed: {},

  mounted() {
    this.intFrom()
    this.getServeList()
  },
  methods: {
    intFrom() {
      this.form = { ...this.form, ...this.item }
    },
    onCancel(isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess)
      }
    },
    getServeList() {
      ModelInstance.canUseList(this, {})
        .then((res) => {
          this.serveList = res?.data?.dataList
          this.form.serviceId = this.serveList[0].serviceId
        })
        .catch((e) => {
          console.log(e)
        })
    },
    getAppList() {
      ModelInstance.canUseList(this, {})
        .then((res) => {
          this.appList = res?.data?.dataList
          this.form.apiId = this.appList[0].apiId
        })
        .catch((e) => {
          console.log(e)
        })
    },
    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>
