<!-- 创建Prompt模板 -->
<template>
  <el-form label-position="left" ref="form" label-width="80px" :model="form" :size="defaultFormItemSize" :rules="rules">
    <el-form-item label="模板名称:" prop="templateName">
      <el-input v-model="form.templateName" class="inputWidth" :size="defaultFormItemSize" ></el-input>
    </el-form-item>
    <el-form-item label="模板标签:">
      <el-select :size="defaultFormItemSize"  class="inputWidth" v-model="form.templateLabel" multiple :filterable='true' :allow-create='true' default-first-option placeholder="请选择模板标签">
        <el-option v-for="item in TemplateLabelDict.getList()" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="场景类型:" >
      <el-radio v-model="form.scenarioType" v-for="item in ScenarioTypeDict.getList()" :label="item.id" :size="defaultFormItemSize" :key="item.id" @input="changeScenarioType">{{ item.name }}</el-radio>
    </el-form-item>
    <el-form-item label="变量识别符:">
      <el-select v-model="form.parameterFormat" placeholder="请选择" :size="defaultFormItemSize" class="inputWidth">
        <el-option v-for="item in ParameterFormatLabelDict.getList()" :key="item.id" :label="item.name" :value="item.symbol">
        </el-option>
      </el-select>
    </el-form-item>
    <template v-if="form.scenarioType === 0 ">
      <el-form-item label="模板框架:" class="templateFramework" v-if=" !isEdit">
        
        <el-tooltip class="item" effect="dark" :content="item.name" placement="top"  v-for="item in TemplateFrameworkDict.getList()" :label="item.id" :size="defaultFormItemSize" :key="item.id">
          <el-radio v-model="form.templateFramework" @input="$refs.promptInput.init()" :label="item.id">{{ item.symbol }}</el-radio>
    </el-tooltip>
      </el-form-item>
      <el-form-item label="Prompt:"  prop="templateContent" key="templateContent1">

        <promptInput  ref="promptInput" v-model="form.templateContent" :templateFramework="form.templateFramework" :isEdit="isEdit" />
      </el-form-item>
    </template>
    <template v-else>
      <el-form-item label="正向Prompt:" prop="templateContent" key="templateContent2">
        <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="form.templateContent" class="inputWidth">
        </el-input>

      </el-form-item>
      <el-form-item label="负向Prompt:" prop="negativeTemplateContent" key="negativeTemplateContent1">
        <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="form.negativeTemplateContent" class="inputWidth">
        </el-input>

      </el-form-item>
    </template>
        <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 {TemplateController} from '@/api/gptController.js';
import promptInput from './promptInput.vue';
export default {
  data () {
    return {
      form: {
        interfaceAddress: '',
        negativeTemplateContent: '',
        negativeTemplateVariables: '',
        parameterFormat: '{}',
        scenarioType: 0,
        templateContent: '',
        templateFramework: 0,
        templateLabel: '',
        templateName: '',
        templateSource: 0,
        templateVariables: '',
        viewCount: 0
      },
      rules: {
        'templateName': [{ required: true, message: '请选输入名称', trigger: 'blur' }],
        'templateContent': [{ required: true, message: '请选输入Prompt', trigger: 'blur' }],
        'negativeTemplateContent': [{ required: true, message: '请选输入负向Prompt', trigger: 'blur' }]
      }
      
    };
  },
  props: ['isEdit', 'item'],
  components: {promptInput},

  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.promptTemplateDto = {...this.form};

            if (this.isEdit) {
              TemplateController.update(this, params).then(res => {
                resolve(res);
                this.$message.success('编辑成功');
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            } else {
              TemplateController.add(this, params).then(res => {
                resolve(res);
                this.$message.success('添加成功');
                this.onCancel(true);
              }).catch(e => {
                reject(e);
              });
            }
          } else {
      
          }
        });
      });
    },
    changeScenarioType (data) {
      if (this.$refs.promptInput) {
        this.$refs.promptInput.init()
      }
      this.form.templateContent = ''
      this.form.negativeTemplateContent = ''
    }
  }
};
</script>
<style scoped>
.inputWidth {
  width: 600px;
}
</style>
