<template>
  <el-dialog :title="title" :visible.sync="dialog" :before-close="handleClose" width="900px">
    <div v-loading="loading" class="content">
      <el-scrollbar style="height:100%;">
        <span>上传人：{{ versionData.userName }}</span>
        <span style="marginLeft:30px;">上传时间：{{ versionData.acceptTime }}</span>
        <p style="marginTop:15px;">更新内容：</p>
        <div
          v-if="Object.keys(versionData.createData).length || Object.keys(versionData.deleteData).length || versionData.buildList.length">
          <!-- 新增设备概要内容 -->
          <el-divider v-if="Object.keys(versionData.createData).length">新增设备概要内容</el-divider>
          <p v-if="Object.keys(versionData.createData).length" style="marginBottom:10px;">
            于【{{ versionData.acceptTime }}】上传模型文件，新增设备实例如下：</p>
          <p v-for="(item, key) in versionData.createData" :key="'C'+key">【{{ mapType[key] }}】：【{{ item.length }}】个</p>
          <!-- 删除设备概要内容 -->
          <el-divider v-if="Object.keys(versionData.deleteData).length">删除设备概要内容</el-divider>
          <p v-if="Object.keys(versionData.deleteData).length" style="marginBottom:10px;">
            于【{{ versionData.acceptTime }}】上传模型文件，删除设备实例如下：</p>
          <p v-for="(item, key) in versionData.deleteData" :key="'D'+key">【{{ mapType[key] }}】：【{{ item.length }}】个</p>
          <!-- 土建设备概要内容 -->
          <el-divider v-if="versionData.buildList.length">土建设备概要内容</el-divider>
          <p v-if="versionData.buildList.length" style="marginBottom:10px;">
            于【{{ versionData.acceptTime }}】上传模型文件，增删土建设备实例如下：</p>
          <p v-if="versionData.createWall.length">【新增实体墙】：【{{ versionData.createWall.length }}】个</p>
          <p v-if="versionData.deleteWall.length">【删除实体墙】：【{{ versionData.deleteWall.length }}】个</p>
          <p v-if="versionData.createVirtualWall.length">【新增虚拟墙】：【{{ versionData.createVirtualWall.length }}】个</p>
          <p v-if="versionData.deleteVirtualWall.length">【删除虚拟墙】：【{{ versionData.deleteVirtualWall.length }}】个</p>
          <p v-if="versionData.createColumn.length">【新增柱体】：【{{ versionData.createColumn.length }}】个</p>
          <p v-if="versionData.deleteColumn.length">【删除柱体】：【{{ versionData.deleteColumn.length }}】个</p>
          <!-- 新增设备 -->
          <el-divider v-if="Object.keys(versionData.createData).length">新增设备</el-divider>
          <div v-for="(item, key) in versionData.createData" :key="'CL'+key">
            <p>【{{ mapType[key] }}】</p>
            <p v-for="(obj, i) in item" :key="obj.Id">
              【{{ i + 1 }}】、建筑：【{{ obj.BuildingName || "无" }}】；楼层：【{{
                obj.FloorName || "无"
              }}】；设备全局id：【{{ obj.ObjId || "无" }}】；BIMID：【{{ obj.BimId || "无" }}】；设备本地编码：【{{
                obj.LocalId || "无"
              }}】；设备本地名称：【{{ obj.LocalName || "无" }}】；CADID：【{{ obj.CADID || "无" }}】；</p>
          </div>
          <!-- 删除设备 -->
          <el-divider v-if="Object.keys(versionData.deleteData).length">删除设备</el-divider>
          <div v-for="(item, key) in versionData.deleteData" :key="'DL'+key">
            <p>【{{ mapType[key] }}】</p>
            <p v-for="(obj, i) in item" :key="obj.Id">
              【{{ i + 1 }}】、建筑：【{{ obj.BuildingName || "无" }}】；楼层：【{{
                obj.FloorName || "无"
              }}】；设备全局id：【{{ obj.ObjId || "无" }}】；BIMID：【{{ obj.BimId || "无" }}】；设备本地编码：【{{
                obj.LocalId || "无"
              }}】；设备本地名称：【{{ obj.LocalName || "无" }}】；CADID：【{{ obj.CADID || "无" }}】；</p>
          </div>
          <!-- 新增实体墙 -->
          <el-divider v-if="versionData.createWall.length">新增实体墙</el-divider>
          <p v-for="(obj, i) in versionData.createWall" :key="obj.Id">
            【{{ i + 1 }}】、建筑：【{{ obj.BuildingName || "无" }}】；楼层：【{{ obj.FloorName || "无" }}】；Id：【{{
              obj.ObjId || "无"
            }}】；</p>
          <!-- 删除实体墙 -->
          <el-divider v-if="versionData.deleteWall.length">删除实体墙</el-divider>
          <p v-for="(obj, i) in versionData.deleteWall" :key="obj.Id">
            【{{ i + 1 }}】、建筑：【{{ obj.BuildingName || "无" }}】；楼层：【{{ obj.FloorName || "无" }}】；Id：【{{
              obj.ObjId || "无"
            }}】；</p>
          <!-- 新增虚拟墙 -->
          <el-divider v-if="versionData.createVirtualWall.length">新增虚拟墙</el-divider>
          <p v-for="(obj, i) in versionData.createVirtualWall" :key="obj.Id">
            【{{ i + 1 }}】、建筑：【{{ obj.BuildingName || "无" }}】；楼层：【{{ obj.FloorName || "无" }}】；Id：【{{
              obj.ObjId || "无"
            }}】；</p>
          <!-- 删除虚拟墙 -->
          <el-divider v-if="versionData.deleteVirtualWall.length">删除虚拟墙</el-divider>
          <p v-for="(obj, i) in versionData.deleteVirtualWall" :key="obj.Id">
            【{{ i + 1 }}】、建筑：【{{ obj.BuildingName || "无" }}】；楼层：【{{ obj.FloorName || "无" }}】；Id：【{{
              obj.ObjId || "无"
            }}】；</p>
          <!-- 新增柱体 -->
          <el-divider v-if="versionData.createColumn.length">新增柱体</el-divider>
          <p v-for="(obj, i) in versionData.createColumn" :key="obj.Id">
            【{{ i + 1 }}】、建筑：【{{ obj.BuildingName || "无" }}】；楼层：【{{ obj.FloorName || "无" }}】；Id：【{{
              obj.ObjId || "无"
            }}】；</p>
          <!-- 删除柱体 -->
          <el-divider v-if="versionData.deleteColumn.length">删除柱体</el-divider>
          <p v-for="(obj, i) in versionData.deleteColumn" :key="obj.Id">
            【{{ i + 1 }}】、建筑：【{{ obj.BuildingName || "无" }}】；楼层：【{{ obj.FloorName || "无" }}】；Id：【{{
              obj.ObjId || "无"
            }}】；</p>
        </div>
        <div v-else>
          <div
            style="height: 60%;transform: translateY(50%);font-weight: normal;font-size: 12px;text-align: center;color: #909399;">
            <i class="icon-wushuju iconfont"></i>
            暂无数据
          </div>
        </div>
      </el-scrollbar>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" type="primary" @click="handleClose">关闭</el-button>
      <!-- <el-button size="small" type="primary">确 定</el-button> -->
    </span>
  </el-dialog>
