<template>
  <div class="big-machine" v-loading="listLoading">
    <div class="condition">
      <div class="condition-left">
        <el-select
          style="margin-right: 0.1rem"
          v-model="building"
          placeholder="请选择楼栋"
          clearable
        >
          <el-option
            v-for="(item, index) in builds"
            :key="index"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
        <el-date-picker
          v-model="dayDate"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateChange"
        >
        </el-date-picker>
        <div class="success-btn" style="margin: 0 0.12rem">
          <el-button type="success" @click="findData">查询</el-button>
        </div>
        <div class="warning-btn">
          <el-button type="warning" @click="exportData">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      :data="postList"
      stripe
      :cell-style="tableRowStyle"
      highlight-current-row
      height="500"
    >
      <el-table-column label="所属楼栋" align="center" prop="dateAndWeek" />
      <el-table-column label="设备编号" align="center" prop="maxTemp" />
      <el-table-column label="设备名称" align="center" prop="minTemp" />
      <el-table-column label="抄表时间" align="center" prop="minTemp" />
      <el-table-column label="采集读数" align="center" prop="minTemp" />
      <el-table-column label="用量" align="center" prop="minTemp" />
      <el-table-column label="倍率" align="center" prop="minTemp" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { formatDay } from "@/utils/datetime";
