<template>
  <div id="elevator">
    <div class="side-area">
      <Title text="设备列表"></Title>
      <ShadowContainer class="short-container">
        <div id="elevator-description">
          <div
            :class="['elevator', activeElevatorIndex == index ? 'active' : '']"
            v-for="(elevator, index) in elevatorList"
            :key="index"
            @click="selectElevator(elevator, index)"
          >
            <div class="elevator-title">
              <i class="iconfont icon-shigongshengjiangji"></i>
              {{ elevator.name }}
              <span class="status">
                <span
                  v-if="elevator.left_elevator"
                  :style="{
                    color:
                      elevator.left_elevator.is_online == 1 ? '#26D591' : '',
                  }"
                >
                  (左){{ elevator.left_elevator.is_online_text }}
                </span>
                <span v-if="elevator.left_elevator && elevator.right_elevator">
                  /
                </span>
                <span
                  v-if="elevator.right_elevator"
                  :style="{
                    color:
                      elevator.right_elevator.is_online == 1 ? '#26D591' : '',
                  }"
                >
                  (右){{ elevator.right_elevator.is_online_text }}
                </span>
              </span>
            </div>
          </div>
        </div>
      </ShadowContainer>
      <Title text="报警信息"></Title>
      <ShadowContainer class="short-2-container">
        <div id="warning-info">
          <div>
            <div class="warning-info-text">今日报警</div>
            <div class="warning-info-num">
              {{ elevatorWarningStatus.today_num }}
            </div>
            <div class="warning-info-img">
              <img src="@/assets/fallprotection/today.png" alt="" />
            </div>
          </div>
          <div>
            <div class="warning-info-text">累计未处理报警</div>
            <div class="warning-info-num" style="color: #ffc74c">
              {{ elevatorWarningStatus.un_handle_num }}
            </div>
            <div class="warning-info-img">
              <img src="@/assets/fallprotection/total-no.png" alt="" />
            </div>
          </div>
          <div>
            <div class="warning-info-text">累计履责率</div>
            <div class="warning-info-num" style="color: #22c3ed">
              {{ elevatorWarningStatus.handle_rate
              }}<span style="font-size: 14px; margin-left: 12px">%</span>
            </div>
            <div class="warning-info-img">
              <img src="@/assets/fallprotection/percent.png" alt="" />
            </div>
          </div>
        </div>
        <div class="warning-table">
          <div
            class="warning-table-list"
            v-infinite-scroll="load"
            :infinite-scroll-distance="1"
            id="table-scroll"
            @mouseenter="clearScroll"
            @mouseleave="reScroll"
          >
            <div
              class="warning-table-column"
              v-for="(column, index) in elevatorWarningList"
              :key="index"
              @click="showWarning(column._id)"
            >
              <div class="warning-title">
                <div class="warning-name">
                  <div
                    class="warning-dot"
                    :style="{
                      backgroundColor:
                        column.alarm_level == 1 ? '#FFC74C' : '#FF4C4C',
                    }"
                  ></div>
                  <span class="warning-device">{{ column.device_no }}</span> /
                  <span
                    :style="{
                      color: column.alarm_level == 1 ? '#FFC74C' : '#FF4C4C',
                    }"
                    >{{ column.alarm_level_text }}</span
                  >
                </div>
                <div
                  class="warning-status"
                  :style="{ color: column.status == 1 ? '#FFC74C' : '#22C3ED' }"
                >
                  {{ column.status_text }}
                </div>
              </div>
              <div class="warning-content">
                <el-tooltip
                  class="item"
                  effect="dark"
                  :content="column.alarm_content"
                  placement="top"
                >
                  <div class="warning-message">
                    预警信息：{{ column.alarm_content }}
                  </div>
                </el-tooltip>
                <div class="warning-time">{{ column.log_time }}</div>
              </div>
            </div>
          </div>
        </div>
      </ShadowContainer>
    </div>
    <div
      class="elevator-container"
      v-loading="elevatorLoading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
    >
      <ShadowContainerRect class="long-container">
        <div id="elevator-detail">
          <div class="left-detail">
            <template
              v-if="
                leftElevator &&
                leftElevator.person &&
                leftElevator.device &&
                leftElevator.realtime
              "
            >
              <div class="tower-title">左侧运行情况</div>
              <div class="driver-info">
                <div class="driver-icon">
                  <img
                    :src="
                      leftElevator.person.head_img
                        ? leftElevator.person.head_img
                        : require('@/assets/elevator/portrait.png')
                    "
                    alt=""
                  />
                </div>
                <div class="driver-description">
                  <div
                    class="t-des"
                    v-for="(personData, index) in leftElevator.person.data"
                    :key="index"
                  >
                    {{ personData.title }}：{{ personData.value }}
                  </div>
                </div>
              </div>
              <div class="current-status">
                <div
                  class="t-des"
                  v-for="(currentData, index) in leftElevator.realtime.data"
                  :key="index"
                >
                  {{ currentData.title }}：{{ currentData.value }}
                </div>
              </div>
              <div class="tower-title">设备信息</div>
              <div class="device-info">
                <div
                  class="t-des"
                  v-for="(deviceData, index) in leftElevator.device.data"
                  :key="index"
                >
                  {{ deviceData.title }}：{{ deviceData.value }}
                </div>
              </div>
            </template>
          </div>
          <div class="elevator-tower">
            <div id="tower-main">
              <div
                v-if="leftElevator"
                class="left-elevator"
                :style="{
                  bottom: `${leftBottom}%`,
                }"
              >
                <div
                  class="online-status"
                  v-if="leftElevator"
                  :style="{
                    color: leftElevator.is_online == 1 ? '#26D591' : '',
                  }"
                >
                  {{ leftElevator.is_online_text }}
                </div>
                <div>
                  <img
                    class="elevator"
                    src="@/assets/elevator/ele-left.png"
                    alt=""
                  />
                </div>
              </div>
              <img class="tower" src="@/assets/elevator/tower.png" alt="" />
              <div
                v-if="rightElevator"
                class="right-elevator"
                :style="{
                  bottom: `${rightBottom}%`,
                }"
              >
                <div>
                  <div
                    class="online-status"
                    v-if="rightElevator"
                    :style="{
                      color: rightElevator.is_online == 1 ? '#26D591' : '',
                    }"
                  >
                    {{ rightElevator.is_online_text }}
                  </div>
                  <img
                    class="elevator"
                    src="@/assets/elevator/ele-right.png"
                    alt=""
                  />
                </div>
              </div>
            </div>
            <div>
              <img class="bottom" src="@/assets/elevator/bottom.png" alt="" />
            </div>
          </div>
          <div class="right-detail">
            <template
              v-if="
                rightElevator &&
                rightElevator.person &&
                rightElevator.device &&
                rightElevator.realtime
              "
            >
              <div class="tower-title">右侧运行情况</div>
              <div class="driver-info">
                <div class="driver-icon">
                  <img
                    :src="
                      rightElevator.person.head_img
                        ? rightElevator.person.head_img
                        : require('@/assets/elevator/portrait.png')
                    "
                    alt=""
                  />
                </div>
                <div class="driver-description">
                  <div
                    class="t-des"
                    v-for="(personData, index) in rightElevator.person.data"
                    :key="index"
                  >
                    {{ personData.title }}：{{ personData.value }}
                  </div>
                </div>
              </div>
              <div class="current-status">
                <div
                  class="t-des"
                  v-for="(currentData, index) in rightElevator.realtime.data"
                  :key="index"
                >
                  {{ currentData.title }}：{{ currentData.value }}
                </div>
              </div>
              <div class="tower-title">设备信息</div>
              <div class="device-info">
                <div
                  class="t-des"
                  v-for="(deviceData, index) in rightElevator.device.data"
                  :key="index"
                >
                  {{ deviceData.title }}：{{ deviceData.value }}
                </div>
              </div>
            </template>
          </div>
        </div>
      </ShadowContainerRect>
    </div>

    <warning-dialog
      :data="warningData"
      :visible="warningDialogVisible"
      @changeVisible="(val) => (warningDialogVisible = val)"
    ></warning-dialog>
  </div>
