<!-- 创建Prompt模板 -->
<template>
  <el-form label-position="left" ref="form" label-width="80px" :model="form" :size="defaultFormItemSize">
    <el-form-item label="模板名称:">
      <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 templateLabelOptions.dropdownList" :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 scenarioTypeOptions.dropdownList" :label="item.id" :size="defaultFormItemSize" :key="item.id">{{ 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 parameterFormatOptions.dropdownList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <template v-if="form.scenarioType == 1">
      <el-form-item label="模板框架:" class="templateFramework">
        <el-radio v-model="form.templateFramework" v-for="item in templateFrameworkOptions.dropdownList" :label="item.id" :size="defaultFormItemSize" :key="item.id">{{ item.name }}</el-radio>
      </el-form-item>
      <el-form-item label="Prompt:">
        <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="form.templateContent" class="inputWidth">
        </el-input>
         {{form.templateVariables}}
      </el-form-item>
    </template>
    <template v-else>
      <el-form-item label="正向Prompt:">
        <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="form.templateContent" class="inputWidth">
        </el-input>
        {{form.templateVariables}}
      </el-form-item>
      <el-form-item label="负向Prompt:">
        <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="form.negativeTemplateContent" class="inputWidth">
        </el-input>
        {{form.negativeTemplateVariables}}
      </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 { DictionaryController } from '@/api';
import { DropdownWidget } from '@/utils/widget.js';
export default {
  data () {
    return {
      form: {
        'interfaceAddress': '',
        'negativeTemplateContent': '',
        'negativeTemplateVariables': '',
        'parameterFormat': '',
        'scenarioType': '1',
        'templateContent': '',
        'templateFramework': '1',
        'templateLabel': '',
        'templateName': '',
        'templateSource': 0,
        'templateVariables': '',
        'viewCount': 0
      },
      templateLabelOptions: new DropdownWidget(this.loadTlDropdownList, true, 'id', 'parentId', true),
      scenarioTypeOptions: new DropdownWidget(this.loadStDropdownList, true, 'id', 'parentId', true),
      parameterFormatOptions: new DropdownWidget(this.loadPfDropdownList, true, 'id', 'parentId', true),
      templateFrameworkOptions: new DropdownWidget(this.loadTfDropdownList, true, 'id', 'parentId', true)
      
    };
  },
  props: ['isEdit'],
  components: {},

  computed: {

  },

  mounted () {
    console.log(this.scenarioTypeOptions);
  },

  methods: {
    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 {
            reject();
          }
        });
      });
    },
    
    loadTlDropdownList () {
      return new Promise((resolve, reject) => {
        let params = {};
        params.dictCode = 'TemplateLabelLabel';
        DictionaryController.dictGlobalDict(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    },
    loadStDropdownList () {
      return new Promise((resolve, reject) => {
        let params = {};
        params.dictCode = 'ScenarioTypeLabel';
        DictionaryController.dictGlobalDict(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    },
    loadPfDropdownList () {
      return new Promise((resolve, reject) => {
        let params = {};
        params.dictCode = 'ParameterFormatLabel';
        DictionaryController.dictGlobalDict(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    },
    loadTfDropdownList () {
      return new Promise((resolve, reject) => {
        let params = {};
        params.dictCode = 'TemplateFrameworkLabel';
        DictionaryController.dictGlobalDict(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    }
  }
};
</script>
<style scoped>
.inputWidth {
  width: 600px;
}
</style>
