<template>
  <el-dialog
    width="55%"
    @opened="opened()"
    @closed="closed()"
    :show-close="false"
    class="my-info-dialog"
  >
    <template #title>
      <div class="showTitle">
        <img
          class="titleImg"
          src="../../assets/img/controlcenter/daraTrue.png"
          alt=""
        />
        <div class="titles">国电电力{{env === 'SX'? '山西': '河北'}}新能源开发有限公司</div>
      </div>
    </template>
    <div class="body">
      <div class="dataList">
        <div class="data">
          <div class="name">健康指数：</div>
          <div class="num">{{ showDate?.healthIndex?.value }}</div>
        </div>
        <div class="data">
          <div class="name">资源指数：</div>
          <div class="num">{{ showDate?.resourceIndex?.value }}</div>
        </div>
        <div class="data">
          <div class="name">风能利用率：</div>
          <div class="num">{{ showDate?.windEnergyRate?.value }}%</div>
        </div>
        <div class="data">
          <div class="name">曲线跟随率：</div>
          <div class="num">{{ showDate?.curveFollowingRate?.value }}%</div>
        </div>
        <div class="data">
          <div class="name">实际功率：</div>
          <div class="num">{{ showDate?.realTimePower?.value }}</div>
          <div class="unit">MW</div>
        </div>
        <div class="data">
          <div class="name">理论功率：</div>
          <div class="num">{{ showDate?.theoreticalPower?.value }}</div>
          <div class="unit" @click="ChangeColor()">MW</div>
        </div>
        <div class="data">
          <div class="name">AGC有功设定：</div>
          <div class="num">{{ showDate?.agcPowerSet?.value }}</div>
          <div class="unit" @click="ChangeColors()">MW</div>
        </div>
      </div>
      <div id="totleEcharts" class="echarts"></div>
      <!-- <MultipleLineChart height="400px" :units="powerLineChartData.units" :list="powerLineChartData.value" :colors="colors"
				:showLegend="true" /> -->
    </div>
  </el-dialog>
</template>

