<template>
  <div id="earlywarning">
    <div class="top-stauts">
      <div class="shadow">
        <img src="@/assets/AIwarning/icon_d_qi@2x.png" alt="" />
      </div>
      <div class="status-container">
        <div class="status-count">
          <img class="status-img" src="@/assets/AIwarning/icon_d_jiu@2x.png" />
          <div class="status-detail">
            <div class="status-text">AI设备总数</div>
            <div class="status-number">
              {{ screenDetail.aiDeviceCount }}
            </div>
          </div>
        </div>
        <div class="status-count">
          <img
            class="status-img"
            src="@/assets/AIwarning/icon_d_si@2x(1).png"
          />
          <div class="status-detail">
            <div class="status-text">总人数</div>
            <div class="status-number">
              {{ screenDetail.personCount }}
            </div>
          </div>
        </div>
        <div class="status-count">
          <img
            class="status-img"
            src="@/assets/AIwarning/icon_d_shiyi@2x.png"
          />
          <div class="status-detail">
            <div class="status-text">今日报警</div>
            <div class="status-number">
              {{ screenDetail.todayDeviceWarningCount }}
            </div>
          </div>
        </div>
      </div>
      <div class="shadow">
        <img src="@/assets/AIwarning/icon_d_ba@2x.png" alt="" />
      </div>
    </div>
    <div class="mid-container">
      <div class="today-warning">
        <Title type="long" text="今日预警"></Title>
        <ShadowContainer>
          <vue-seamless-scroll
            :data="warningList"
            :class-option="classOption"
            class="warp"
            refs="scroll"
          >
            <div
              class="person-check"
              v-for="(warns, index) in warningList"
              :key="index"
            >
              <div class="portrait">
                <img
                  class="rect"
                  src="@/assets/AIwarning/icon_d_san@2x.png"
                  alt=""
                />
                <img class="person-head" :src="warns.alarm_big_img_url" />
              </div>
              <div class="person-detail">
                <div class="name">
                  <div class="name-text">{{ warns.alarm_type_text }}</div>
                  <div class="work-no">
                    <img
                      class="warn-icon"
                      src="@/assets/AIwarning/icon_d_er@2x.png"
                      alt=""
                    />
                    {{ warns.area_name }}
                    ({{ warns.serial_no }})
                  </div>
                </div>
                <div class="status">状态：{{ warns.status_text }}</div>
                <div class="time">时间：{{ warns.log_time }}</div>
              </div>
            </div>
          </vue-seamless-scroll>
        </ShadowContainer>
      </div>
      <div class="right-container">
        <Title type="long" text="今日统计"></Title>
        <ShadowContainer>
          <div class="total-chart">
            <div class="chart-container">
              <img class="custom-chart" src="@/assets/chart.png" />
              <div ref="todayChart" id="today-chart"></div>
            </div>
            <div class="chart-container">
              <img class="custom-chart" src="@/assets/chart.png" />
              <div ref="historyChart" id="history-chart"></div>
            </div>
          </div>
        </ShadowContainer>
        <Title type="long" text="本月报警趋势"></Title>
        <ShadowContainer>
          <div id="warning-chart" ref="warningChart"></div>
        </ShadowContainer>
      </div>
    </div>
  </div>