</template>
<script>
import {
  getElevatorList,
  getElevatorWarningList,
  getElevatorWarningStatus,
  getElevatorRealData,
  getWarningLogDetail,
} from "@/api/index";
import WarningDialog from "@/components/earlywarning/WarningDialog";

export default {
  name: "Elevator",
  components: { WarningDialog },
  data() {
    return {
      elevatorList: [],
      page: 1,
      elevatorWarningList: [],
      elevatorWarningStatus: {
        today_num: 0,
        un_handle_num: 0,
        handle_rate: 0,
      },
      activeElevatorIndex: 0,
      leftElevator: {},
      rightElevator: {},
      currentElevatorTower: {},
      leftBottom: 0,
      rightBottom: 0,
      interval1: null,
      elevatorLoading: false,
      warningDialogVisible: false,
      warningData: {},
    };
  },
  methods: {
    load() {
      if (this.elevatorWarningList.length == this.page * 20) {
        getElevatorWarningList({
          page: this.page + 1,
          limit: 20,
        }).then((res) => {
          this.page += 1;
          this.elevatorWarningList = [
            ...this.elevatorWarningList,
            ...res.data.data,
          ];
        });
      } else if (this.elevatorWarningList.length == 0) {
        getElevatorWarningList({ page: 1, limit: 20 }).then((res) => {
          this.page = 1;
          this.elevatorWarningList = res.data.data;
          this.$nextTick(() => {
            const element = document.getElementById("table-scroll");
            this.autoScroll(1, 50, 1500, element);
          });
        });
      }
    },
    autoScroll(stepLength, speed, delay, element) {
      var that = this;
      function start() {
        that.interval1 = 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
        ) {
          element.scrollTop = 0;
          clearInterval(that.interval1);
          that.timeout1 = setTimeout(start, delay);
        }
      }
      if (element.offsetHeight !== element.scrollHeight) {
        // 元素内容没有溢出时，不触发
        that.timeout1 = setTimeout(start, delay);
      }
    },
    clearScroll() {
      clearInterval(this.interval1);
      clearTimeout(this.timeout1);
    },
    reScroll() {
      this.$nextTick(() => {
        const element = document.getElementById("table-scroll");
        this.autoScroll(1, 50, 1500, element);
      });
    },
    getCurrentElevatorStatus(tower) {
      this.elevatorLoading = true;
      this.leftBottom = 0;
      this.rightBottom = 0;
      this.leftElevator = null;
      this.rightElevator = null;
      const elevatorArray = [];
      if (tower.left_elevator) {
        elevatorArray.push(tower.left_elevator.id);
      }
      if (tower.right_elevator) {
        elevatorArray.push(tower.right_elevator.id);
      }
      getElevatorRealData({ device_ids: elevatorArray.join(",") }).then(
        (res) => {
          this.elevatorLoading = false;
          this.leftElevator = tower.left_elevator
            ? {
                ...tower.left_elevator,
                ...res.data.filter((item) => {
                  return item.device_id == tower.left_elevator.id;
                })[0],
              }
            : null;
          this.rightElevator = tower.right_elevator
            ? {
                ...tower.right_elevator,
                ...res.data.filter((item) => {
                  return item.device_id == tower.right_elevator.id;
                })[0],
              }
            : null;
          this.$nextTick(() => {
            setTimeout(() => {
              this.leftBottom = this.leftElevator
                ? (this.leftElevator.realtime.height /
                    this.leftElevator.device.total_height) *
                  100
                : 0;
              this.rightBottom = this.rightElevator
                ? (this.rightElevator.realtime.height /
                    this.rightElevator.device.total_height) *
                  100
                : 0;
            }, 100);
          });
        }
      );
    },
    selectElevator(elevator, index) {
      this.activeElevatorIndex = index;
      this.getCurrentElevatorStatus(elevator);
    },
    showWarning(id) {
      getWarningLogDetail(id).then((res) => {
        this.warningData = res.data;
        this.warningDialogVisible = true;
      });
    },
  },
  mounted() {
    getElevatorList().then((res) => {
      this.elevatorList = res.data;
      this.currentElevatorTower = res.data[0];
      this.getCurrentElevatorStatus(this.currentElevatorTower);
    });
    getElevatorWarningStatus().then((res) => {
      this.elevatorWarningStatus = res.data;
    });
  },
};
</script>
<style lang="less" scoped>
@import "@/assets/common/element.less";
#elevator {
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  padding-bottom: 0;
  height: calc(100% - 12px);
}
.side-area {
  width: 25%;
  min-width: 400px;
}

