<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="right-menu-item" v-if="total>0" @click="$router.push('/emergency/emergency')">-->
        <!--<img src="@/assets/xiaoxi.png" :hidden="xiaohidden" style="height: 40px;width: 40px;margin-top: 5px;cursor: pointer;">-->
        <!--</div>-->
        <!-- <search id="header-search" class="right-menu-item" /> -->
        <!--          <el-badge :value="total" :max="99" class="item" >-->
        <!--            <i class="el-icon-chat-dot-round" style="width: 10px;height: 10px;" ></i>-->
        <!--          </el-badge>-->

        <el-dropdown
          class="avatar-container right-menu-item hover-effect"
          style="margin-right: 0px"
          trigger="click"
        >
          <div class="avatar-wrapper">
            <el-badge :value="total" :max="99" class="item">
                 <i
                class="el-icon-chat-dot-round"
                style="width: 10px; height: 10px"
              ></i>
               </el-badge
            >
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-if="total == 0">
              <span>暂无消息</span>
            </el-dropdown-item>
            <div v-for="(item, index) in receivedList">
              <el-dropdown-item
                v-if="index == 0"
                @click.native="openMassage(item.id, item.eventName)"
              >
                <span>您有新消息：{{ item.eventName }}</span>
              </el-dropdown-item>
              <el-dropdown-item
                v-if="index > 0"
                divided
                @click.native="openMassage(item.id, item.eventName)"
              >
                <span>您有新消息：{{ item.eventName }}</span>
              </el-dropdown-item>
            </div>
          </el-dropdown-menu>
        </el-dropdown>
        <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, mapMutations } 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 { getUserProfile } from "@/api/system/user";
import { listReceive } from "@/api/system/receive";
export default {
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search,
  },
  data() {
    return {
      userType: -2,
      total: null,
      xiaohidden: false,
      receivedList: [],
      routerPath: "",
    };
  },
  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;
      },
    },
  },
  mounted() {
    getUserProfile().then((response) => {
      this.userType = response.data.deptId;
      if (this.userType != -2) {
        this.queryParams.enterpriseId = response.data.deptId;
      }
      this.getList();
    });
    this.socket();
  },
  methods: {
    ...mapMutations({
      SET_EMERGENCY: "bigWindowCompany/SET_EMERGENCY",
    }),

    toggleSideBar() {
      this.$store.dispatch("app/toggleSideBar");
    },
    async logout() {
      this.$confirm("确定注销并退出系统吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store.dispatch("LogOut").then(() => {
            location.href = "/index";
          });
        })
        .catch(() => {});
    },
    openMassage(id, title) {
      //this.$router.push('/emergency/emergency?eventid='+id);
      this.routerPath = "/emergency/emergency";
      if (window.location.pathname == this.routerPath) {
        // this.$parent.showDetail(id,title);
        this.SET_EMERGENCY({
          eventId: id,
          eventName: title,
        });
      } else {
        this.$router.push({
          path: this.routerPath,
          query: { eventId: id, eventName: title },
        });
      }
    },
    socket() {
      console.log("socket执行");
      this.ws = new WebSocket(
        "ws://222.223.203.154:8902/gassafety/websocketServer"
      );
      this.ws.onopen = (evt) => {
        console.log("WebSockets开启");
      };
      this.ws.onmessage = (evt) => {
        console.log("推送", evt);
        const obj = JSON.parse(evt.data);
        console.log("接受socketobj", obj);
        this.getList();
      };
      this.ws.onclose = () => {
        console.log("ws协议关闭");
      };
    },
    getList() {
      console.log(this.userType);
      var params = {
        pageNum: 1,
        pageSize: 100,
        status: 5,
        enterpriseId: this.userType,
      };
      if (this.userType == -2) {
        params.governmentRead = 0;
        params.enterpriseId = null;
      } else {
        params.companyRead = 0;
      }
      listReceive(params).then((response) => {
        this.receivedList = response.rows;
        if (response.total != 0) {
          this.total = response.total;
        } else {
          this.total = "";
        }
      });
    },
  },
};
</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 {
    float: right;
    height: 100%;
    line-height: 50px;
    &:focus {
      outline: none;
    }

    .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>
