<template>
  <div id="construction">
    <div class="side-area">
      <Title text="项目概况"></Title>
      <ShadowContainer>
        <div class="construction-status">
          <div class="construction-details">
            <div class="construction-detail">
              <div class="data">{{ screenDetail.project_cost }}</div>
              <div class="text">工程造价(万元)</div>
            </div>
            <div class="construction-detail">
              <div class="data">{{ screenDetail.build_area }}</div>
              <div class="text">建筑面积(㎡)</div>
            </div>
            <div class="construction-detail">
              <div class="data small">
                {{
                  screenDetail.manageList &&
                  screenDetail.manageList[0] &&
                  screenDetail.manageList[0].person_name
                }}
                <br />
                {{
                  screenDetail.manageList &&
                  screenDetail.manageList[0] &&
                  screenDetail.manageList[0].person_mobile
                }}
              </div>
              <div class="text">项目经理</div>
            </div>
          </div>
          <div class="divider">
            <div class="divider-line"></div>
            <div class="divider-text">·参建单位·</div>
            <div class="divider-line"></div>
          </div>
          <div class="table-scroll" id="table-scroll">
            <table>
              <tr>
                <td width="60">
                  <div class="table-label">建设单位</div>
                </td>
                <td>
                  <div
                    v-for="(unit, index) in screenDetail.unitList.jsList"
                    :key="index"
                    class="table-unit"
                  >
                    <div class="unit-name">{{ unit.company_name }}</div>
                    <div class="unit-contact">
                      {{ unit.contact_person }}({{ unit.contact_mobile }})
                    </div>
                  </div>
                </td>
              </tr>
              <tr>
                <td>
                  <div class="table-label">施工单位</div>
                </td>
                <td>
                  <div
                    v-for="(unit, index) in screenDetail.unitList.sgList"
                    :key="index"
                    class="table-unit"
                  >
                    <div class="unit-name">{{ unit.company_name }}</div>
                    <div class="unit-contact">
                      {{ unit.contact_person }}({{ unit.contact_mobile }})
                    </div>
                  </div>
                </td>
              </tr>
              <tr>
                <td><div class="table-label">监理单位</div></td>
                <td>
                  <div
                    v-for="(unit, index) in screenDetail.unitList.jlList"
                    :key="index"
                    class="table-unit"
                  >
                    <div class="unit-name">{{ unit.company_name }}</div>
                    <div class="unit-contact">
                      {{ unit.contact_person }}({{ unit.contact_mobile }})
                    </div>
                  </div>
                </td>
              </tr>
              <tr>
                <td>
                  <div class="table-label">设计单位</div>
                </td>
                <td>
                  <div>
                    <div
                      v-for="(unit, index) in screenDetail.unitList.sjList"
                      :key="index"
                      class="table-unit"
                    >
                      <div class="unit-name">{{ unit.company_name }}</div>
                      <div class="unit-contact">
                        {{ unit.contact_person }}({{ unit.contact_mobile }})
                      </div>
                    </div>
                  </div>
                </td>
              </tr>
              <tr>
                <td>
                  <div class="table-label">勘察单位</div>
                </td>
                <td>
                  <div>
                    <div
                      v-for="(unit, index) in screenDetail.unitList.kcList"
                      :key="index"
                      class="table-unit"
                    >
                      <div class="unit-name">{{ unit.company_name }}</div>
                      <div class="unit-contact">
                        {{ unit.contact_person }}({{ unit.contact_mobile }})
                      </div>
                    </div>
                  </div>
                </td>
              </tr>
            </table>
          </div>
        </div>
      </ShadowContainer>
      <Title text="今日考勤率"></Title>
      <ShadowContainer>
        <div class="check-in">
          <div class="checks">
            <div class="title">管理人员/考勤率</div>
            <div class="count" style="color: rgba(254, 186, 1, 1)">
              {{ screenDetail.today_manage_attendance_rate }} <span>%</span>
            </div>
            <img
              style="width: 120px; transform: translateY(-30px)"
              src="@/assets/icon-person.png"
              alt=""
            />
          </div>
          <div class="checks">
            <div class="title">普通工人/考勤率</div>
            <div class="count" style="color: rgba(24, 215, 185, 1)">
              {{ screenDetail.today_normal_attendance_rate }} <span>%</span>
            </div>
            <img
              style="width: 120px; transform: translateY(-30px)"
              src="@/assets/icon-person.png"
              alt=""
            />
          </div>
        </div>
      </ShadowContainer>
    </div>
    <div class="mid-area">
      <Title text="项目平面图" type="long"></Title>
      <ShadowContainer>
        <div class="swagger-container">
          <img class="lt" src="@/assets/lt.png" alt="" />
          <img class="rt" src="@/assets/rt.png" alt="" />
          <img class="rb" src="@/assets/rb.png" alt="" />
          <img class="lb" src="@/assets/lb.png" alt="" />
          <el-carousel :interval="5000" arrow="always" height="530px">
            <el-carousel-item
              v-for="(item, index) in screenDetail.banner_image_arr"
              :key="index"
            >
              <img class="container-img" :src="item.url" alt="" />
            </el-carousel-item>
          </el-carousel>
        </div>
      </ShadowContainer>
      <Title text="工程进度" type="long"></Title>
      <ShadowContainer>
        <div class="progress-contains">
          <div class="progress">
            <div class="progress-container">
              <div class="progress-day">
                <div class="progress-count">{{ progressDetail.zgq_num }}</div>
                <div class="progress-text">总工期(天)</div>
              </div>
              <div class="progress-day">
                <div class="progress-count" style="color: #ffaf51">
                  {{ progressDetail.sg_num }}
                </div>
                <div class="progress-text">已施工(天)</div>
              </div>
              <div class="progress-day">
                <div class="progress-count" style="color: #ff5151">
                  {{ progressDetail.current_progress_over_day }}
                </div>
                <div class="progress-text">距离当前工期结束(天)</div>
              </div>
            </div>
          </div>
          <div id="step-scroll">
            <el-steps :active="progressDetail.step" finish-status="success">
              <el-step
                v-for="(progress, index) in progressDetail.progress_data"
                :key="index"
              >
                <template #title>
                  <div class="step-title">
                    {{ progress.over_date }}
                    <div class="step-status">{{ progress.status_text }}</div>
                  </div>
                </template>
                <template #description>
                  <div class="step-description">{{ progress.name }}</div>
                </template>
              </el-step>
            </el-steps>
          </div>
        </div>
      </ShadowContainer>
    </div>
    <div class="side-area">
      <Title text="设备情况"></Title>
      <ShadowContainer>
        <div class="status">
          <div
            class="status-count"
            v-for="(status, index) in deviceStatus"
            :key="index"
          >
            <img class="status-img" :src="status.icon_path" alt="" />
            <div class="status-detail">
              <div class="status-text">
                {{ status.name }}
              </div>
              <div class="status-number">
                {{ status.total }}
                <span>台</span>
              </div>
            </div>
          </div>
        </div>
      </ShadowContainer>
      <Title text="AI预警"> </Title>
      <ShadowContainer>
        <div class="chart-container">
          <div ref="qualityChart" id="quality-chart"></div>
        </div>
      </ShadowContainer>
    </div>
  </div>
