<template>
  <div id="addFolder">
    <el-dialog title="提示" :visible.sync="addFolderVisible" width="30%" :before-close="closeDiaLog">
      <div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          @submit.native.prevent
          ref="addFolderModel"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="文件夹名称" prop="name">
            <el-input v-model.trim="ruleForm.name"  @keyup.enter.native="addFolder" 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="addFolder">确 定</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: {
    addFolderVisible: Boolean,
    folderName: String
  },
  data() {
    return {
      ruleForm: {
        name: ""
      },
      rules: {
        name: [
          { required: true, message: "文件夹名称不能为空!", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    closeDiaLog() {
      this.resetForm();
      this.$emit("closeAddFolderVisible");
    },
    addFolder() {
      this.$refs["addFolderModel"].validate(valid => {
        if (valid) {
          request.createModel({ ProjectId: this.projectId, Name:this.ruleForm.name },res => {
              this.$message({
                message: "模型文件夹创建成功",
                type: "success"

              });
              // 重新获取文件夹列表
              this.$emit('getfolderModel')
              this.closeDiaLog();
            })
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.$refs["addFolderModel"].resetFields();
    },
    createModel(Name) {
      return request.createModel({ ProjectId: this.projectId, Name });
    }
  },
  watch: {
    folderName: function(val, oldVal) {
      this.ruleForm.name = val;
    }
  }
};
</script>
