<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-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-header">
            <div class="warning-table-time">报警时间</div>
            <div class="warning-table-device">设备</div>
            <div class="warning-table-message">报警信息</div>
            <div class="warning-table-status">处理状态</div>
          </div>
          <div
            class="warning-table-list"
            v-infinite-scroll="load"
            :infinite-scroll-distance="1"
            id="table-scroll"
          >
            <div
              class="warning-table-column"
              v-for="(column, index) in elevatorWarningList"
              :key="index"
            >
              <div class="warning-table-time">{{ column.log_time }}</div>
              <div class="warning-table-device">{{ column.device_no }}</div>
              <el-tooltip
                class="item"
                effect="dark"
                :content="column.alarm_content"
                placement="top"
              >
                <div class="warning-table-message">
                  {{ column.alarm_content }}
                </div>
              </el-tooltip>
              <div
                class="warning-table-status"
                :style="{ color: column.status == 1 ? '#FFC74C' : '' }"
              >
                {{ column.status_text }}
              </div>
            </div>
          </div>
        </div>
      </ShadowContainer>
    </div>
    <div class="elevator-container">
      <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" 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">
                  {{ 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">
                    {{ 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" 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>
  </div>
</template>
<script>
import {
  getElevatorList,
  getElevatorWarningList,
  getElevatorWarningStatus,
  getElevatorRealData,
} from "@/api/index";
export default {
  name: "Elevator",
  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,
    };
  },
  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) {
      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
        ) {
          element.scrollTop = 0;
          clearInterval(interval);
          setTimeout(start, delay);
        }
      }
      if (element.offsetHeight !== element.scrollHeight) {
        // 元素内容没有溢出时，不触发
        setTimeout(start, delay);
      }
    },
    getCurrentElevatorStatus(tower) {
      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.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);
    },
  },
  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>
#elevator {
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  padding-bottom: 0;
  height: 100%;
}
.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(99, 226, 253, 0.6),
      0 0 5px rgba(99, 226, 253, 0.6), 0 0 6px rgba(99, 226, 253, 0.6),
      0 0 40px rgba(99, 226, 253, 0.6);
  }
  .warning-info-num {
    font-size: 24px;
    color: #ff6151;
  }
  .warning-info-img {
    position: relative;
    transform: translateY(-40px);
    margin-bottom: -40px;
    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: calc(100% - 50px);
    overflow-y: auto;
  }
  .warning-table-header {
    background: rgba(44, 96, 162, 0.2);
    display: flex;
    padding: 12px 0;
  }
  .warning-table-column {
    padding: 12px 0;
    background: rgba(44, 96, 162, 0.05);
    display: flex;
    &:nth-child(2n) {
      background: rgba(44, 96, 162, 0.1);
    }
  }
  .warning-table-time {
    width: 40%;
  }
  .warning-table-device,
  .warning-table-message,
  .warning-table-status {
    width: 20%;
  }
  .warning-table-message {
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }
}
#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(50% - 112px);
}
#elevator-detail {
  height: 100%;
}
</style>