<template>
  <div>
    <el-dialog title="发布版本" :visible.sync="isOpen" width="500px" append-to-body @close="cancel"
               :close-on-click-modal="false"
    >
      <el-form ref="form" label-position="top" :model="form" :rules="rules">
        <el-form-item label="版本名称" prop="versionName">
          <el-input v-model="form.versionName" maxlength="20" placeholder="请输入版本名称"/>
        </el-form-item>
        <el-form-item label="描述" prop="versionDesc">
          <el-input
            type="textarea"
            placeholder="请输入描述"
            v-model="form.versionDesc"
            maxlength="200"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" v-debounce:500="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>

</template>

<script>


import { add } from '@/api/dataCenter/releaseVersion'
import { getLatestDatasetVersion } from '../../../../api/dataCenter/annotationTask'

export default {
  props: {
    open: {
      type: Boolean,
      default: false,
      required: true
    },
    getList: {
      type: Function,
      required: true
    },
    datasetId: {
      type: Number,
      default: 0,
      required: true
    },
    taskId: {
      type: Number,
      default: 0,
      required: true
    }
  },
  computed: {
    isOpen: {
      get() {
        return this.open
      },
      set(value) {
        this.$emit('dialog-cancel') // 通知父组件
      }
    }
  },
  data() {
    return {
      lastVersionName: null,
      form: {
        versionName: this.lastVersionName || '' // 初始化为传入的 lastVersionName
      }, // 初始化为空
      rules: {
        versionName: [
          { required: true, message: '版本名称不能为空', trigger: 'blur' },
          { pattern: /^V\d{1,3}(\.\d{1,3}){2}$/, message: '版本名称式不正确，应为 VX.Y.Z 格式', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    isOpen(newVal) {
      if (newVal) {
        this.getLatestDatasetVersion(this.datasetId)
      }
    }
  },
  methods: {
    getLatestDatasetVersion(datasetId) {
      getLatestDatasetVersion(datasetId).then(response => {
       this.lastVersionName = response.msg;
        if (this.isValidVersion(this.lastVersionName)) {
          this.form.versionName = this.incrementMajorVersion(this.lastVersionName)
        } else {
          this.form.versionName = 'V1.0.0' // 如果格式不正确，则清空
        }
      })
    },
    // 判断版本号格式是否正确
    isValidVersion(version) {
      return /^V\d{1,3}(\.\d{1,3}){2}$/.test(version)
    },

    // 增加大版本号
    incrementMajorVersion(version) {
      const match = version.match(/^V(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
      if (match) {
        let major = parseInt(match[1], 10)
        // 增加主版本号
        major += 1
        return `V${major}.0.0` // 返回新的版本号，次版本和修订号重置为 0
      }
      return version // 如果格式不符合，则返回原始版本号
    },
    // 数据提交
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.datasetId = this.datasetId
          this.form.taskId = this.taskId
          add(this.form).then(response => {
            this.$modal.msgSuccess('发布成功')
            this.cancel()
          })
        }
      })
    },
    // 取消按钮
    cancel() {
      this.isOpen = false
      this.reset()
      this.getList()
    },
    // 表单重置
    reset() {
      this.lastVersionName = ''
      this.form = { versionName: '' } // 清空表单
      this.$refs.form.resetFields() // 调用 Element UI 的重置方法
    }
  }
}
</script>

<style scoped lang="scss">
/* 样式按需定义 */
</style>
