<template>
  <div class="container">
    <!-- 标题区域 -->
    <div class="title">
      <div class="title_text">能耗管理</div>
      <div class="title_sub">Energy Management</div>
    </div>

    <!-- 内容区域 -->
    <div class="content">
      <div class="sub_title">能耗统计</div>
      <!-- 用电量总计区域 -->
      <div class="energy_box">
        <div class="energy_box_left">
          <div class="left_top">
            <img src="@/assets/images/energy-left-icon.png" />
            用电量总计（kw.h）
          </div>
          <div class="left_bottom" ref="eleValueDom">{{ eleValue }}</div>
        </div>
        <div class="energy_box_right">
          <div class="right_top">增速</div>
          <div class="right_bottom">
            <div ref="eleSpeedDom">{{ eleSpeed }}</div>
            <span>%</span>
          </div>
        </div>
      </div>

      <!-- 用水量总计区域 -->
      <div class="energy_box">
        <div class="energy_box_left">
          <div class="left_top">
            <img src="@/assets/images/energy-left-icon2.png" />
            用水量总计（m³）
          </div>
          <div class="left_bottom" ref="waterValueDom">{{ waterValue }}</div>
        </div>
        <div class="energy_box_right">
          <div class="right_top">增速</div>
          <div class="right_bottom">
            <div ref="waterSpeedDom">{{ waterSpeed }}</div>
            <span>%</span>
          </div>
        </div>
      </div>

      <!-- 平均用量区域 -->
      <div class="evenly">
        <div class="evenly_box">
          <img src="@/assets/images/evenly-box-icon.png" />
          <div class="box_right">
            <div class="right_top">
              <div ref="eleValueAvDom">{{ eleValueAv }}</div>
              <span>kw.h</span>
            </div>
            <div>人均用电量</div>
          </div>
        </div>

        <div class="evenly_box">
          <img src="@/assets/images/evenly-box-icon2.png" />
          <div class="box_right">
            <div class="right_top">
              <div ref="waterValueAvDom">{{ waterValueAv }}</div>
              <span>m³</span>
            </div>
            <div>人均用水量</div>
          </div>
        </div>
      </div>

      <div class="sub_title">区域能耗统计</div>

      <!-- 水电切换按钮区域 -->
      <div class="change">
        <div
          class="button"
          :class="currentIndex === '水' ? 'active' : ''"
          @click="handleClickWater"
        >
          水
        </div>
        <div
          class="button"
          :class="currentIndex === '电' ? 'active' : ''"
          @click="handleClickEle"
        >
          电
        </div>
      </div>

      <!-- 区域能耗统计图表 -->
      <div class="bar" ref="barChart"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import * as Echarts from "echarts";
import { countUpNum } from "@/utils/countUpNum";
import { reqGetEnergyAll, reqGetEnergyevenly } from "@/api/energy/index";

// 用电总量
const eleValue = ref(888.88);
const eleValueDom = ref();

// 用电速率
const eleSpeed = ref(10.06);
const eleSpeedDom = ref();

// 用水总量
const waterValue = ref(88.88);
const waterValueDom = ref();

// 用水速率
const waterSpeed = ref(10.06);
const waterSpeedDom = ref();

// 人均用电量
const eleValueAv = ref(88.88);
const eleValueAvDom = ref();

// 人均用水量
const waterValueAv = ref(38.88);
const waterValueAvDom = ref();

// 区域能耗统计图表实例
let myBarChart: any;
// 区域能耗统计图表DOM
const barChart = ref(null);

// 默认选中按钮
const currentIndex = ref("水");

onMounted(() => {
  // 获取能耗统计（总量）信息
  getEnergyAll();

  // 能耗统计（平均）信息接口地址
  getEnergyevenly();

  // 初始化区域能耗统计图表
  initBarChart();

  // 让数字跳动
  getCountUpNum();
});

// 获取能耗统计（总量）信息
const getEnergyAll = async () => {
  const res = await reqGetEnergyAll();
  // console.log(res);
};

// 能耗统计（平均）信息接口地址
const getEnergyevenly = async () => {
  const res = await reqGetEnergyevenly();
  // console.log(res);
};

// 点击水 按钮回调
const handleClickWater = () => {
  if (currentIndex.value !== "水") {
    currentIndex.value = "水";
    initBarChart();
  }
};

// 点击电 按钮回调
const handleClickEle = () => {
  if (currentIndex.value !== "电") {
    currentIndex.value = "电";
    initBarChart();
  }
};