</template>
<script>
import {
  getBaseData,
  getDeviceStatus,
  getProgressDetail,
  getAIWarningData,
} from "@/api/index";
export default {
  name: "Construction",
  data() {
    return {
      constructionUnits: [
        {
          unitText: "建设单位：",
          unitName: "绍兴第二医院",
          contact: "金吉祥(1556783245)",
        },
        {
          unitText: "施工单位：",
          unitName: "绍兴第二医院",
          contact: "金吉祥(1556783245)",
        },
        {
          unitText: "监理单位：",
          unitName: "绍兴第二医院",
          contact: "金吉祥(1556783245)",
        },
        {
          unitText: "设计单位：",
          unitName: "绍兴第二医院",
          contact: "金吉祥(1556783245)",
        },
      ],
      pieChartData: [
        { value: 1048, name: "未整改" },
        { value: 735, name: "整改中" },
        { value: 580, name: "已完成" },
      ],
      personData: [],
      active: 4,
      deviceStatus: [
        {
          img: require("@/assets/status/icon_spjk@2x.png"),
          text: "视频监控",
          counts: 1,
        },
        {
          img: require("@/assets/status/icon_td@2x.png"),
          text: "塔吊",
          counts: 2,
        },
        {
          img: require("@/assets/status/icon_sjt@2x.png"),
          text: "升降机",
          counts: 2,
        },
        {
          img: require("@/assets/status/icon_hjgl@2x.png"),
          text: "环境管理",
          counts: 2,
        },
        {
          img: require("@/assets/status/icon_ydjc@2x.png"),
          text: "用电监测",
          counts: 1,
        },
        {
          img: require("@/assets/status/icon_zccl@2x.png"),
          text: "在场车辆",
          counts: 1,
        },
        {
          img: require("@/assets/status/icon_sb@2x.png"),
          text: "水表",
          counts: 1,
        },
        {
          img: require("@/assets/status/icon_lbfh@2x.png"),
          text: "临边防护",
          counts: 1,
        },
        {
          img: require("@/assets/status/icon_xlt@2x.png"),
          text: "卸料平台",
          counts: 1,
        },
      ],
      screenDetail: {
        unitList: {
          sgList: [],
          jsList: [],
          jlList: [],
          sjList: [],
          kcList: [],
        },
        manageList: [],
        plane_image_arr: [],
      },
      progressDetail: {
        progress_data: [],
        sg_num: 0,
        zgq_num: 0,
        step: 0,
        current_progress_over_day: 0,
      },
      aiwarningData: [],
    };
  },
  methods: {
    renderQualityChart() {
      let total = 0;
      this.aiwarningData.map((item) => {
        total += item.value;
      });
      const chart = this.$echarts.init(this.$refs.qualityChart);
      const option = {
        title: {
          x: "49%", //X坐标
          y: "27%",
          text: total,
          subtext: "AI预警",
          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",
          bottom: "0%",
          left: "center",
          itemGap: 40,
          textStyle: {
            color: "inherit",
            rich: {
              a: {
                fontSize: 16,
                verticalAlign: "top",
                align: "left",
                padding: [22, 0, 0, 5],
              },
              b: {
                fontSize: 18,
                align: "left",
                lineHeight: 25,
                padding: [0, 0, 0, 7],
              },
            },
          },
          formatter: (name) => {
            let data = this.aiwarningData;
            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("\n");
          },
        },
        series: [
          {
            type: "pie",
            radius: ["58%", "70%"],
            center: ["50%", "40%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 20,
              borderWidth: 4,
            },
            label: {
              show: false,
              position: "center",
            },

            data: this.aiwarningData.map((item) => {
              return {
                ...item,
                itemStyle: {
                  color: item.name == "已处理" ? "#AEE4F8" : "#FF754C",
                },
              };
            }),
          },
        ],
      };
      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("quality-chart"));
      });
    },
    getBaseData() {
      getBaseData().then((res) => {
        if (res.status == 200) {
          this.screenDetail = res.data;

          this.$nextTick(() => {
            const element = document.getElementById("table-scroll");
            this.autoScroll(1, 50, 1500, element);
          });
        }
      });

      getDeviceStatus().then((res) => {
        if (res.status == 200) {
          this.deviceStatus = res.data;
        }
      });
      getProgressDetail().then((res) => {
        if (res.status == 200) {
          this.progressDetail = res.data;
          // if (res.data.progress_data.length > 6) {
          //   document.getElementById("step-scroll").style.width =
          //     2 * res.data.progress_data.length + "rem";
          //   document.getElementById("step-scroll").style.animationDuration =
          //     res.data.progress_data.length / 2 + "s";
          // }
          this.$nextTick(() => {
            const lines = document.getElementsByClassName(
              "el-step__line-inner"
            );
            if (lines.length > 2) {
              lines[this.progressDetail.step - 1].style.width =
                res.data.current_progress_rate + "%";

              lines[this.progressDetail.step - 1].style.borderWidth = "1px";
            }
          });
        }
      });
      getAIWarningData().then((res) => {
        this.aiwarningData = res.data;
        this.renderQualityChart();
      });
    },
    mouseLeaveEvent() {},
    mouseOverEvent() {},
    autoScroll(stepLength, speed, delay, element) {
      let interval;
      //let step = 1
      element.scrollTop = 0;
      function start() {
        interval = setInterval(scrolling, speed);
        element.scrollTop += stepLength;
      }
      function scrolling() {
        var sTop = element.scrollTop;
        element.scrollTop += stepLength;
        if (
          sTop === element.scrollTop ||
          sTop == 0 ||
          element.scrollTop === element.scrollHeight - element.offsetHeight
        ) {
          stepLength *= -1; // 转换方向
          clearInterval(interval);
          setTimeout(start, delay);
        }
      }
      if (element.offsetHeight !== element.scrollHeight) {
        // 元素内容没有溢出时，不触发
        setTimeout(start, delay);
      }
    },
  },
  mounted() {
    this.getBaseData();
  },
};
</script>
<style scoped lang="less">
@import "@/assets/common/element.less";
#construction {
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  height: 100%;
  padding-bottom: 0;
}
.side-area {
  width: 25%;
}
.mid-area {
  width: calc(50% - 40px);
  margin: 0 20px;
}
.divider {
  display: flex;
  justify-content: space-between;
  align-items: stretch;
  margin-bottom: 12px;
  .divider-line {
    width: calc(50% - 46px);
    border-top: 1px solid #f2af58;
    transform: translateY(50%);
  }
  .divider-text {
    text-align: center;
    width: 80px;
    font-size: 16px;

    font-weight: 400;
    color: #27acfb;
  }
}
.construction-name {
  font-size: 20px;
  font-family: Source Han Sans CN;
  font-weight: 500;
  font-style: italic;
  color: #ffffff;
  text-align: center;
  margin: 20px 0;
  margin-top: 0;
}
.construction-details {
  display: flex;
  justify-content: space-between;
  text-align: center;
  margin-bottom: 28px;
  position: relative;
  padding: 24px 0;
  background: url("@/assets/icon-short-bg.png") no-repeat;
  background-size: 100% 100%;
  img {
    position: absolute;
    width: 100%;
    vertical-align: top;
    left: 0;
    top: 0;
    z-index: -1;
  }
  .construction-detail {
    width: calc(100% / 3);
  }
  .data {
    font-size: 24px;

    font-weight: bold;
    color: #ffaf51;
    line-height: 24px;
    margin-bottom: 12px;
    &.unrecord {
      opacity: 0.2;
    }
    &.small {
      font-size: 12px;
      line-height: 12px;
    }
  }
  .text {
    font-size: 14px;

    font-weight: 400;
    color: #27acfb;
  }
}
.unit {
  line-height: 15px;
  display: flex;
  justify-content: flex-start;
  &:last-child {
    margin-bottom: 0;
  }
  .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #a3a1fb;
    margin-right: 6px;
    display: inline-block;
    vertical-align: top;
    transform: translateY(2px);
  }
}
table {
  width: 100%;
  border: 1px solid #097ca7;
  border-collapse: collapse;
  th,
  td {
    border: 1px solid #097ca7;
    padding: 20px;
  }
  .unit-text {
    font-size: 14px;

    font-weight: 400;
    color: #22c3ed;
    margin-right: 14px;
  }
  .unit-name {
    font-size: 14px;

    font-weight: 400;
    color: #fefefe;
    margin-bottom: 4px;
  }
  .unit-contact {
    font-size: 12px;

    font-weight: 400;
    color: #99a9d0;
  }
}
.chart-container {
  position: relative;
  .custom-chart {
    position: absolute;
    width: 20%;
    left: 50%;
    transform: translateX(-50%);
    top: 18%;
  }
}
#security-chart,
#quality-chart {
  width: 100%;
  height: 220px;
}
.steps {
  padding-top: 40px;
  overflow-x: hidden;
}
.step-title {
  position: relative;
  color: #22c3ed;
  font-size: 12px;

  .step-status {
    position: absolute;
    top: -170%;
    color: #18d7b9;
    font-size: 12px;
  }
}
.step-description {
  font-size: 14px;
  color: #fff;
}

