<!-- 基本信息-->
<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="服务名称:" prop="modelDeployDto.serviceName">
      <el-input v-model="form.modelDeployDto.serviceName" class="inputWidth" placeholder="服务名称" :maxlength="20" :minlength="2"></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.modelDeployDto.serviceDescription" placeholder="服务描述"></el-input>
    </el-form-item>
    <el-form-item label="选择模型:" prop="modelDeployDto.versionId">
      <el-cascader v-model="form.modelDeployDto.versionId" :options="modelList" :props='{ label: "name", value: "id", emitPath: false }'></el-cascader>
    </el-form-item>
    <el-form-item label="API地址:">
      <el-input v-model="form.modelDeployDto.apiUrl" class="inputWidth" placeholder="API地址"></el-input>
    </el-form-item>
    <el-row class="title">模型配置</el-row>
    <el-form-item label="资源配置:" prop="modelDeployDto.resourceInfo">
      <modelConfiguration v-model="form.modelDeployDto.resourceInfo" />
    </el-form-item>

    <el-row type="flex" justify="end" class="dialog-btn-layer mt20">
      <el-button :plain="true" @click="onCancel(false)">取消</el-button>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </el-row>
  </el-form>
</template>

<script>
import { ModelDeployment } from '@/api/gptController.js'
import modelConfiguration from './modelConfiguration'
export default {
  data() {
    return {
      modelList: [],
      form: {
        modelDeployDto: {
          apiUrl: undefined,
          resourceInfo: '',
          serviceName: '',
          serviceVersion: undefined,
          versionId: undefined
        }
      },
      rules: {
        'modelDeployDto.serviceName': [{ required: true, message: '请输入服务名称', trigger: 'blur' }],
        'modelDeployDto.resourceInfo': [{ required: true, message: '请选择资源配置', trigger: 'blur' }],
        'modelDeployDto.versionId': [{ required: true, message: '请选择模型', trigger: 'blur' }]
      }
    }
  },
  props: ['isEdit', 'item'],
  components: { modelConfiguration },

  computed: {},

  mounted() {
    this.intFrom()
    this.getModelList()
  },

  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) {
              ModelDeployment.update(this, params)
                .then((res) => {
                  resolve(res)
                  this.$message.success('编辑成功')
                  this.onCancel(true)
                })
                .catch((e) => {
                  reject(e)
                })
            } else {
              ModelDeployment.add(this, params)
                .then((res) => {
                  resolve(res)
                  this.$message.success('添加成功')
                  this.onCancel(true)
                })
                .catch((e) => {
                  reject(e)
                })
            }
          } else {
            // reject();
          }
        })
      })
    },
    getModelList() {
      ModelDeployment.listForTree(this, {})
        .then((res) => {
          this.modelList = res.data.map((item) => {
            return {
              id: item.modelId,
              name: item.modelName,
              children:
                // prettier-ignore
                item.modelVersionList === [] ? [] : item.modelVersionList.map((item2) => {
                  return { id: item2.versionId, name: 'V' + item2.modelVersion }
                })
            }
          })
        })
        .catch((e) => {
          console.log(e)
        })
    }
  }
}
</script>
<style scoped>
.inputWidth {
  width: 600px;
}
.title {
  font-size: 20px;
  margin-bottom: 16px;
}
.introduce {
  font-size: 12px;
  color: #909399;
}
</style>
