<template>
  <!-- 新增楼层文件 -->
  <div id="addFloorDialog">
    <el-dialog
      title="新增楼层"
      :visible.sync="addFloorFileVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="模型文件:">
          <el-upload
            class="upload-demo"
            ref="upload"
            :headers="headers"
            :data="updataData"
            action="/modelapi/model-file/upload"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :auto-upload="false"
            :on-change="onChangeUpLoad"
            :limit="1"
          >
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="模型所属楼层:">
          <div class="floorModle">
            <el-select v-model="form.floorTypeVal" placeholder="请选择">
              <el-option
                v-for="item in floorType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
            <!-- 计数 -->
            <el-input-number
              style="margin-left:10px"
              v-model="form.floorNum"
              :min="1"
              :disabled="form.floorTypeVal == '选项2'"
              @change="handleChange"
            ></el-input-number>
            <!-- 是否夹层 -->
            <el-checkbox style="margin-left:10px" v-model="form.haveInterlayer">是否夹层</el-checkbox>
            <!-- 夹层选择 -->
            <el-select
              v-model="form.interlayerTypeVal"
              :disabled="!form.haveInterlayer"
              placeholder="请选择"
            >
              <el-option
                v-for="item in interlayerType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="备注信息:">
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/api/model/file.js";
import { mapGetters } from "vuex";
export default {
  props: {
    addFloorFileVisible: Boolean,
    FolderName: String,
    FolderId:String
  },
  computed: {
    ...mapGetters("layout", ["projectId", "userId", "secret"])
  },
  data() {
    return {
      form: {
        desc: "", //描述
        floorTypeVal: "F", //楼层类型得值
        interlayerTypeVal: "M1", //夹层类型得值
        haveInterlayer: false, //是否有夹层
        file: null, //上传文件
        floorNum: 1 //楼层
      },
      fileList: [], //上传楼层列表
      floorType: [
        {
          value: "F",
          label: "正常层(F)"
        },
        {
          value: "RF",
          label: "屋顶(RF)"
        },
        {
          value: "B",
          label: "地下(B)"
        }
      ],
      interlayerType: [
        {
          value: "M1",
          label: "夹层M1"
        },
        {
          value: "M2",
          label: "夹层M2"
        },
        {
          value: "M3",
          label: "夹层M3"
        }
      ],
      value: "",
      //请求头
      headers: {
        ProjectId: ""
      },
      updataData: {
        model: {}
      }
    };
  },
  methods: {
    onSubmit() {
      if (this.form.file == null) {
        this.$message.error("模型文件不能为空！");
      } else {
        let FloorName = null;
        // 根据是否有夹层拼接楼层名
        if (this.haveInterlayer) {
          FloorName =
            this.form.floorTypeVal + this.form.floorNum + interlayerType;
        } else {
          FloorName = this.form.floorTypeVal + this.form.floorNum;
        }
        let data = {
          ProjectId: this.projectId,
          folderId: this.FolderId,
          FloorName: FloorName
        };
        request.createFloor(data, res => {
          // let data1 = {
          //   form: this.form,
          //   res: res.data
          // };
          // this.$emit("finishCreateFloor", data1);
          // this.handleClose();
          this.headers.ProjectId = this.projectId;
          this.updataData.model =JSON.stringify({
            floorModelId:res.EntityList[0].Id,
            modelName:this.FolderName,
            userId:this.userId,
            note:this.form.desc,
            userName:''
          });
          // this.$refs.upload.submit();
        });
      }
    },
    // /上传到服务器/
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleClose() {
      this.$emit("closeAddFloorDia");
    },
    // 删除上传文件
    handleRemove(file, fileList) {
      this.form.file = null;
    },
    handlePreview(file) {
      console.log(file);
    },
    handleChange() {},
    // 获取上传文件
    onChangeUpLoad(file, fileList) {
      if (fileList.length) {
        this.form.file = file;
      }
    }
  }
};
</script>
<style lang="less">
#addFloorDialog {
  .floorModle {
    display: flex;
    justify-content: left;
  }
}
</style>