<script>
import MultipleLineChart from "../focus/multiple-line-chart.vue";
import MessageBridge from "utils/MessageBridge";
import dayjs from "dayjs";
import api from "api/index";
import * as echarts from "echarts";
export default {
  components: {
    MultipleLineChart,
  },
  props: {
    stationName: {
      type: String,
      default: "",
    },
    data: {
      type: Array,
    },
  },
  created() {},
  mounted() {},
  data() {
    return {
      // 定时器
      timer: "",
      colors: ["rgba(75, 85, 174, 1)", "rgba(05, 187, 76, 1)"],
      showDate: {},
      chartData: {
        units: [""],
        value: [
          {
            title: "实际功率(MW)",
            yAxisIndex: 0,
            value: [],
          },
          {
            title: "理论功率(MW)",
            yAxisIndex: 0,
            value: [],
          },
        ],
      },
      timeData: [],
      PowerSet: [],
      ActualPower: [],
      index: 0,
      powerLineChartData: {},
      intervals: null,
      env: process.env.VUE_APP_ENV,
    };
  },
  methods: {
    opened() {
      this.getMessage();
      this.intervals = setInterval(this.getMessage, 10000);
    },
    getMessage() {
      api.stationOverview().then((res) => {
        let data = res.data;
        data.healthIndex.value = Number(data.healthIndex.value).toFixed(0);
        data.resourceIndex.value = Number(data.resourceIndex.value).toFixed(0);
        data.realTimePower.value = data.realTimePower.value.toFixed(2);
        data.theoreticalPower.value = data.theoreticalPower.value.toFixed(2);
        data.agcPowerSet.value = data.agcPowerSet.value.toFixed(2);
        data.windEnergyRate.value = data.windEnergyRate.value.toFixed(2);
        data.curveFollowingRate.value =
          data.curveFollowingRate.value.toFixed(2);
        this.showDate = data;
      });
    },
    async getDate() {
      await this.getPower({
        PowerSet: this.env === 'HB'? "HB_GD_QYG_JS_XX_XX_XXX_CI0125" : "HB_GD_QYG_JS_XX_XX_XXX_CI0125",
        name: "ActualPower",
      });
      await this.getPower({
        ActualPower: this.env === 'HB' ? "HB_GD_QYG_JS_XX_XX_XXX_CI0180" : "HB_GD_QYG_JS_XX_XX_XXX_CI0180",
        name: "theoryPower",
      });
      this.timer = setTimeout(() => {
        this.getDate();
      }, 60000);
    },
    getPower(values) {
      let date = new Date();
      let endTs = date.getTime();
      let startTs = endTs - 28800000;
      let names = values.name;
      api
        .getPower({
          tagName: values.PowerSet ? values.PowerSet : values.ActualPower,
          startTs: startTs,
          endTs: endTs,
          interval: 60,
        })
        .then((res) => {
          if (res) {
            if (names === "ActualPower") {
              let list = [];
              res.data.forEach((item) => {
                list.push({
                  text: dayjs(item.ts).format("HH:mm"),
                  value: parseFloat(item.doubleValue.toFixed(2)),
                });
                this.chartData.value[0].value = list;
              });
            } else {
              let list = [];
              res.data.forEach((item) => {
                list.push({
                  text: dayjs(item.ts).format("HH:mm"),
                  value: parseFloat(item.doubleValue.toFixed(2) / 1000),
                });
                this.chartData.value[1].value = list;
              });
            }
            this.totleErtcher(this.chartData);
          }
        });
    },
    totleErtcher(chartData) {
      console.log(document.getElementById("totleEcharts"));
      let chartDom = document.getElementById("totleEcharts");
      let myChart = echarts.init(chartDom, "#ffffff");
      let option;
      option = {
        tooltip: {
          trigger: "axis",
        },
        legend: {
          show: true,
          data: chartData.value.map((t) => {
            return t.title;
          }),
          right: 56,
          icon: "circle",
          itemWidth: 6,
          inactiveColor: "#606769",
          textStyle: {
            color: "#B3BDC0",
            fontSize: 12,
          },
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            axisLabel: {
              interval: 60,
              showMinLabel: true,
              showMaxLabel: true,
              formatter: "{value}",
              fontSize: 14,
              textStyle: {
                color: "#606769",
              },
            },
            axisLine: {
              show: false,
            },
            data: chartData.value[0].value.map((item) => {
              return item.text;
            }),
          },
        ],
        yAxis: {
          type: "value",
          axisLabel: {
            formatter: "{value}",
            fontSize: 14,
          },
          axisLine: {
            show: false,
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "#606769",
              type: "dashed",
            },
          },
        },
        series: [
          {
            name: chartData.value[0].title,
            smooth: true,
            showSymbol: false,
            data: chartData.value[0].value.map((item) => {
              return item.value;
            }),
            type: "line",
            lineStyle: {
              normal: {
                color: "rgba(75, 85, 174, 1)",
                width: 1,
              },
            },
          },
          {
            name: chartData.value[1].title,
            smooth: true,
            showSymbol: false,
            data: chartData.value[1].value.map((item) => {
              return item.value;
            }),
            type: "line",
            lineStyle: {
              normal: {
                color: "rgba(05, 187, 76, 1)",
                width: 1,
              },
            },
          },
        ],
      };

      option && myChart.setOption(option);
    },
    ChangeColors() {
      this.colors = ["#aa2116", "#fcaf17"];
    },
    ChangeColor() {
      this.colors = ["rgba(75, 85, 174, 1)", "rgba(05, 187, 76, 1)"];
    },
    closed() {
      clearInterval(this.timer);
      //勿删，传递关闭方法
      clearInterval(this.intervals);
      this.intervals = null;
      this.timer = null;
    },
  },
  unmounted() {
    clearInterval(this.intervals);
    this.intervals = null;
  },
};
</script>
<style scoped>
.echartsBox {
  width: 64rem;
  height: 220px;
  overflow: hidden;
}

.el-dialog__header {
  background-color: #000000;
}

.showTitle {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  margin-top: -10px;
  font-size: 18px;
  color: #ffffff;
}

.titleImg {
  width: 16px;
  height: 16px;
  margin-right: 13px;
}

.icon {
  font-size: 12px;
  margin-left: 10px;
  margin-top: 5px;
}

.body {
  background-color: black;
  width: 100%;
  margin-top: -30px;
  /* height: 200px; */
}

.dataList {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  padding-top: 27px;
}

.data {
  width: 50%;
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-bottom: 22px;
}

.name {
  width: 40%;
  display: flex;
  flex-direction: row-reverse;
  font-size: 12px;
  color: #ffffff;
}

.num {
  margin-left: 59px;
  font-size: 16px;
  color: #05bb4c;
  min-width: 40px;
}

.unit {
  font-size: 16px;
  color: #ffffff;
  margin-left: 20px;
}

.condition {
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-bottom: 20px;
  border-bottom: 1px solid #3d3d3d;
  padding-bottom: 10px;
}

.status {
  width: 25%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.statusIcon {
  width: 14px;
  height: 14px;
  margin-left: 8px;
}

.echarts {
  width: 100%;
  height: 400px;
  display: inline-block;
}
</style>