<template>
  <div id="construction">
    <div class="side-area">
      <Title text="项目概况"></Title>
      <ShadowContainer class="long-container">
        <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 class="short-container">
        <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 class="long-container">
        <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="100%">
            <el-carousel-item
              v-for="(item, index) in screenDetail.banner_image_arr"
              :key="index"
            >
              <!-- <img class="container-img" :src="item.url" alt="" /> -->
              <el-image
                style="width: 100%; height: 100%"
                :src="item.url"
                fit="cover"
              ></el-image>
            </el-carousel-item>
          </el-carousel>
        </div>
      </ShadowContainer>
      <Title text="工程进度" type="long"></Title>
      <ShadowContainer class="short-container">
        <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 class="long-container">
        <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 class="short-container">
        <div style="display: flex">
          <div class="chart-container">
            <div ref="qualityChart" id="quality-chart"></div>
            <div class="title">
              <div class="main-title">
                {{ AIwarningTotal }}
              </div>
              <div class="sub-title">AI预警</div>
            </div>
          </div>

          <div class="chart-legends">
            <div
              v-for="(legend, index) in aiwarningData"
              :key="index"
              :style="{
                color: legend.name == '已处理' ? '#AEE4F8' : '#FF754C',
              }"
            >
              <span
                class="rect"
                :style="{
                  background: legend.name == '已处理' ? '#AEE4F8' : '#FF754C',
                }"
              ></span>
              {{ legend.name }} {{ legend.value }}
            </div>
          </div>
        </div>
      </ShadowContainer>
    </div>
  </div>
</template>
<script>
import {
  getBaseData,
  getDeviceStatus,
  getProgressDetail,
  getAIWarningData,
} from "@/api/index";
export default {
  name: "Construction",
  data() {
    return {
      constructionUnits: [],
      personData: [],
      active: 4,
      deviceStatus: [],
      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: [],
      AIwarningTotal: 0,
    };
  },
  methods: {
    renderQualityChart() {
      let total = 0;
      this.aiwarningData.map((item) => {
        total += item.value;
      });
      this.AIwarningTotal = total;
      const chart = this.$echarts.init(this.$refs.qualityChart);
      const option = {
        tooltip: {
          trigger: "item",
        },
        series: [
          {
            type: "pie",
            radius: ["58%", "70%"],
            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: calc(100% - 12px);
  padding-bottom: 0;
}
.side-area {
  width: 400px;
  height: 100%;
}
.mid-area {
  width: calc(100% - 824px);
  height: 100%;
}
.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: 14px;
  }
  .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;
  width: 200px;
  height: 200px;
  .title {
    position: absolute;
    top: 50%;
    left: 50%;
    text-align: center;
    transform: translateX(-50%) translateY(-50%);
    .main-title {
      font-size: 30px;
      font-weight: bold;
      color: #00fcff;
    }
    .sub-title {
      font-size: 16px;
      font-weight: bold;
      color: rgba(255, 255, 255, 0.7);
    }
  }
}
#quality-chart {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  background: url("@/assets/chart.png") no-repeat;
  background-size: 50% 50%;
  background-position: center center;
}
.steps {
  padding-top: 40px;
  overflow-x: hidden;
}
.step-title {
  position: relative;
  color: #22c3ed;
  font-size: 12px;

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

.progress-contains {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  height: 100%;
}
.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;
  height: 100%;
  .container-img {
    width: 100%;
    vertical-align: top;
    object-fit: cover;
  }
  .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;

  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;
}

.table-label {
  font-size: 14px;
  color: #22c3ed;
}
.table-unit {
  margin-bottom: 12px;
  &:last-child {
    margin-bottom: 0;
  }
}
.table-scroll {
  height: calc(100% - 166px);
  overflow-y: auto;
}
.check-in {
  width: 100%;
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  height: 100%;
  .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;
  width: 100%;
}
.long-container {
  height: calc(66% - 108px);
}
.short-container {
  height: calc(33% - 98px);
}
::v-deep .el-carousel {
  height: 100%;
}
.construction-status {
  height: 100%;
}
.chart-legends {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  width: calc(100% - 200px);
  & > div {
    padding: 0 20px;
    font-size: 14px;
    line-height: 36px;
    width: 100%;
    text-align: left;
  }
  .rect {
    width: 14px;
    height: 14px;
    display: inline-block;
    vertical-align: middle;
  }
}
</style>