</template>
<script>
import { getAIdata, getWarningList, getMonthWarningStatis } from "@/api/index";
export default {
  name: "EarlyWarning",
  data() {
    return {
      warningList: [],
      classOption: {
        singleHeight: 152,
      },
      pieChartData: [
        { value: 30, name: "已处理" },
        { value: 10, name: "未处理" },
      ],
      screenDetail: {},
    };
  },
  methods: {
    renderWarningChart() {
      const xAxisData = this.warningChartData.map((item) => {
        return item.date;
      });
      const chart = this.$echarts.init(this.$refs.warningChart);

      const warnTypes = this.warningChartData[0].data.map((warnType) => {
        return warnType.name;
      });
      const seriesData = warnTypes.map((warnType) => {
        const dayDataArray = [];
        this.warningChartData.map((item) => {
          item.data.map((dayData) => {
            if (dayData.name == warnType) {
              dayDataArray.push(dayData.total);
            }
          });
        });
        return {
          name: warnType,
          data: dayDataArray,
          type: "line",
          smooth: true,
        };
      });

      const option = {
        grid: {
          top: 100,
          bottom: 30,
          left: 30,
          right: 30,
        },
        legend: {
          orient: "horizontal",
          left: "0px",
          right: "100px",
          textStyle: {
            fontSize: 12,
            color: "rgba(225, 239, 255, 0.7)",
          },
          itemGap: 12,
        },
        xAxis: {
          type: "category",
          data: xAxisData,
          axisTick: { show: false },
          axisLabel: {
            fontSize: "12",
            color: "#c6def9",
          },
        },
        yAxis: {
          type: "value",
          axisLabel: {
            fontSize: "12",
            color: "#c6def9",
          },
          splitNumber: 4,
          splitLine: {
            lineStyle: {
              color: "rgba(95, 120, 144, 0.4)",
            },
          },
          minInterval: 1,
        },
        series: seriesData,
      };
      chart.setOption(option);
      this.$nextTick(() => {
        // 解决echarts图表放大溢出父容器
        const resizeOb = new ResizeObserver((entries) => {
          for (const entry of entries) {
            this.$echarts.getInstanceByDom(entry.target).resize();
          }
        });
        resizeOb.observe(document.getElementById("today-chart"));
        resizeOb.observe(document.getElementById("history-chart"));
        resizeOb.observe(document.getElementById("warning-chart"));
      });
    },
    renderTodayChart() {
      const chart = this.$echarts.init(this.$refs.todayChart);
      const option = {
        title: {
          x: "27%", //X坐标
          y: "37%",
          text: "5",
          subtext: "今日整改",
          textAlign: "center",
          textStyle: {
            //标题样式
            fontSize: 30,
            fontWeight: "bolder",
            color: "#00fcff",
          },
          subtextStyle: {
            //副标题样式
            fontSize: 16,
            fontWeight: "bolder",
            color: "rgba(255,255,255,0.7)",
          },
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          itemHeight: 12,
          itemWidth: 12,
          icon: "rect",
          right: "10%",
          top: "30%",
          orient: "vertical",
          itemGap: 40,
          textStyle: {
            color: "inherit",
            rich: {
              a: {
                fontSize: 14,
                align: "left",
              },
              b: {
                fontSize: 20,
                align: "left",
                lineHeight: 25,
              },
            },
          },
          formatter: (name) => {
            let data = this.pieChartData;
            let target;
            for (let i = 0, l = data.length; i < l; i++) {
              if (data[i].name == name) {
                target = data[i].value;
              }
            }
            let arr = [`{a|${name}}`, `{b|${target}} 台`];
            return arr.join("   ");
          },
        },
        series: [
          {
            type: "pie",
            radius: ["60%", "70%"],
            center: ["28%", "50%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 20,
              borderWidth: 4,
            },
            label: {
              show: false,
              position: "center",
            },

            data: this.pieChartData,
          },
        ],
      };
      chart.setOption(option);
    },
    renderHistoryChart() {
      const chart = this.$echarts.init(this.$refs.historyChart);
      const option = {
        title: {
          x: "27%", //X坐标
          y: "37%",
          text: "10",
          subtext: "历史整改",
          textAlign: "center",
          textStyle: {
            //标题样式
            fontSize: 30,
            fontWeight: "bolder",
            color: "#00fcff",
          },
          subtextStyle: {
            //副标题样式
            fontSize: 16,
            fontWeight: "bolder",
            color: "rgba(255,255,255,0.7)",
          },
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          itemHeight: 12,
          itemWidth: 12,
          icon: "rect",
          right: "10%",
          top: "30%",
          orient: "vertical",
          itemGap: 40,
          textStyle: {
            color: "inherit",
            rich: {
              a: {
                fontSize: 14,
                align: "left",
              },
              b: {
                fontSize: 20,
                align: "left",
                lineHeight: 25,
              },
            },
          },
          formatter: (name) => {
            let data = this.pieChartData;
            let target;
            for (let i = 0, l = data.length; i < l; i++) {
              if (data[i].name == name) {
                target = data[i].value;
              }
            }
            let arr = [`{a|${name}}`, `{b|${target}} 台`];
            return arr.join("   ");
          },
        },
        series: [
          {
            type: "pie",
            radius: ["60%", "70%"],
            center: ["28%", "50%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 20,
              borderWidth: 4,
            },
            label: {
              show: false,
              position: "center",
            },

            data: this.pieChartData,
          },
        ],
      };
      chart.setOption(option);
    },
    getAIdata() {
      getAIdata().then((res) => {
        if (res.status == 200) {
          this.screenDetail = res.data;
        }
      });
      getWarningList().then((res) => {
        if (res.status == 200) {
          this.warningList = res.data;
        }
      });
      getMonthWarningStatis().then((res) => {
        if (res.status == 200) {
          this.warningChartData = res.data;
          this.renderWarningChart();
        }
      });
    },
  },

  mounted() {
    this.getAIdata();
    this.renderTodayChart();
    this.renderHistoryChart();
  },
};
</script>
<style lang="less" scoped>
#earlywarning {
  padding: 12px 20px;
}
.top-stauts {
  display: flex;
  justify-content: center;
  .shadow {
    width: 300px;
    img {
      width: 100%;
      vertical-align: top;
    }
  }
  .status-container {
    display: flex;
    width: 65vw;
    padding: 20px 0;
  }
  .status-count {
    width: calc(100% / 3 - 20px);
    margin: 0 10px;
    background: url("@/assets/realname/rect.png") no-repeat;
    background-size: 100% 100%;
    padding: 12px 20px;
    margin-bottom: 20px;
    .status-img {
      width: 120px;
    }
    .status-detail {
      display: inline-block;
      vertical-align: top;
      width: calc(100% - 120px);
      .status-text {
        font-size: 22px;
        color: #1abcec;
        text-align: right;
        line-height: 60px;
      }
      .status-number {
        text-align: right;
        font-size: 44px;
        font-weight: bold;
        color: #c6eaf9;
        line-height: 60px;
        letter-spacing: 4px;
        text-shadow: 0 0 5px #c6eaf9;
      }
    }
  }
}
.mid-container {
  display: flex;
  justify-content: space-between;
  .today-warning,
  .right-container {
    width: calc(50% - 10px);
  }
}
.warp {
  width: 100%;
  height: 680px;
  overflow: hidden;
}
.person-check {
  padding: 14px;
  background: rgba(20, 86, 144, 0.2);
  border-radius: 4px;
  margin-bottom: 12px;
  display: flex;
  .portrait {
    position: relative;
    .rect {
      width: 160px;
      height: 110px;
      vertical-align: top;
    }
    .person-head {
      position: absolute;
      top: 8px;
      width: 144px;
      height: 94px;
      left: 8px;
    }
  }
  .person-detail {
    font-size: 14px;
    color: #c6def9;
    margin-left: 24px;
    width: calc(100% - 104px);
    line-height: calc(110px / 3);
    .name {
      display: flex;
      justify-content: space-between;
      .name-text {
        font-size: 18px;
        font-weight: 400;
        color: #feba01;
      }
      .work-no {
        font-size: 14px;
        font-weight: 400;
        color: #27acfb;
        line-height: calc(110px / 3);
        .warn-icon {
          width: 16px;
          vertical-align: top;
          margin-top: 9px;
        }
      }
    }
  }
}
#warning-chart {
  width: 100%;
  height: 300px;
}
.total-chart {
  display: flex;
  justify-content: space-between;
  .chart-container {
    width: calc(50% - 20px);
    position: relative;
    .custom-chart {
      position: absolute;
      width: 30%;
      top: 50%;
      transform: translateY(-50%);
      left: 13%;
    }
  }
  #today-chart,
  #history-chart {
    width: 100%;
    height: 268px;
  }
}
</style>