<template>
  <el-dialog :title="'新建版本'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="基础版本" prop="versionName">
        <el-select v-model="dataForm.fatherversionId" placeholder="基础版本">
          <el-option v-for="data in versions" :key="data.versionId" :label="data.versionName" :value="data.versionId">
          </el-option>
        </el-select>
      </el-form-item>
     
      <el-form-item label="描述信息" prop="remark">
        <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" placeholder="请输入内容" v-model="dataForm.remark">
        </el-input>
      </el-form-item>
 
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { Message } from "element-ui"
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        versionName: "",
        fatherversionId: null,
        remark: "",
        algorithmId: null
      },
      versions:[],
    };
  },
  methods: {
    init(id) {
      this.dataForm.fatherversionId=null;
      this.dataForm.algorithmId = id;
      this.$http({
        //   查询当前算法下已有的版本
        url: this.$http.adornUrl("/version/select/"+id),
        method: "get",
        params: this.$http.adornParams(),
      })
        .then(({ data }) => {
          this.versions = data && data.code === 0 ? data.versions : [];
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/version/save`
            ),
            method: "post",
            data: this.$http.adornData({
              'versionName': this.dataForm.versionName,
              'remark': this.dataForm.remark,
              'fatherversionId': this.dataForm.fatherversionId,
              'algorithmId': this.dataForm.algorithmId,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              Message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit('refreshDataList', this.dataForm.algorithmId);
                },
              });
            } else {
              Message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
