<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'">
        <search id="header-search" class="right-menu-item"/>

        <!--        <el-tooltip content="源码地址" effect="dark" placement="bottom">
                  <ruo-yi-git id="ruoyi-git" class="right-menu-item hover-effect" />
                </el-tooltip>

                <el-tooltip content="文档地址" effect="dark" placement="bottom">
                  <ruo-yi-doc id="ruoyi-doc" class="right-menu-item hover-effect" />
                </el-tooltip>-->


        <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">
          <i class="el-icon-bell  " style="font-size:22px ">
            <el-badge
              :value="Number(hdMsgCount)+Number(msgCount)+Number(msgMaterial1)+Number(msgMaterial2)+Number(msgMaterial3)+Number(msgMaterial4)"
              class="item"/>
          </i>
          <i class="el-icon-caret-bottom"/>
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-show='this.$auth.hasRole("store")' @click.native="goMaterial(msgMaterial1,1)">
            <span>通用应急救援装备 <el-badge class="mark" :value="msgMaterial1"/></span>
          </el-dropdown-item>
          <el-dropdown-item v-show='this.$auth.hasRole("store")' @click.native="goMaterial(msgMaterial2,2)">
            <span>防汛抗旱装备 <el-badge class="mark" :value="msgMaterial2"/></span>
          </el-dropdown-item>
          <el-dropdown-item v-show='this.$auth.hasRole("store")' @click.native="goMaterial(msgMaterial3,3)">
            <span>森林防灭火装备 <el-badge class="mark" :value="msgMaterial3"/></span>
          </el-dropdown-item>
          <el-dropdown-item v-show='this.$auth.hasRole("store")' @click.native="goMaterial(msgMaterial3,4)">
            <span>救灾物资 <el-badge class="mark" :value="msgMaterial4"/></span>
          </el-dropdown-item>
          <el-dropdown-item v-show='this.$auth.hasRole("HDDept") || this.$auth.hasRole("HDAdmin")'
                            @click.native="goHD(hdMsgCount)">
            <span>隐患消息 <el-badge class="mark" :value="hdMsgCount"/></span>
          </el-dropdown-item>
          <el-dropdown-item @click.native="goMsg(msgCount)">
            <span>消息通知</span>
            <el-badge class="mark" :value="msgCount"/>
          </el-dropdown-item>

        </el-dropdown-menu>
      </el-dropdown>

      <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 {setReadByHD, updateEhsMyMessage, getSendQuantity, getUnread, getNoticeMsg} from "@/api/ehs/ehsMyMessage";
  import {getOverTimeId} from "@/api/ehs/ehsHiddenDanger";

  export default {
    data() {
      return {
        hdMsgCount: 0,
        msgCount: 0,
        timer: null,
        msgMaterial1: 0,
        msgMaterial2: 0,
        msgMaterial3: 0,
        msgMaterial4: 0,
      }
    },
    components: {
      Breadcrumb,
      TopNav,
      Hamburger,
      Screenfull,
      SizeSelect,
      Search,
      RuoYiGit,
      RuoYiDoc,
    },
    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
        }
      }
    },
    methods: {
      goMaterial(msgC, classFast) {
        if (msgC != 0) {
          this.$router.push({
            path: '/ehsMaterial/ehsMaterial' + classFast, query: {"classFirst": classFast,"isSendQuantity":1}
          });
         /* setMaterial().then(response => {
            this.$router.push({
              path: '/ehsMaterial/ehsMaterial' + classFast, query: {"classFirst": classFast}
            });
          });*/
        }
      },
      goHD(msgC) {
        if (msgC != 0) {
          setReadByHD().then(response => {
            this.$router.push({
              path: '/ehsHiddenDangerMenu/ehsHiddenDanger', query: {overTime: "10"}
            });
          });
        }
      },
      goMsg(msgC) {
        if (msgC != 0) {
          this.$router.push({
            path: '/ehsNoticeMessage/ehsNoticeMessageDept', query: {noticeType: "0"}
          });
        }
      },
      getUnread() {
        console.log("定时任务");
        //各种消息提醒在这里提醒，
        //有新增的进行消息提醒。只提醒一次，
        //隐患消息提醒
        this.getHdCount();
        this.getMaterialMsg();
        this.getNoticeMsg();
        /*        getUnread().then(response => {
                  //this.hdMsgCount = this.getCount(response.msgHD );
                  //this.msgCount = this.getCount(response.msg );
                  this.getHdCount();
                  this.getMaterialMsg();
                  this.getNoticeMsg();
                  console.log(response);
                });*/

      },
      getCount(msgC) {
        return (msgC == null|| msgC =="")? 0 : (msgC.split(",")).length;
      },
      toggleSideBar() {
        this.$store.dispatch('app/toggleSideBar')
      },
      async logout() {
        this.$confirm('确定注销并退出系统吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('LogOut').then(() => {
            location.href = '/index';
          })
        }).catch(() => {
        });
      },
      getHdCount() {
        if (this.$auth.hasRoleOr(["HDDept", "HDAdmin"])) {
          getOverTimeId().then(response => {
            console.log(response);
            this.hdMsgCount =  this.getCount(response.data.myMessageKey)
            if (response.code == 200) {
              if (response.data.myMessageDesc != "" && response.data.myMessageDesc != null) {
                const h = this.$createElement;
                this.$message({
                  type: "success",
                  message: h(
                    "div",
                    {style: "padding-right:20px display: flex;align-items: center;color:#00B42A"},
                    [
                      h("span", null, response.data.myMessageDesc),
                      h(
                        "span",
                        {
                          style: "position:absolute;right: 15px;cursor:pointer",
                          on: {
                            click: () => {
                              this.goHD((response.data.myMessageKey.split(",")).length);
                            },
                          },
                        },
                        "查看"
                      ),
                    ]
                  ),
                });
              }
              /*              if(response.data.myMessageDesc!=""  && response.data.myMessageDesc!=null){
                              this.$confirm(response.data.myMessageDesc , '隐患信息', {
                                confirmButtonText: '查看',
                                cancelButtonText: '取消',
                                type: 'warning'
                              }).then(() => {
                                updateEhsMyMessage({"myMessageId":response.data.myMessageId,"myMessageState":"Y"}).then(response => {
                                  this.$router.push({
                                    path: '/ehsHiddenDangerMenu/ehsHiddenDanger', query: {overTime: "10" }
                                  });
                                });
                              }).catch(() => {
                                /!* this.$message({
                                   type: 'info',
                                   message: '已取消删除'
                                 });*!/
                              });
                            }*/
            }

          });
        } else {
          console.log("不是隐患管理员！");
        }
      },
      getMaterialMsg() {
        if (this.$auth.hasRoleOr(["store"])) {
          for (var i = 1; i <= 4; i++) {
            getSendQuantity(i).then(response => {
              console.log(response);
              if (response.code == 200) {
                var tt = response.data.myMessageType.substring(1, 2);
                if (tt == 1) {
                  this.msgMaterial1 = this.getCount(response.data.myMessageKey);
                } else if (tt == 2) {
                  this.msgMaterial2 = this.getCount(response.data.myMessageKey);
                } else if (tt == 3) {
                  this.msgMaterial3 = this.getCount(response.data.myMessageKey);
                } else if (tt == 4) {
                  this.msgMaterial4 = this.getCount(response.data.myMessageKey);
                }
                if (response.data.myMessageDesc != "" && response.data.myMessageDesc != null) {
                  const h = this.$createElement;
                  this.$message({
                    type: "success",
                    message: h(
                      "div",
                      {style: "padding-right:20px display: flex;align-items: center;color:#00B42A"},
                      [
                        h("span", null, response.data.myMessageDesc),
                        h(
                          "span",
                          {
                            style: "position:absolute;right: 15px;cursor:pointer",
                            on: {
                              click: () => {
                                this.goMaterial(this.getCount(response.data.myMessageKey), response.data.myMessageType.substring(1, 2));
                              },
                            },
                          },
                          "查看"
                        ),
                      ]
                    ),
                  });
                }
              }
            });
          }
        } else {
          console.log("不是仓库管理员！");
        }
      },
      getNoticeMsg() {
        getNoticeMsg( ).then(response => {
          console.log(response);
          if (response.code == 200) {
            this.msgCount = this.getCount(response.data.myMessageKey);
            if (response.data.myMessageDesc != "" && response.data.myMessageDesc != null) {
              const h = this.$createElement;
              this.$message({
                type: "success",
                message: h(
                  "div",
                  {style: "padding-right:20px display: flex;align-items: center;color:#00B42A"},
                  [
                    h("span", null, response.data.myMessageDesc),
                    h(
                      "span",
                      {
                        style: "position:absolute;right: 15px;cursor:pointer",
                        on: {
                          click: () => {
                            this.goMsg((response.data.myMessageKey.split(",")).length);
                          },
                        },
                      },
                      "查看"
                    ),
                  ]
                ),
              });
            }
          }
        });
      },
/*      goMaterial(myMessageId, classFast) {
        setMaterial(myMessageId).then(response => {
          this.$router.push({
            path: '/ehsMaterial/ehsMaterial' + classFast, query: {"classFirst": classFast}
          });
        });
      },*/

    },
    mounted() {
      console.log("启动定时器");
      this.getUnread();
      //一秒后执行一次get方法，打印1
      this.timer = setInterval(this.getUnread, 30000);//十秒执行一次
    },

    //销毁定时器，不然会一直存在
    beforeDestroy() {
      clearTimeout(this.timer);
    }
  }
</script>

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

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

      &:hover {
        background: rgba(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 .3s;

          &:hover {
            background: rgba(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;
          }
        }
      }
    }
  }

  .item {
    margin-top: -20px;

  }
</style>