#elevator-description {
  height: 100%;
  overflow-y: scroll;
  .elevator {
    background: rgba(44, 96, 162, 0.2);
    margin-bottom: 6px;
    &.active {
      background: rgba(44, 96, 162, 0.5);
    }
    &:hover {
      background: rgba(44, 96, 162, 0.5);
    }
  }
  .elevator-title {
    line-height: 22px;
    i {
      font-size: 22px;
    }
    &:hover {
      color: #27acfb;
    }
    img {
      width: 22px;
      vertical-align: top;
    }

    font-size: 14px;
    color: #c6def9;
    padding: 10px 12px;
    cursor: pointer;
    .status {
      float: right;
    }
  }
  .elevator-info {
    line-height: 22px;

    img {
      width: 22px;
      vertical-align: top;
    }
    background: rgba(44, 96, 162, 0.1);
    font-size: 14px;
    color: #c6def9;
    padding: 10px 12px;
    border-top: 1px solid rgba(122, 143, 173, 0.2);
    cursor: pointer;
    span {
      float: right;
    }
  }
}
.elevator-container {
  width: 75%;
  margin-left: 12px;
}
.platform {
  img {
    width: 20px;
    vertical-align: middle;
    margin-right: 6px;
  }
  font-size: 14px;
  color: #c6def9;
  .platform-title {
    background: rgba(44, 96, 162, 0.2);
    border-bottom: 1px solid rgba(122, 143, 173, 0.4);
    line-height: 40px;
    padding: 0 12px;
    .status {
      float: right;
    }
  }
  .platform-info {
    background: rgba(44, 96, 162, 0.1);
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    & > div {
      width: calc(50% - 48px);
      padding: 6px 12px;
    }
  }
  &:hover {
    color: #27acfb;
    .platform-title,
    .platform-info {
      background: rgba(44, 96, 162, 0.4);
    }
  }
}
#warning-info {
  display: flex;
  justify-content: space-between;
  text-align: center;
  & > div {
    width: 33%;
  }
  .warning-info-text {
    white-space: nowrap;
    padding-top: 10px;
    font-size: 16px;
    color: #ffffff;
    line-height: 24px;
    text-shadow: 0 0 5px rgba(31, 129, 239, 0.6),
      0 0 5px rgba(31, 129, 239, 0.6), 0 0 6px rgba(31, 129, 239, 0.6),
      0 0 40px rgba(31, 129, 239, 0.6);
  }
  .warning-info-num {
    font-size: 24px;
    color: #ff6151;
  }
  .warning-info-img {
    position: relative;
    transform: translateY(-60px);
    margin-bottom: -60px;
    img {
      width: 80%;
      margin: 0 auto;
    }
  }
}