// 初始化区域能耗统计
const initBarChart = () => {
  myBarChart = Echarts.init(barChart.value);
  // 图表配置
  const options = {
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow",
      },
    },
    legend: {
      data: ["教学楼", "行政楼", "报告厅", "食堂"],
      top: 45,
      right: 22,
      textStyle: {
        color: "#fff",
      },
    },
    color: ["#0A87E8", "#BFA23B", "#09B9DB", "#00BAAD"],
    grid: {
      top: "28%",
      left: "3%",
      right: "6%",
      bottom: "6%",
      containLabel: true,
    },
    xAxis: [
      {
        type: "category",
        axisTick: { show: false },
        axisLabel: {
          color: "#fff",
        },
        data: ["7月", "8月", "9月", "10月", "11月", "12月"],
      },
    ],
    yAxis: [
      {
        type: "value",
        name: currentIndex.value === "水" ? "m³" : "kw.h",
        nameTextStyle: {
          fontSize: 14,
          color: "#fff",
        },
        axisLabel: {
          color: "#fff",
        },
        splitLine: {
          show: false,
        },
      },
    ],
    series: [
      {
        name: "教学楼",
        type: "bar",
        barGap: 0,
        emphasis: {
          focus: "series",
        },
        data: [320, 332, 301, 334, 390, 360],
      },
      {
        name: "行政楼",
        type: "bar",

        emphasis: {
          focus: "series",
        },
        data: [220, 182, 191, 234, 290, 390],
      },
      {
        name: "报告厅",
        type: "bar",

        emphasis: {
          focus: "series",
        },
        data: [150, 232, 201, 154, 190, 370],
      },
      {
        name: "食堂",
        type: "bar",
        emphasis: {
          focus: "series",
        },
        data: [98, 77, 101, 99, 40, 390],
      },
    ],
  };
  myBarChart.setOption(options);
};

// 让数字跳动
const getCountUpNum = () => {
  countUpNum(eleValueDom.value, eleValue.value, 2);
  countUpNum(waterValueDom.value, waterValue.value, 2);
  countUpNum(eleSpeedDom.value, eleSpeed.value, 2);
  countUpNum(waterSpeedDom.value, waterSpeed.value, 2);
  countUpNum(eleValueAvDom.value, eleValueAv.value, 2);
  countUpNum(waterValueAvDom.value, waterValueAv.value, 2);
};
</script>

<style lang="scss" scoped>
.container {
  width: 435px;
  height: 951px;
  color: #fff;
  background-image: url(@/assets/images/box-bg.png);

  .title {
    display: flex;
    align-items: center;
    width: 430px;
    height: 47px;
    font-family: "庞门正道标题体";
    background-image: url(@/assets/images/title-bg.png);
    background-size: 100% 100%;

    .title_text {
      margin-left: 38px;
      font-size: 20px;
      text-shadow: 0px 0px 9px #158eff;
    }

    .title_sub {
      margin-left: 19px;
      font-size: 12px;
      color: #215a8e;
    }
  }

  .content {
    padding: 20px 19px 0 14px;
    height: 904px;

    .sub_title {
      padding-left: 28px;
      height: 35px;
      line-height: 18px;
      color: #abd6ff;
      font-weight: bold;
      background-image: url(@/assets/images/title-bg2.png);
      background-size: 100% 100%;
    }

    .energy_box {
      display: flex;
      justify-content: space-between;
      margin-top: 24px;
      padding: 0 30px 0 26px;
      width: 100%;
      height: 88px;
      background-image: url(@/assets/images/energy-box-bg.png);
      background-size: 100% 100%;

      .energy_box_left {
        .left_top {
          display: flex;
          align-items: center;
          margin-top: 9px;
          font-size: 16px;

          img {
            margin-right: 4px;
            width: 20px;
            height: 20px;
          }
        }

        .left_bottom {
          margin-top: 2px;
          font-size: 38px;
          font-weight: bold;
        }
      }

      .energy_box_right {
        min-width: 100px;
        .right_top {
          margin-top: 22px;
          font-size: 14px;
        }

        .right_bottom {
          display: flex;
          align-items: flex-end;
          margin-top: 2px;
          color: #ffd15c;
          font-size: 24px;
          font-weight: bold;
          span {
            margin-left: 2px;
            font-size: 18px;
          }
        }
      }
    }

    .evenly {
      display: flex;
      height: 130px;

      .evenly_box {
        flex: 1;
        display: flex;
        align-items: center;

        img {
          margin-right: 8px;
          width: 84px;
          height: 48px;
        }

        .box_right {
          font-size: 16px;

          .right_top {
            display: flex;
            align-items: center;
            font-weight: bold;
            font-size: 24px;

            span {
              margin-top: 5px;
              margin-left: 5px;
              font-size: 12px;
              font-weight: 400;
            }
          }
        }
      }
    }

    .change {
      display: flex;
      justify-content: flex-end;
      align-items: flex-end;
      height: 60px;
      .button {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-left: 18px;
        width: 88px;
        height: 32px;
        font-size: 14px;
        border-radius: 4px;
        border: 1px solid #bef4f7;
        background-image: linear-gradient(
          rgba(156, 255, 248, 0.4),
          rgba(152, 217, 237, 0.15),
          rgba(188, 216, 247, 0.4)
        );
        cursor: pointer;
      }

      .active {
        background-image: linear-gradient(
          rgba(29, 242, 228, 0.8),
          rgba(61, 198, 239, 0.4),
          rgba(26, 94, 232, 0.8)
        );
      }
    }

    .bar {
      height: 398px;
    }
  }
}
</style>
