<template>
  <div id="realname">
    <div class="lt-area">
      <div class="status">
        <div class="status-count">
          <img
            class="status-img"
            src="@/assets/realname/icon_b_yi@2x.png"
            alt=""
          />
          <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/realname/icon_b_er@2x.png"
            alt=""
          />
          <div class="status-detail">
            <div class="status-text">普通工人人数</div>
            <div class="status-number">
              {{ screenDetail.normalPersonCount }}
            </div>
          </div>
        </div>
        <div class="status-count">
          <img
            class="status-img"
            src="@/assets/realname/icon_b_san@2x.png"
            alt=""
          />
          <div class="status-detail">
            <div class="status-text">管理人员人数</div>
            <div class="status-number">
              {{ screenDetail.managePersonCount }}
            </div>
          </div>
        </div>
        <div class="status-count">
          <img
            class="status-img"
            src="@/assets/realname/icon_b_si@2x.png"
            alt=""
          />
          <div class="status-detail">
            <div class="status-text">新入场人员人数</div>
            <div class="status-number">
              {{ screenDetail.newWorkPersonCount }}
            </div>
          </div>
        </div>
        <div class="status-count">
          <img
            class="status-img"
            src="@/assets/realname/icon_b_wu@2x.png"
            alt=""
          />
          <div class="status-detail">
            <div class="status-text">今日考勤人数</div>
            <div class="status-number">
              {{ screenDetail.todayAttendancePersonCount }}
            </div>
          </div>
        </div>
        <div class="status-count">
          <img
            class="status-img"
            src="@/assets/realname/icon_b_liu@2x.png"
            alt=""
          />
          <div class="status-detail">
            <div class="status-text">今日在场人数</div>
            <div class="status-number">
              {{ screenDetail.workPersonCount }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="mid-area">
      <Title text="实时考勤"></Title>
      <ShadowContainer>
        <div class="warp" id="warp-list">
          <div
            class="person-check"
            v-for="(person, index) in checkList"
            :key="index"
          >
            <div class="portrait">
              <img
                class="rect"
                src="@/assets/realname/portrait-rect.png"
                alt=""
              />
              <img
                class="person-head"
                :src="
                  person.face_img_url
                    ? person.face_img_url
                    : require('@/assets/realname/portrait.png')
                "
              />
            </div>
            <div class="person-detail">
              <div class="name">
                <div class="name-text">姓名：{{ person.person_name }}</div>
                <div class="work-no">工号：{{ person.job_number }}</div>
              </div>
              <div class="status">状态：{{ person.type_str }}</div>
              <div class="time">时间：{{ person.record_time_cn }}</div>
            </div>
          </div>
          <div v-if="checkList.length == 0" class="no-data">
            <img src="@/assets/nodata.png" alt="" />
            <div>暂无数据！</div>
          </div>
        </div>
      </ShadowContainer>
      <Title text="人员地域分布"></Title>
      <ShadowContainer>
        <div class="chart-container" id="reset-area2">
          <img class="custom-chart" src="@/assets/chart.png" />
          <div ref="areaChart" id="area-chart"></div>
        </div>
      </ShadowContainer>
    </div>
    <div class="rt-area" id="reset-area1">
      <Title type="long" text="一周考勤"></Title>
      <ShadowContainer>
        <div class="chart-title">近一周考勤</div>
        <div id="week-check-chart" ref="weekcheckChart"></div>
        <div class="chart-title">近一周工种考勤</div>
        <div id="week-work-chart" ref="weekworkChart"></div>
      </ShadowContainer>
      <div class="bottom-chart">
        <div>
          <Title text="近一周进出场统计"></Title>
          <ShadowContainer>
            <div id="in-out-chart" ref="inoutChart"></div>
          </ShadowContainer>
        </div>
        <div>
          <Title text="工种分布"></Title>
          <ShadowContainer>
            <div class="work-types">
              <div
                v-for="(iType, index) in industryTypes"
                :key="index"
                class="i-type"
              >
                <div class="i-type-name">
                  {{ iType.name }}
                </div>
                <div class="slider">
                  <el-slider
                    :value="
                      Number(
                        Number(
                          (iType.total / industryTypesTotal) * 100
                        ).toFixed(2)
                      )
                    "
                    disabled
                  ></el-slider>
                </div>
                <div class="i-type-count">
                  <span>{{ iType.total }}</span>
                  人
                </div>
              </div>
            </div>
          </ShadowContainer>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  getRealNameStatus,
  getCurrentAttendance,
  getPersonAreaStatus,
  getLastWeekAttendance,
  getLastWeekWorkType,
  getInOutData,
  getWorkTypeList,
} from "@/api/index";
export default {
  name: "Realname",
  data() {
    return {
      checkList: [],
      industryTypes: [],
      classOption: {
        singleHeight: 118,
      },
      screenDetail: {},
      inOutData: [],
      personAreaDetail: [],
      lastweekAttendanceData: [],
      lastweekWorkTypeData: [],
      industryTypesTotal: 0,
      date: "",
      intervalTimer: null,
    };
  },
  methods: {
    getTime() {
      let date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      month = month > 9 ? month : "0" + month;
      day = day < 10 ? "0" + day : day;
      let today = year + "-" + month + "-" + day;
      this.date = today;
    },
    renderAreaChart() {
      const chart = this.$echarts.init(this.$refs.areaChart);

      const areaData = [...this.personAreaDetail];
      let total = 0;
      areaData.map((item) => {
        total += item.value;
      });
      const option = {
        title: {
          x: "27%", //X坐标
          y: "40%",
          text: total,
          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: "0%",
          top: "20%",
          orient: "vertical",
          itemGap: 40,
          textStyle: {
            color: "inherit",
            rich: {
              a: {
                fontSize: 14,
                align: "left",
              },
              b: {
                fontSize: 20,
                align: "left",
                lineHeight: 25,
              },
            },
          },
          formatter: (name) => {
            let data = areaData;
            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: areaData,
          },
        ],
      };
      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("area-chart"));
      });
    },
    renderWeekCheckChart() {
      const xAxisData = this.lastweekAttendanceData.map((item) => {
        return item.date;
      });
      const checkData = this.lastweekAttendanceData.map((item) => {
        return item.num;
      });
      const chart = this.$echarts.init(this.$refs.weekcheckChart);
      const option = {
        color: "#00FFF0",
        grid: {
          top: 30,
          bottom: 30,
          left: 30,
          right: 30,
        },
        tooltip: {
          trigger: "axis",
        },
        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: [
          {
            data: checkData,
            type: "line",
            symbol: "circle",
            symbolSize: 12,
          },
        ],
      };
      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("week-check-chart"));
      });
    },
    renderWeekWorkChart() {
      const xAxisData = this.lastweekWorkTypeData.map((item) => {
        return item.date;
      });
      const workTypes = this.lastweekWorkTypeData[0].data.map((workType) => {
        return workType.name;
      });
      const seriesData = workTypes.map((workType) => {
        const dayDataArray = [];
        this.lastweekWorkTypeData.map((item) => {
          item.data.map((dayData) => {
            if (dayData.name == workType) {
              dayDataArray.push(dayData.total);
            }
          });
        });
        return {
          name: workType,
          data: dayDataArray,
          type: "line",
          smooth: true,
        };
      });

      const chart = this.$echarts.init(this.$refs.weekworkChart);
      const option = {
        grid: {
          top: 80,
          bottom: 30,
          left: 30,
          right: 30,
        },
        legend: {
          orient: "horizontal",
          x: "center",
          y: "top",
          textStyle: {
            fontSize: 12,
            color: "rgba(225, 239, 255, 0.7)",
          },
        },
        tooltip: {
          trigger: "axis",
        },
        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("week-work-chart"));
      });
    },
    renderInOutChart() {
      const xAxisData = this.inOutData.map((item) => {
        return item.date;
      });
      const inData = this.inOutData.map((item) => {
        return item.work_in;
      });
      const outData = this.inOutData.map((item) => {
        return item.work_out;
      });
      const chart = this.$echarts.init(this.$refs.inoutChart);
      const option = {
        color: ["#FF4544", "#2980FF"],
        grid: {
          top: 30,
          bottom: 30,
          left: 30,
          right: 30,
        },
        legend: {
          orient: "horizontal",
          x: "center",
          y: "top",
          textStyle: {
            fontSize: 12,
            color: "rgba(225, 239, 255, 0.7)",
          },
        },
        tooltip: {
          trigger: "axis",
        },
        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: [
          {
            name: "进场",
            data: inData,
            type: "line",
            smooth: true,
            areaStyle: {
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#FF4544",
                },
                {
                  offset: 1,
                  color: "rgba(0,0,0,0)",
                },
              ]),
            },
          },
          {
            name: "离场",
            type: "line",
            data: outData,
            smooth: true,
            areaStyle: {
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#2980ff",
                },
                {
                  offset: 1,
                  color: "rgba(0,0,0,0)",
                },
              ]),
            },
          },
        ],
      };
      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("in-out-chart"));
      });
    },
    getRealNameStatus() {
      getRealNameStatus().then((res) => {
        this.screenDetail = res.data;
      });
      getCurrentAttendance({
        start_date: this.date,
        end_date: this.date,
      }).then((res) => {
        this.checkList = [...res.data];
        this.intervalTimer = setInterval(() => {
          getCurrentAttendance({
            start_date: this.date,
            end_date: this.date,
          }).then((res) => {
            this.checkList = [...res.data];
          });
        }, 60000);
      });

      getPersonAreaStatus().then((res) => {
        this.personAreaDetail = res.data;
        this.renderAreaChart();
      });
      getLastWeekAttendance().then((res) => {
        this.lastweekAttendanceData = res.data;
        this.renderWeekCheckChart();
      });
      getLastWeekWorkType().then((res) => {
        this.lastweekWorkTypeData = res.data;
        this.renderWeekWorkChart();
      });
      getInOutData().then((res) => {
        this.inOutData = res.data;
        this.renderInOutChart();
      });
      getWorkTypeList().then((res) => {
        this.industryTypes = res.data;
        this.industryTypesTotal = eval(
          res.data
            .map((item) => {
              return item.total;
            })
            .join("+")
        );
      });
    },
  },
  mounted() {
    this.getTime();
    this.getRealNameStatus();
  },
  beforeDestroy() {
    clearInterval(this.intervalTimer);
  },
};
</script>
<style lang="less" scoped>
#realname {
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  .lt-area {
    width: 20%;
  }
  .mid-area {
    width: 28%;
  }
  .lt-area {
    .status {
      .status-count {
        background: url("@/assets/realname/rect.png") no-repeat;
        background-size: 100% 100%;
        padding: 10px 20px;
        margin-bottom: 20px;
        &:last-child {
          margin-bottom: 0;
        }
        .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-area {
    margin: 0 20px;
  }

  .rt-area {
    width: 50%;
  }
}
.bottom-chart {
  display: flex;
  justify-content: space-between;
  & > div {
    width: calc(50% - 10px);
  }
}
.warp {
  height: 480px;
  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: 80px;
      height: 80px;
      vertical-align: top;
    }
    .person-head {
      position: absolute;
      top: 8px;
      width: 64px;
      height: 64px;
      left: 8px;
    }
  }
  .person-detail {
    font-size: 14px;

    color: #c6def9;
    margin-left: 24px;
    width: calc(100% - 104px);
    line-height: calc(80px / 3);
    .name {
      display: flex;
      justify-content: space-between;
    }
  }
}
.chart-container {
  position: relative;
  .custom-chart {
    position: absolute;
    width: 23%;
    top: 50%;
    transform: translateY(-50%);
    left: 16.5%;
  }
}
#area-chart {
  width: 100%;
  height: 270px;
}
.chart-title {
  background: rgba(40, 137, 195, 0.2);
  border: 1px solid #217ff7;
  width: 160px;
  text-align: center;
  font-size: 16px;

  font-weight: bold;
  color: #ffffff;
  padding: 6px 0;
  letter-spacing: 2px;
}
#week-check-chart {
  width: 100%;
  height: 203px;
}
#week-work-chart {
  width: 100%;
  height: 203px;
}
#in-out-chart {
  width: 100%;
  height: 270px;
}
.i-type {
  display: flex;
  font-size: 14px;
  color: #c6def9;
  padding: 4px;
  .slider {
    width: 70%;
    vertical-align: top;
  }
  .i-type-name,
  .i-type-count {
    width: 15%;
    vertical-align: top;
    line-height: 42px;
    text-align: right;
    span {
      font-size: 16px;
    }
  }
  .i-type-name {
    padding-right: 12px;
  }
}
::v-deep .el-slider__bar {
  background-image: linear-gradient(90deg, #152647 0%, #5a9bee 97%) !important;
  height: 10px;
}
::v-deep .el-slider__runway {
  background: #103153;
  height: 10px;
}
::v-deep .el-slider__button {
  width: 12px;
  height: 12px;
}
::v-deep .el-slider__button-wrapper {
  top: 50%;
  transform: translateY(-50%) translateX(-50%);
}
.work-types {
  max-height: 270px;
  overflow-y: scroll;
}
.no-data {
  text-align: center;
  font-size: 24px;
  color: #1abcec;
  img {
    width: 200px;
    margin: 0 auto;
    vertical-align: top;
  }
}
</style>