<template>
  <div class="navbar">
    <hamburger
      id="hamburger-container"
      :is-active="sidebar.opened"
      class="hamburger-container"
      @toggleClick="toggleSideBar"
    />

    <breadcrumb
      id="breadcrumb-container"
      class="breadcrumb-container"
      v-if="!topNav"
    />
    <top-nav id="topmenu-container" class="topmenu-container" v-if="topNav" />

    <div class="right-menu">
      <template v-if="device !== 'mobile'">
        <div class="menu-return right-menu-item">
          <i class="el-icon-data-analysis" @click="goBigScreen"></i>
        </div>
        <!-- <search id="header-search" class="right-menu-item" /> -->
        <div class="header-news">
          <el-popover
            placement="bottom-end"
            width="400"
            popper-class="screen-message-popover"
            trigger="hover"
          >
            <div class="screen-message-content">
              <div class="message-content">
                <fssm-scroll style="max-height: 12rem">
                  <div
                    class="message-item"
                    v-for="(item, index) in messageList"
                    :key="`message-item-${index}`"
                  >
                    <div
                      class="item-top"
                      @click="handleMessageItemClick(item.notificationType)"
                    >
                      <div class="index">
                        <!-- <i v-if="item.title === '成功'" style="color: #67c23a" class="el-icon-success"></i> -->
                        <i
                          v-if="item.notificationType === 'REMIND'"
                          style="color: #e6a23c"
                          class="el-icon-info"
                        ></i>
                        <i
                          v-if="item.notificationType === 'WARNING'"
                          style="color: #909399"
                          class="el-icon-warning"
                        ></i>
                        <!-- <i v-if="item.title === '错误'" style="color: #f56c6c" class="el-icon-error"></i> -->
                      </div>
                      <div class="time">
                        <span>{{ item.createdTime }}</span>
                      </div>
                      <div class="carNo">
                        <span>{{ item.title }}</span>
                      </div>
                    </div>
                    <div class="item-content">
                      {{ item.content }}
                    </div>
                  </div>
                </fssm-scroll>
              </div>
              <div class="message-footer">
                <el-button
                  @click="handleMessageClick"
                  icon="el-icon-d-arrow-right"
                  type="text"
                  >查看更多</el-button
                >
              </div>
            </div>
            <el-badge
              slot="reference"
              :value="noticeNum"
              :max="99"
              class="item"
            >
              <i class="el-icon-bell">
                <!-- <div class="num">{{ noticeNum }}</div> -->
              </i>
            </el-badge>
          </el-popover>
        </div>
        <screenfull id="screenfull" class="right-menu-item hover-effect" />

        <!-- <el-tooltip content="布局大小" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip> -->
      </template>

      <el-dropdown
        class="avatar-container right-menu-item hover-effect"
        trigger="click"
      >
        <div class="avatar-wrapper">
          <img :src="avatar" class="user-avatar" />
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/user/profile">
            <el-dropdown-item>个人中心</el-dropdown-item>
          </router-link>
          <el-dropdown-item @click.native="setting = true">
            <span>布局设置</span>
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout">
            <span>退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Breadcrumb from "@/components/Breadcrumb";
import TopNav from "@/components/TopNav";
import Hamburger from "@/components/Hamburger";
import Screenfull from "@/components/Screenfull";
import SizeSelect from "@/components/SizeSelect";
import Search from "@/components/HeaderSearch";
import RuoYiGit from "@/components/RuoYi/Git";
import RuoYiDoc from "@/components/RuoYi/Doc";
import FssmScroll from "@/components/scroll/fssm-scroll.vue";
// 接口
import { getNoticeList } from "@/api/xj/screen/index";
export default {
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search,
    RuoYiGit,
    RuoYiDoc,
    FssmScroll,
  },
  computed: {
    ...mapGetters(["sidebar", "avatar", "device"]),
    setting: {
      get() {
        return this.$store.state.settings.showSettings;
      },
      set(val) {
        this.$store.dispatch("settings/changeSetting", {
          key: "showSettings",
          value: val,
        });
      },
    },
    topNav: {
      get() {
        return this.$store.state.settings.topNav;
      },
    },
  },
  data() {
    return {
      // 预警中心数据条数
      noticeNum: 0,
      // 预警中心消息数据
      messageList: [],
      // 预警中心定时器
      messageTimer: null,
    };
  },
  mounted() {
    this.getMessageList();
  },
  methods: {
    /* 点击跳转大屏页面 */
    goBigScreen() {
      this.$router.push("/screen");
    },
    toggleSideBar() {
      this.$store.dispatch("app/toggleSideBar");
    },
    async logout() {
      this.$confirm("确定注销并退出系统吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store.dispatch("LogOut").then(() => {
            location.href = "/index";
          });
        })
        .catch(() => {});
    },

    /**
     * @description: 获取消息中心数据
     * @return {void}
     */
    getMessageList() {
      this.messageTimer = setInterval(() => {
        getNoticeList({ read: 1 }).then(({ code, rows, total }) => {
          if (code === 200) {
            this.messageList = rows;
            this.noticeNum = total;
          }
        });
      }, 5000);
      getNoticeList({ read: 1 }).then(({ code, rows, total }) => {
        if (code === 200) {
          this.messageList = rows;
          this.noticeNum = total;
        }
      });
    },

    /**
     * @description: 消息中心查看更多事件
     * @return {*}
     */
    handleMessageClick() {
      this.$router.push("/inspection_warn/warning-center");
    },

    /**
     * @description: 消息中心点击具体数据事件
     * @return {*}
     */
    handleMessageItemClick(value) {
      this.$router.push({
        name: "Warning-center",
        params: { title: value },
      });
    },
  },
  beforeDestroy() {
    clearInterval(this.messageTimer);
  },
};
</script>

<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .topmenu-container {
    position: absolute;
    left: 50px;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    height: 100%;
    display: flex;
    float: right;
    line-height: 50px;

    &:focus {
      outline: none;
    }

    .menu-return,
    .header-news {
      height: 100%;
      padding: 0 8px;

      i {
        cursor: pointer;
        font-size: 18px;
        font-weight: bold;
        color: #000000;
      }
    }

    // 消息中心样式
    .header-news {
      i {
        position: relative;
        cursor: pointer;
        font-size: 18px;

        // .num {
        //   min-width: 0.9rem;
        //   position: absolute;
        //   top: -0.2rem;
        //   right: -0.5rem;
        //   padding: 0.2rem;
        //   font-size: 0.6rem;
        //   border-radius: 0.5rem;
        //   color: #ffffff;
        //   background-color: red;
        // }
      }

      ::v-deep .el-badge__content {
        top: 15px !important;
      }
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>

<style lang="scss">
// 大屏消息弹出框样式
.screen-message-popover {
  background-color: rgba(0, 0, 0, 0.8);
  border: none;

  .screen-message-content {
    width: 100%;
    color: #ffffff;

    .message-content {
      width: 100%;
      max-height: 12rem;

      .message-item {
        padding: 0.5rem 0;
        border-bottom: 1px dashed rgb(115, 115, 116);

        .item-top {
          width: 100%;
          display: flex;

          &:hover {
            span {
              color: rgb(113, 179, 255);
            }
          }

          .time {
            width: 40%;
          }
        }

        .item-content {
          padding-left: 1rem;
        }
      }
    }

    .message-footer {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
