<!-- -->
<template>
  <el-form label-position="left" ref="form" label-width="80px" :model="form" :size="defaultFormItemSize" :rules="rules">
    <el-form-item label="数据集版本:">
      V {{ form.datasetVersion  }}
    </el-form-item>
    <el-form-item label="备注信息:">
      <el-input v-model="form.remark" style="width:70%" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请备注本次版本主要做的修改，如添加数据、更换标注方式等，限制50字符内" maxlength="50">
      </el-input>
    </el-form-item>

    <el-form-item label="继承历史版本:">
      <el-switch v-model="form.isInherit" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" @change="isInherit">
      </el-switch>
    </el-form-item>

    <el-form-item label="历史版本:" v-if="form.isInherit == 1" prop="hisVersion">
      <el-select v-model="form.hisVersion" placeholder="请选择">
        <el-option v-for="item in versionList" :key="item.datasetVersion" :label="'V' + item.datasetVersion" :value="item.datasetVersion">
        </el-option>
      </el-select>
    </el-form-item>
    <template v-else>
      <el-form-item label="标注类型:" prop="dimensionType">
        <el-radio v-for="item in LabelType.getList()" :key="item.id" v-model="form.dimensionType" @change="isText" :label="item.id">{{ item.name }}</el-radio>
      </el-form-item>
      <el-form-item label="标注模版:" v-if="form.dimensionType === 0" prop="template">
        <el-radio v-for="item in MarkTemplate.getList()" :key="item.id" v-model="form.template" :label="item.id">{{ item.name }}</el-radio>
      </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 { DatasetVersion } from '@/api/gptController.js'
export default {
  data() {
    return {
      versionList: [],
      form: {
        datasetVersion: undefined,
        isInherit: 1,
        hisVersion: undefined,
        dimensionType: undefined,
        template: undefined
      },
      rules: {
        hisVersion: [{ required: true, message: '历史版本不能为空', trigger: 'blur' }],
        dimensionType: [{ required: true, message: '标注模版不能为空', trigger: 'blur' }],
        template: [{ required: true, message: '标注模版', trigger: 'blur' }]
      }
    }
  },
  props: ['isEdit', 'datasetId'],
  components: {},

  computed: {},
  watch: {
    versionList() {
      this.form.datasetVersion = this.versionList?.slice(0, 1)[0]?.datasetVersion + 1
    }
  },
  mounted() {
    this.intFrom()
    this.getList()
  },

  methods: {
    intFrom() {
      this.form = { ...this.form, datasetId: this.datasetId }
    },
    onCancel(isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess)
      }
    },
    getList() {
      let params = {
        datasetVersionDtoFilter: { datasetId: this.datasetId },
        pageParam: {
          pageNum: 0,
          pageSize: 100
        }
      }
      DatasetVersion.list(this, params)
        .then((res) => {
          this.versionList = res.data.dataList
        })
        .catch((e) => {})
    },
    onSubmit() {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let params = {}
            params.datasetVersionDto = { ...this.form }

            if (this.isEdit) {
              DatasetVersion.update(this, params)
                .then((res) => {
                  resolve(res)
                  this.$message.success('编辑成功')
                  this.onCancel(true)
                })
                .catch((e) => {
                  reject(e)
                })
            } else {
              DatasetVersion.add(this, params)
                .then((res) => {
                  resolve(res)
                  this.$message.success('添加成功')
                  this.onCancel(true)
                })
                .catch((e) => {
                  reject(e)
                })
            }
          } else {
            // reject();
          }
        })
      })
    },
    isInherit(data) {
      if (data === 1) {
        this.form.dimensionType = undefined
        this.form.template = undefined
      } else if (data === 0) {
        this.form.hisVersion = undefined
      }
    },
    isText(data) {
      if (data !== 0) {
        this.form.template = undefined
      }
    }
  }
}
</script>
<style scoped>
.inputWidth {
  width: 600px;
}

.title {
  font-size: 20px;
  margin-bottom: 16px;
}

.introduce {
  font-size: 12px;
  color: #909399;
}
</style>
