<template>
  <div class="actualEvent">
    <div class="title">
      <div class="title_left">
        <div class="icon"></div>
        <p>实时事件</p>
      </div>
      <img src="@/assets/index/more_icon.png" alt="" @click="moreEvent" />
    </div>
    <div class="actualTab">
      <p
        v-for="(item, index) of tab"
        :key="index"
        @click="changeTab(item.num)"
        :class="{ active: page.cur === item.num }"
      >
        {{ item.name }}
      </p>
    </div>
    <div class="contentWapper" id="conWapper">
      <div
        v-for="(item, index) of content"
        :key="index"
        v-show="page.cur == index"
      >
        <div v-for="(con, i) in item" :key="i">
          <div class="conList">
            <div class="eventcode">
              <img src="@/assets/index/event_code.png" alt="" />
              <p class="">{{ con.id }}</p>
              <div
                class="status"
                :style="{
                  background: con.operateBackColor,
                  color: con.operateColor,
                }"
              >
                {{ con.operateStatusName }}
              </div>
            </div>
            <div class="itemWrapper">
              <div>
                <div class="cell">
                  <p style="width: 60px">发生时间：</p>
                  <p style="width: 152px">{{ con.createTime }}</p>
                </div>
                <div class="cell">
                  <p style="width: 60px">事件编号：</p>
                  <p style="width: 152px">{{ con.id }}</p>
                </div>
                <div class="cell">
                  <p style="width: 60px">事件类型：</p>
                  <p style="width: 152px">
                    {{
                      con.kindName
                        ? con.kindName
                        : con.kind == 9
                        ? "未出水"
                        : con.kind == 10
                        ? "绕行未清洗"
                        : con.kind == 11
                        ? "清洗时长不足"
                        : con.kind == 12
                        ? "黑车"
                        : con.kind == 13
                        ? "后盖异常"
                        : con.kind == 14
                        ? "河上垃圾堆放"
                        : con.kind == 15
                        ? "游泳检测"
                        : con.kind == 16
                        ? "钓鱼检测"
                        : con.kind == 17
                        ? "漂浮物"
                        : con.kind == 18
                        ? "渔船"
                        : con.kind == 19
                        ? "垂钓者"
                        : "-"
                    }}
                  </p>
                </div>
                <div class="cell">
                  <p style="width: 60px">发生位置：</p>
                  <p
                    @click="click(con)"
                    style="width: 152px; color: #ff9e48; cursor: pointer"
                  >
                    {{ con.cameraAddress }}
                  </p>
                </div>
              </div>
              <el-image
                style="width: 110px; height: 80px; margin-left: 14px"
                :src="con.picture"
                :preview-src-list="[con.picture]"
              >
              </el-image>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  name: "ActualEvent",
  data() {
    return {
      tab: [{ name: "全部", num: 0 }, { name: "待派发", num: 1 }, { name: "待认领", num: 2 }, { name: "待处理", num: 3 }, { name: "待审核", num: 4 }, { name: "已结束", num: 5 }],
      content: [],
      page: {
        currentPage: 1,
        size: 10,
        cur: 0,
      },
      allData: [],
      first: false, // 如果是第一次加载不监听触底
      timer: null,
      front: false,
      map: null,
    };
  },
  props: {
    eventList: {
      type: Array,
      default: () => {
        [];
      },
    },
    typeId: {
      type: Number
    }
  },
  created() {
    let _this = this;
    this.timer = window.setInterval(() => {
      _this.front = true;
      _this.$emit("eventStatus", _this.page, 1);
    }, 60 * 1000 * 5);
    console.log('------', JSON.parse(localStorage.getItem('accountInfo')).level)
    // if (JSON.parse(localStorage.getItem('accountInfo')).level == 2 || (JSON.parse(localStorage.getItem('accountInfo')).level == 0 && JSON.parse(localStorage.getItem('typeId')) == 4)) {
    //   this.tab = [{ name: "全部", num: 0 }, { name: "待处理", num: 3 }, { name: "已结束", num: 5 }]
    // } else {
    //   this.tab = [{ name: "全部", num: 0 }, { name: "待派发", num: 1 }, { name: "待认领", num: 2 }, { name: "待处理", num: 3 }, { name: "待审核", num: 4 }, { name: "已结束", num: 5 }]
    // }
  },
  mounted() {
    this.checkDivScroolTop();
  },
  watch: {
    eventList: {
      immediate: true,
      deep: true,
      handler(val) {
        console.log(this.front)
        val.forEach((item) => {
          // 请求到10条数据
          if (this.front === true) {
            // 通过轮询收到新数据要放到数据前面，先合并
            this.allData.unshift(item);
            // 还要去重this.allData和item
            for (let i = this.allData.length - 1; i >= 0; i--) {
              for (let j = i - 1; j >= 0; j--) {
                if (this.allData[i].id === this.allData[j].id) {
                  this.allData.splice(j, 1);
                  this.front = false;
                }
              }
            }
          } else {
            this.allData.push(item);
            console.log(this.allData)
          }
        });
        this.$set(this.content, this.page.cur, this.allData);
        this.first = false;
        this.$emit("getMapData", this.content[this.page.cur]);
      },
    },
  },
  methods: {
    changeTab(index) {
      // 切换标签,做初始化工作
      this.allData = [];
      this.page.currentPage = 1;
      this.first = true;
      const el = document.querySelector("#conWapper");
      el.scrollTop = 0;
      this.page.cur = index;
      this.$emit("eventStatus", this.page, 1);
    },
    // 跳转地图详情
    click(val) {
      console.log("当前详情val----", val);
      this.$emit("marckClick", val);
    },
    moreEvent() {
      console.log(this.typeId);
      // this.$router.push("/event");
      this.$router.push({
        name: "event",
        query: this.typeId,
      });
    },
    checkDivScroolTop() {
      this.$nextTick(() => {
        const el = document.querySelector("#conWapper");
        const offsetHeight = el.offsetHeight;
        el.onscroll = () => {
          const scrollTop = el.scrollTop;
          const scrollHeight = el.scrollHeight;
          if (this.first === false) {
            if (offsetHeight + scrollTop - scrollHeight >= 0) {
              let index = this.page.currentPage++;
              console.log(index,'index---')
              this.$set(this.page, "currentPage", index == 1 ? index + 1 : index);
              this.$emit("eventStatus", this.page);
            }
          }
        };
      });
    },
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
};
</script>

