<template>
  <div style="overflow: hidden auto" v-loading="loading">
    <cardList
      :content="content"
      :source="source"
      :TableVisibleMoreLoading="TableVisibleMoreLoading"
      :computingNum="computingNum"
      @calcSpecial="calcSpecial"
      @openComputed="openComputed"
      @RelationType="RelationType"
      @calcSpecialUpdate="calcSpecialUpdate"
    />
  </div>
</template>

<script>
import CardList from "@/components/old-adm/relation/components/overview/CardList";
// import {
//   blockQuery,
//   blockSource,
//   businessSpaceAll,
//   calcSpecial,
//   graphic,
//   graphicNumber,
//   sysDirection
// } from "../../../api/relation/api";
import relation from '@/controller/old-adm/relationController'
import { mapGetters } from "vuex";
import { logicConfig } from "@/logicConfig";
const {
  blockQuery,
  blockSource,
  businessSpaceAll,
  calcSpecial,
  graphic,
  graphicNumber,
  sysDirection
} = relation
export default {
  data() {
    return {
      content: [],
      countNumber: [],
      loading: false,
      source: [], //源端数据
      currentRelationTypeName: "", //当前点击卡片的RelationTypeName
      TableVisibleMoreLoading: false,
      computingNum: 0, //正在计算的关系数量
    };
  },
  created() {
		this.computingNum = 0;
    this.init();
  },
  watch: {
    projectId() {
      this.init();
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  mounted() {
  },
  components: { 
    CardList 
  },
  methods: {
    init() {
      this.loading = true;
      let dataNumber = {
        projectId: this.projectId
      };
      const promise1 = new Promise((resolve, reject) => {
        graphic(dataNumber, res => {
          resolve(res)
        })
      })
      const promise2 = new Promise((resolve, reject) => {
        graphicNumber(dataNumber, res => {
          resolve(res)
        })
      })
      Promise.all([promise1, promise2]).then(values => {
        this.countNumber = values[1].content;
        this.content = values[0].content;
        this.transform(this.content, this.countNumber);
        this.loading = false;
      })
    },
    updateState() {
        this.loading = true;
        let dataNumber = {
            projectId: this.projectId
        };
        graphic(dataNumber, res => {
            if (res.result === "success") {
                this.content = res.content;
                this.transform(this.content, this.countNumber);
                this.loading = false;
            } else {
                this.$message.error(res.message);
                this.loading = false;
            }
        })
    },
    transform(list1, list2) {
      let countInfo = {}; // { graphTypeName: { Id: count } }
      list2.forEach(item => {
        countInfo[item.graphTypeName] = countInfo[item.graphTypeName] || {};
        let rList = item.relationTypeProjectList;
        rList.forEach(r => {
          countInfo[item.graphTypeName][r.id] = r.count;
        });
      });
      let changeList = [];
      list1.forEach(item => {
        // 这里保存的是引用
        if (item.childGraphicTypeList) {
          changeList.push(...item.childGraphicTypeList);
        }
      });
      changeList.length &&
      changeList.forEach(item => {
        let name = item.graphTypeName;
        let rList = item.relationTypeProjectList;
        rList.forEach(r => {
          r.count = countInfo[name][r.id];
          // this.$set(r, 'cardDisabled', false)
        });
      });
    },
    RelationType(relation_type, zone_type, obj) {
      let param = {
        relation_type,
        zone_type,
        proId: {
          projectId: this.projectId
        }
      };
      this.computingNum++;
      businessSpaceAll(param).then(res => {
        this.computingNum--;
        if (res.result != logicConfig.resultObj.success) {
          this.$message({
            message: "计算失败",
            type: "error"
          });
        }
        
        this.init();
      }).catch(err => {
        console.log({err})
      });
      // todo
      obj.computationalState = 3
      setTimeout(() => {
        this.updateState();
      })
    },
    /**
     * 计算需更新的关系
     */
    calcSpecialUpdate(params, obj) {
			this.computingNum++;
			calcSpecial(params, res => {
				this.computingNum--;
				if (res.result === "failure") {
					this.$message.success('计算失败')
				}
				this.init();
			})
			obj.computationalState = 3
			setTimeout(() => {
				this.updateState();
			})
    },
    statusCard() {

    },
    calcSpecial(relation) {
      this.currentRelationTypeName = relation.RelationTypeName;
    },

    openComputed(result) {
      let param = {
        // BuildingId: '',
        domain: "DomainPiping",
        mepSystemType: "冷冻水供水管",
        projectId: this.projectId,
        sourceList: result
      };
      blockSource(param, res => {
        //覆盖源端数据
        //确定流向
        result.forEach(i => {
          let { blockId, buildingId, domain, source } = i;
          this.DataSorting(blockId, buildingId, domain, source);
        });
      });
    },
    DataSorting(BlockId, BuildingId, Domain, source) {
      let param = {
        blockId: BlockId,
        domain: Domain,
        source,
        buildingId: BuildingId,
        systemName: this.currentRelationTypeName
      };
      sysDirection(param, res => {
        this.init();
      });
    }
  }
};
</script>
<style lang="less" scoped>
</style>