.overflowed {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.warning-table {
  font-size: 14px;
  text-align: center;
  color: #c6def9;
  height: calc(100% - 132px);
  .warning-table-list {
    height: 100%;
    overflow-y: auto;
  }
  .warning-table-column {
    margin-bottom: 10px;
    cursor: pointer;
    .warning-title,
    .warning-content {
      display: flex;
      justify-content: space-between;
      line-height: 40px;
      padding: 0 20px;
    }
    .warning-title {
      background: rgba(44, 96, 162, 0.3);
      border-bottom: 1px solid rgba(122, 143, 173, 0.4);
    }
    .warning-content {
      background: rgba(44, 96, 162, 0.1);
    }
    .warning-message {
      text-align: left;
      white-space: nowrap;
      text-overflow: ellipsis;
      width: 50%;
      overflow: hidden;
    }
    .warning-dot {
      border-style: double;
      border-width: 4px;
      width: 10px;
      height: 10px;
      border-radius: 50%;
      background: #fff;
      display: inline-block;
      vertical-align: middle;
      margin-right: 12px;
      border-color: #153a68;
    }
  }
}
#elevator-detail {
  display: flex;
  justify-content: space-between;
  .left-detail,
  .right-detail {
    width: 30%;
    max-width: 360px;
  }

  .elevator-tower {
    width: 40%;
    height: 96%;
    margin-top: 2%;
    text-align: center;
    #tower-main {
      display: inline-block;
      position: relative;
      height: 100%;
      .left-elevator {
        position: absolute;
        left: 0;
        transform: translateX(-100%);
        bottom: 0%;
        transition: all 3s linear;
        .online-status {
          font-size: 18px;
          color: #c6def9;
          text-align: right;
          padding-right: 24px;
        }
      }
      .right-elevator {
        position: absolute;
        right: 0;
        transform: translateX(100%);
        bottom: 0%;
        transition: all 3s linear;

        .online-status {
          font-size: 18px;
          color: #c6def9;
          text-align: left;
          padding-left: 24px;
        }
      }
      .elevator {
        width: 124px;
      }
    }
    img {
      vertical-align: top;
    }
    .bottom {
      height: 10px;
    }
    .tower {
      height: 100%;
    }
  }
  .driver-info {
    display: flex;
    justify-content: space-between;
    .driver-icon {
      width: 100px;
      border: 1px solid #0f6996;
      img {
        width: 100%;
        height: 100%;
        vertical-align: top;
      }
    }
    .driver-description {
      width: calc(100% - 110px);
    }
  }
  .tower-title {
    text-align: center;
    margin-bottom: 16px;
    background: linear-gradient(
      90deg,
      rgba(39, 172, 251, 0.4) 0%,
      rgba(39, 172, 251, 0.15) 50%,
      rgba(39, 172, 251, 0.4) 100%
    );
    border-top: 1px solid
      linear-gradient(
        90deg,
        rgba(34, 195, 237, 0.79) 1%,
        rgba(34, 195, 237, 0.81) 100%
      );
    border-bottom: 1px solid
      linear-gradient(
        90deg,
        rgba(34, 195, 237, 0.79) 1%,
        rgba(34, 195, 237, 0.81) 100%
      );
    font-size: 18px;
    color: #ffffff;
    line-height: 40px;
  }
  .t-des {
    white-space: nowrap;
    background: linear-gradient(
      90deg,
      rgba(39, 172, 251, 0.1) 0%,
      rgba(39, 172, 251, 0) 50%,
      rgba(39, 172, 251, 0.1) 100%
    );
    line-height: 40px;
    color: #c6def9;
    font-size: 14px;
    padding: 0 12px;
    margin-bottom: 2px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .current-status {
    margin: 10px 0 20px 0;
  }
}
.long-container {
  height: calc(100% - 52px);
}
.short-container {
  height: calc(100% / 3 - 112px);
}
.short-2-container {
  height: calc(100% / 3 * 2 - 112px);
}
#elevator-detail {
  height: 100%;
}
.tower {
  width: 60px;
}
</style>