.progress-contains {
  padding: 8px 0;
}
.progress {
  position: relative;
  width: 100%;
  img {
    width: 100%;
  }
  .progress-container {
    width: 100%;
    background: url("@/assets/icon-long-bg.png") no-repeat;
    background-size: 100% 100%;
    display: flex;
    justify-content: space-between;
    .progress-day {
      width: 50%;
      text-align: center;
      padding: 12px;
    }
    .progress-count {
      font-size: 30px;

      font-weight: bold;
      color: #22c2ec;
    }
    .progress-text {
      font-size: 14px;

      font-weight: 400;
      color: #27acfb;
    }
  }
}
.swagger-container {
  position: relative;
  .container-img {
    width: 100%;
    height: 100%;
    vertical-align: top;
  }
  .lt,
  .lb,
  .rb,
  .rt {
    position: absolute;
    width: 20px;
    height: 20px;
  }
  .lt {
    top: -2px;
    left: -2px;
  }
  .lb {
    left: -2px;
    bottom: -2px;
  }
  .rb {
    right: -2px;
    bottom: -2px;
  }
  .rt {
    right: -2px;
    top: -2px;
  }
}
.status {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  height: 528px;
  overflow-y: auto;
  .status-count {
    width: calc(100% / 3);
    padding: 30px 0px;
    .status-img {
      width: 40%;
    }
    .status-detail {
      display: inline-block;
      vertical-align: top;
      .status-text {
        font-size: 14px;
        font-weight: 400;
        color: #ffffff;
        padding-top: 12px;
      }
      .status-number {
        font-weight: 400;
        color: #25d2ff;
        opacity: 0.7;
        font-size: 22px;
        span {
          font-size: 12px;
        }
      }
    }
  }
}
.unit-scroll {
  max-height: 160px;
  overflow: scroll;
}
.construction-status {
  min-height: 380px;
}
.table-label {
  font-size: 14px;
  color: #22c3ed;
}
.table-unit {
  margin-bottom: 12px;
  &:last-child {
    margin-bottom: 0;
  }
}
.table-scroll {
  max-height: 366px;
  overflow-y: auto;
}
.check-in {
  width: 100%;
  display: flex;
  justify-content: space-between;
  .checks {
    width: 50%;
    text-align: center;
    &:first-child {
      border-right: 1px solid rgba(9, 124, 167, 0.5);
    }
    img {
      width: 80%;
      margin: 0 auto;
      vertical-align: top;
    }
    .title {
      font-size: 12px;
      color: rgba(225, 239, 255, 0.7);
      margin-bottom: 20px;
    }
    .count {
      font-size: 40px;
      span {
        font-size: 14px;
      }
    }
  }
}

#step-scroll {
  margin-top: 32px;
}
</style>