<style lang="stylus" scoped>
.actualEvent {
  width: 384px;
  height: 360px;
  background: linear-gradient(180deg, rgba(10, 29, 72, 0.51) 0%, #0F152E 100%);
  border-radius: 4px;
  border: 2px solid #00356D;

  .title {
    height: 40px;
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(270deg, #072B4D 0%, #134778 100%);

    .title_left {
      display: flex;
      align-items: center;

      .icon {
        width: 2px;
        height: 20px;
        background: #5CE3FB;
        border-radius: 2px;
      }

      p {
        margin-left: 10px;
        font-size: 20px;
        font-weight: 400;
        color: #5CE3FB;
      }
    }

    img {
      cursor: pointer;
    }
  }

  .actualTab {
    margin: 12px 0;
    padding: 0 10px;
    display: flex;
    align-items: center;
    justify-content: space-around;

    p {
      width: 50px;
      height: 20px;
      background: #123154;
      border-radius: 10px;
      line-height: 20px;
      text-align: center;
      font-size: 12px;
      font-weight: 500;
      color: #6A8EB7;
      cursor: pointer;
    }

    .active {
      background: #54E3FD;
      border: 1px solid #5CE3FB;
      color: #030303;
    }
  }

  .contentWapper {
    padding-left: 10px;
    margin-right: 5px;
    height: 260px;
    overflow-y: auto;

    .conList {
      width: 356px;
      margin-bottom: 10px;
      background: linear-gradient(180deg, #072B4D 0%, #000D29 100%);
      border-radius: 4px;

      .eventcode {
        position: relative;
        height: 36px;
        display: flex;
        align-items: center;
        background: linear-gradient(90deg, #072B4D 0%, #072B4E 100%);

        p {
          margin-left: 10px;
          font-size: 16px;
          font-weight: 500;
          color: #FFFFFF;
        }

        .status {
          position: absolute;
          right: 12px;
          top: 8px;
          width: 68px;
          height: 20px;
          border-radius: 10px;
          line-height: 20px;
          text-align: center;
          font-size: 12px;
          font-weight: 500;
        }
      }

      .itemWrapper {
        padding: 10px;
        display: flex;
        align-items: center;

        .cell {
          max-width: 212px;
          font-size: 12px;
          color: #FFFFFF;
          line-height: 24px;
          display: flex;
        }
      }
    }
  }
}

::-webkit-scrollbar {
  width: 3px !important;
  /* 滚动条宽度 */
  height: 3px !important;
  /* 滚动条高度 */
  background-color: #123154;
}

::-webkit-scrollbar-thumb {
  background-color: #54A7E7 !important;
  border-radius: 8px;
}
</style>
