<template>
  <!-- 新增楼层文件 -->
  <div id="addFloorDialog">
    <el-dialog
      title="新增楼层"
      :visible.sync="addFloorFileVisible"
      width="900px"
      :before-close="handleClose"
    >
      <el-form ref="addfloorform" :model="form" label-width="120px" style="overflow: hidden">
        <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="revit版本:">
          <el-select v-model="form.revitVersion" placeholder="请选择">
            <el-option
              v-for="item in revitVersionList"
              :key="`revit-${item.value}`"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </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 == 'RF'"
              @change="handleChange"
            ></el-input-number>
            <!-- 是否夹层 -->
            <el-checkbox style="margin:0 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 style="float: right">
          <el-button type="primary" @click="onSubmit" >确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
        <div style="float: right;margin-top: -10px;color:#AAAAAA">目前只可识别标准字典定义的设备、部件</div>
    </el-dialog>
  </div>
</template>
<script>
  import request from "@/api/model/file.js";
  import {mapGetters} from "vuex";

  export default {
    props: {
      addFloorFileVisible: Boolean,
      FolderName: String,
      floorList: Array,
      FolderId: String
    },
    computed: {
      ...mapGetters("layout", ["projectId", "userInfo", "userId", "secret", "uploaderList"])
    },
    data() {
      return {
        form: {
          desc: "", //描述
          revitVersion: "2017", //revit默认版本
          floorTypeVal: "F", //楼层类型得值
          interlayerTypeVal: "M1", //夹层类型得值
          haveInterlayer: false, //是否有夹层
          file: null, //上传文件
          floorNum: 1 //楼层
        },
        fileList: [], //上传楼层列表
        revitVersionList: [
          {
            value: "2016",
            label: "2016"
          },
          {
            value: "2017",
            label: "2017"
          },
        {
          value: "2018",
          label: "2018"
        }
      ],
      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"
        }
      ],
      //请求头
      headers: {
        ProjectId: ""
      },
      updataData: {
        model: {}
      }
    };
  },
  methods: {
    onSubmit() {
      let FloorName = null;
      if (this.form.file == null) {
        this.$message.error("模型文件不能为空！");
      } else if (!this.form.revitVersion) {
        this.$message.error("revit版本不能为空！");
      } else {
        let FloorName = null;
        // 根据是否有夹层拼接楼层名
        if (this.form.haveInterlayer) {
          if (this.form.floorTypeVal == "RF") {
            FloorName = this.form.floorTypeVal + this.form.interlayerTypeVal;
          } else {
            FloorName =
              this.form.floorTypeVal + this.form.floorNum + this.form.interlayerTypeVal;
          }
        } else {
          if (this.form.floorTypeVal == "RF") {
            FloorName = this.form.floorTypeVal;
          } else {
            FloorName = this.form.floorTypeVal + this.form.floorNum;
          }
        }
        let FloorObj = this.floorList.find((item) => {return item.FloorName == FloorName})
        if(FloorObj && FloorObj.Status != 0) {
          this.$message.error('该楼层名称已存在，请勿重复创建！');
        } else {
          let data = {
            FileName: this.form.file.name,
            FloorName: FloorName,
            FolderId: this.FolderId,
            RevitVersion: this.form.revitVersion,
            Note: this.form.desc,
            ProjectId: this.projectId,
            ReplaceReason: null,
            Size: this.form.file.size,
            UserName: this.userInfo.userName,
            UserId: this.userInfo.userId
          };
          request.modelFileUpload(data, res => {
            //  创建成功
            this.$emit("finishCreateFloor", {
              modelId: res.ModelId,
              uploadId: res.UploadId,
              floorId: res.floorId,
              file: this.form.file
            });
            this.handleClose();
          });
        }
      }
    },
    // /上传到服务器/
    submitUpload(FloorModelId) {
      this.$refs.upload.submit();
    },
    handleClose() {
      this.$emit("closeAddFloorDia");
    },
    // 删除上传文件
    handleRemove(file, fileList) {
      this.fileList = []
      this.form.file = null;
    },
    handlePreview(file, fileList) {
      console.log(file, fileList);
    },
    handleChange(file, fileList) {
      console.log(file, fileList);
    },
    // 获取上传文件
    onChangeUpLoad(file, fileList) {
      console.log(file, fileList);
      if (fileList.length) {
        this.form.file = file;
      }
    }
  },
  watch: {
    addFloorFileVisible(val) {
      if (val) {
        this.handleRemove();
        this.form = {
          revitVersion: "2017", //revit默认版本
          desc: "", //描述
          floorTypeVal: "F", //楼层类型得值
          interlayerTypeVal: "M1", //夹层类型得值
          haveInterlayer: false, //是否有夹层
          file: null, //上传文件
          floorNum: 1 //楼层
        };
      }
    }
  },
  mounted() {
    this.fileList = [];
    this.form.file = null;
  }
};
</script>
<style lang="less">
#addFloorDialog {
  .floorModle {
    display: flex;
    justify-content: left;
  }
}
</style>