</template>

<script>
import { mapGetters } from "vuex";
import { querySyncLogCount } from "@/api/model/file.js";
import { getAllEqPaType } from "@/api/scan/request";
import { queryPhysicsAllType } from "@/api/dict";

export default {
  components: {},
  props: {
    dialogVisible: Boolean,
    modelFile: Object,
  },
  computed: {
    ...mapGetters("layout", ["projectId"]),
    dialog() {
      return this.dialogVisible;
    }
  },
  data() {
    return {
      title: "版本文件信息",
      loading: true,
      logData: [],
      versionData: {
        userName: '',
        acceptTime: '',
        createData: {},
        deleteData: {},
        buildList: [],
        createWall: [],
        deleteWall: [],
        createVirtualWall: [],
        deleteVirtualWall: [],
        createColumn: [],
        deleteColumn: [],
      },
      mapType: {
        'Wall': '实体墙',
        'VirtualWall': '虚拟墙',
        'Column': '柱体',
      },
    }
  },
  mounted() {
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.$emit("update:dialogVisible", false);
    },
    // 获取版本信息
    getData() {
      if (this.modelFile.Id || this.modelFile.CurrentModelId) {
        this.versionData.userName = this.modelFile.UserName;
        this.versionData.acceptTime = this.modelFile.AcceptTime;
        let promise1 = new Promise((resolve, reject) => {
          querySyncLogCount({
            Filters: `ModelId='${this.modelFile.CurrentModelId ? this.modelFile.CurrentModelId : this.modelFile.Id}'`,
            PageNumber: 1,
            PageSize: 1000,
          }, res => {
            resolve(res)
          })
        })
        let promise2 = new Promise((resolve, reject) => {
          // getAllEqPaType({ PageNumber: 1, PageSize: 1000, }, res => {
          //   resolve(res)
          // })
          queryPhysicsAllType({ type: 'equipmentAndComponent' }, res => {
            resolve(res)
          })
        })
        Promise.all([promise1, promise2]).then(values => {
          this.logData = values[0].Content;
          let typeList = values[1].content;
          typeList.forEach(item => {
            this.mapType[item.code] = item.name;
          });
          console.log(this.mapType);
          if (values[0].Total / (values[0].PageSize * values[0].PageNumber) > 1) {
            this.queryLogData(values[0].PageNumber + 1)
          } else {
            this.formatData();
            this.loading = false;
            this.$forceUpdate();
            console.log(this.versionData);
          }
        })
      }
    },
    // 格式化数据
    formatData() {
      this.logData.forEach(item => {
        if (item.Type == 'Wall' || item.Type == 'VirtualWall' || item.Type == 'Column') {
          this.versionData.buildList.push(item);
          if (item.Type == 'Wall') {
            if (item.Operation) {//创建
              this.versionData.createWall.push(item);
            } else {//删除
              this.versionData.deleteWall.push(item);
            }
          } else if (item.Type == 'VirtualWall') {
            if (item.Operation) {//创建
              this.versionData.createVirtualWall.push(item);
            } else {//删除
              this.versionData.deleteVirtualWall.push(item);
            }
          } else if (item.Type == 'Column') {
            if (item.Operation) {//创建
              this.versionData.createColumn.push(item);
            } else {//删除
              this.versionData.deleteColumn.push(item);
            }
          }
        } else {
          if (item.Operation) {//创建
            if (this.versionData.createData[item.Type]) {
              this.versionData.createData[item.Type].push(item);
            } else {
              this.versionData.createData[item.Type] = [];
              this.versionData.createData[item.Type].push(item);
            }
          } else {//删除
            if (this.versionData.deleteData[item.Type]) {
              this.versionData.deleteData[item.Type].push(item);
            } else {
              this.versionData.deleteData[item.Type] = [];
              this.versionData.deleteData[item.Type].push(item);
            }
          }
        }
      });
    },
    // 获取版本信息
    getVersionData() {
      if (this.modelFile.Id || this.modelFile.CurrentModelId) {
        this.versionData.userName = this.modelFile.UserName;
        this.versionData.acceptTime = this.modelFile.AcceptTime;
        this.queryLogData();
      }
    },
    queryLogData(pageNum) {
      pageNum = pageNum ? pageNum : 1
      let params = {
        Filters: `ModelId='${this.modelFile.CurrentModelId ? this.modelFile.CurrentModelId : this.modelFile.Id}'`,
        PageNumber: pageNum,
        PageSize: 1000,
      }
      querySyncLogCount(params, res => {
        this.logData = this.logData.concat(res.Content);//拼接数据
        if (res.Total / (res.PageSize * res.PageNumber) > 1) {
          this.queryLogData(res.PageNumber + 1)
        } else {
          this.formatData();
          this.loading = false;
          this.$forceUpdate();
          console.log(this.versionData)
        }
      })
    },
  },
  watch: {
    dialogVisible: {
      handler() {
        if (this.dialogVisible) {
          this.loading = true;
          this.logData = [];
          this.versionData = {
            userName: '',
            acceptTime: '',
            createData: {},
            deleteData: {},
            buildList: [],
            createWall: [],
            deleteWall: [],
            createVirtualWall: [],
            deleteVirtualWall: [],
            createColumn: [],
            deleteColumn: [],
          };
          if (Object.keys(this.mapType).length > 3) {
            this.getVersionData();
          } else {
            this.getData();
          }
        }
      },
      immediate: true
    }
  }
};
</script>
<style lang="less" scoped>
.content {
  height: 400px;
  font-size: 16px;
  font-weight: 650;
  color: #333;

  /deep/ .el-scrollbar__wrap {
    overflow-x: hidden;
  }
}
</style>
