<template>
  <div id="changeFolderName">
    <el-dialog
      title="提示"
      :visible.sync="changeFolderNameVisible"
      width="30%"
      :before-close="closeDiaLog"
    >
      <div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          @submit.native.prevent
          ref="changeFolderModelName"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="文件夹名称" prop="name">
            <el-input v-model="ruleForm.name" @keyup.enter.native="changeFolder" focus></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDiaLog">取 消</el-button>
        <el-button type="primary" @click="changeFolder">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/api/model/file.js";
import { mapGetters } from "vuex";
export default {
  computed: {
    ...mapGetters("layout", ["projectId", "userId", "secret"])
  },
  props: {
    changeFolderNameVisible: Boolean,
    folderName: String,
    currentFolderId: String
  },
  data() {
    return {
      ruleForm: {
        name: ""
      },
      rules: {
        name: [
          { required: true, message: "文件夹名称不能为空!", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    closeDiaLog() {
      this.resetForm();
      this.$emit("closeChangeFolderVisible");
    },
    changeFolder() {
      this.$refs["changeFolderModelName"].validate(valid => {
        if (valid) {
          if (this.folderName == this.ruleForm.name) {
            this.$message({ message: "文件夹名字不可与原来相同！",type:'error' });
          } else {
            request.updateModelName(
              { Name: this.ruleForm.name, Id: this.currentFolderId },
              () => {
                this.$message({
                  message: "模型文件夹创建成功",
                  type: "success"
                });
                // 重新获取文件夹列表
                this.$emit("finishChangeFolderName");
                this.closeDiaLog();
              }
            );
          }
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.$refs["changeFolderModelName"].resetFields();
    },
  },
  watch: {
    folderName: function(val, oldVal) {
      this.ruleForm.name = val;
    }
  }
};
</script>
<style scoped >
.dialog-footer {

}
</style>