import { energyAnalyze, analyzeExport } from "@/api/centerairC/energyManage";
import { spaceTree } from "@/api/region";
export default {
  data() {
    return {
      listLoading: false,
      dayDate: [], //日值
      dialogVisible: false,
      building: "", //选中的楼栋
      builds: [], //楼栋列表
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 表格数据
      postList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startTime: "",
        endTime: "",
      },
    };
  },
  created() {
    // 在组件创建完成后，设置默认值
  },
  mounted() {
    this.initializeTimeDate();
    this.getBuildList();
    this.getList();
  },
  methods: {
    // 选中日期事件
    dateChange() {
      // console.log("打印时间", this.timeform.time1);
      if (!this.dayDate) {
        this.$nextTick(() => {
          this.dayDate = [];
        });
      }
    },
    /** 查询楼栋 */
    getBuildList() {
      spaceTree().then((res) => {
        if (res.code == 200) {
          // 只需要保留热水的系统
          console.log("楼栋返回值", res);
          let newRes = { ...res };

          if (newRes.data && newRes.data[0] && newRes.data[0].children) {
            newRes.data[0].children = newRes.data[0].children.filter((item) => {
              // 假设子项有一个 label属性，用于检查是否包含 "热水"
              return item.label && item.label.includes("热水");
            });
          }
          // 指定要保留的最大层级（从 1 开始计数），这里假设指定为第 4 级
          const targetLevel = 4;
          // 从 data[0] 开始处理，当前层级为 1
          if (newRes.data[0]) {
            this.removeChildrenAfterLevel(newRes.data[0], 1, targetLevel);
          }
          this.builds = newRes.data[0].children[0].children[0].children;
          //   console.log("筛选后的新结果", this.builds);
          this.getList();
        }
      });
    },
    // 递归函数，用于去除指定层级往后的 children 数据
    removeChildrenAfterLevel(obj, currentLevel, targetLevel) {
      if (currentLevel >= targetLevel) {
        // 当达到指定层级时，将 children 属性置为空数组
        obj.children = [];
        return;
      }
      if (obj.children && obj.children.length > 0) {
        // 若存在 children 数组，则递归处理每个子项
        for (let i = 0; i < obj.children.length; i++) {
          this.removeChildrenAfterLevel(
            obj.children[i],
            currentLevel + 1,
            targetLevel
          );
        }
      }
    },
    // 初始化时间
    initializeTimeDate() {
      this.dayDate = formatDay(new Date()); // 更新
    },
    // 查询
    findData() {
      this.getList();
    },
    //请求数据
    getList() {
      let data = {
        startTime: this.dayDate,
        endTime: this.dayDate,
        building: this.building,
      };
      console.log("查询数据参数", data);
      this.listLoading = true;
      //   请求数据
      // Just to simulate the time of the request
      setTimeout(() => {
        this.listLoading = false;
      }, 1.0 * 1000);
    },
    // 导出
    exportData() {
      let data = {
        timeType: this.dateType,
        startTime: this.queryParams.startTime,
        endTime: this.endTime,
        building: this.building,
      };
      console.log("导出数据参数", data);
      analyzeExport(data).then((res) => {
        console.log("导出返回", res);
        if (res) {
          // 请求的结果就是文件
          // 创建一个blob URL，用于生成下载链接
          const url = window.URL.createObjectURL(new Blob([res]));
          // 创建一个<a>元素，并设置其href和download属性
          const link = document.createElement("a");
          link.href = url;
          link.setAttribute("download", "机房能耗报表.xls"); // 设置下载的文件名
          // 模拟点击下载链接
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          // 释放blob URL资源
          window.URL.revokeObjectURL(url);
          this.$message({
            type: "success",
            message: "导出成功!",
          });
        } else {
          this.$message.error("导出失败!");
        }
      });
      // .catch((err) => {
      //     this.$message.error('导出失败!');
      // })
    },
    tableRowStyle({ row, column, rowIndex, columnIndex }) {
      // 修改颜色
      if (columnIndex === 1) {
        return "color: #fd1e00;!important;text-align:center";
      }
      if (columnIndex === 2) {
        return "color: #3390ff;!important;text-align:center";
      }
      if (columnIndex === 4) {
        return "color: #75d148;!important;text-align:center";
      }
      // return "text-align:center";
    },
  },
};
</script>
<style lang="scss" scoped>
.condition {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-bottom: 0.24rem;

  .condition-left {
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;

    .years-div {
      display: flex;
      flex-direction: row;
      align-items: center;

      .years-word {
        font-size: 14px;
        margin: 0 5px;
        color: #388ff3;
      }
    }

    .time-label {
      font-size: 16px;
      line-height: 7px;
      margin-right: 12px;
      white-space: nowrap;
    }
  }
}
.data-content {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 100%;
  margin-top: 23px;
  .data-li {
    background-color: transparent;
    border-radius: 8px;
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 0.3rem;
    width: 31%;
    justify-content: space-between;
    margin-bottom: 0.24rem;
    font-family: SourceHanSansCN-Regular;
    font-size: 0.16rem;
    position: relative;
    overflow: hidden;
    .use-icon {
      width: 0.6rem;
      height: 0.6rem;
    }
    .use-text {
      width: 33%;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      color: #dbdbdb;
      .data-text {
        z-index: 11 !important;
        font-family: DIN-Bold;
        font-size: 0.24rem;
        margin-top: 0.05rem;
        color: #ffffff;
      }
      .downText {
        color: #ff4027;
        z-index: 11 !important;
        font-family: DIN-Bold;
        font-size: 0.24rem;
        margin-top: 0.05rem;
        .el-icon-bottom {
          margin-right: 0.08rem;
        }
      }
      .upText {
        color: #2ff016;
        z-index: 11 !important;
        font-family: DIN-Bold;
        font-size: 0.24rem;
        margin-top: 0.05rem;
        .el-icon-bottom,
        .el-icon-top {
          margin-right: 0.08rem;
        }
      }
    }
  }
  .type-img {
    position: absolute;
    bottom: 0;
    width: 3.17rem;
    height: 0.52rem;
    z-index: 10;
  }
  .type1 {
    border: 1px solid #e8aa13;
    border-radius: 10px;
    /* 设置渐变背景 */
    background: linear-gradient(rgba(26, 56, 65, 0.5), rgba(184, 196, 51, 0.5));
  }
  .type1::after {
    content: "";
    width: 100%;
    height: 90%;
    box-shadow: 0 0 30px 40px rgba(243, 166, 23, 0.3);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    bottom: -100% !important;
    z-index: 1;
  }
  .type2 {
    border: 1px solid #27a0f2;
    border-radius: 10px;
    /* 设置渐变背景 */
    background: linear-gradient(rgba(2, 43, 88, 0.5), rgba(8, 96, 179, 0.5));
  }
  .type2::after {
    content: "";
    width: 100%;
    height: 90%;
    box-shadow: 0 0 30px 40px rgba(28, 128, 241, 0.3);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    bottom: -100% !important;
    z-index: 1;
  }
  .type3 {
    border: 1px solid #1ad3ef;
    border-radius: 10px;
    /* 设置渐变背景 */
    background: linear-gradient(rgba(2, 50, 89, 0.5), rgba(17, 145, 151, 0.5));
  }
  .type3::after {
    content: "";
    width: 100%;
    height: 90%;
    box-shadow: 0 0 30px 40px rgba(117, 242, 248, 0.3);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    bottom: -100% !important;
    z-index: 1;
  }
  .type4 {
    border: 1px solid #11d47b;
    border-radius: 10px;
    /* 设置渐变背景 */
    background: linear-gradient(rgba(2, 57, 82, 0.5), rgba(5, 91, 90, 0.5));
  }
  .type4::after {
    content: "";
    width: 100%;
    height: 90%;
    box-shadow: 0 0 30px 40px rgba(115, 245, 176, 0.3);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    bottom: -100% !important;
    z-index: 1;
  }
}
.choice {
  margin: 20px;
  .mr20 {
    padding: 0.05rem 0.2rem;
    white-space: nowrap;
    width: auto;
  }
}
.charts {
  width: 100%;
  height: 300px;
  margin-bottom: 0.1rem;
}
// 媒体查询，适配大于2000px分辨率的大屏样式
@media (min-width: 2000px) {
  .condition {
    .condition-left {
      .years-div {
        .years-word {
          font-size: 0.14rem !important;
          margin: 0 0.05rem !important;
        }
      }

      .time-label {
        font-size: 0.16rem !important;
        line-height: 0.07rem !important;
        margin-right: 0.12rem !important;
      }
    }
  }
}
</style>
