<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"/>
    <img src="../../assets/images/ldr_logo.png" id="logo" style="float:left; height:42px; margin-top:4px; margin-bottom:4px; padding:0; background:transparent;">
    <span class="left-menu">利德尔-技术创新中心-{{ this.deptLabel }}</span>
    <div class="right-menu">
      <template v-if="device!=='mobile'">
        <span class="right-menu-item">{{nowTime}} </span>
        <el-tooltip content="门户网站" effect="dark" placement="bottom">
          <ruo-yi-git id="ruoyi-git" 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">
          <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 Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Search from '@/components/HeaderSearch'
import RuoYiDoc from '@/components/RuoYi/Doc'
import RuoYiGit from '@/components/RuoYi/Git'
import Screenfull from '@/components/Screenfull'
import SizeSelect from '@/components/SizeSelect'
import TopNav from '@/components/TopNav'
import { mapGetters } from 'vuex'
import service from "@/utils/request";
import request from "@/utils/request";

export default {
  data(){
    return{
      nowTime: '',
      weatherData: '',
      deptLabel: ''
    }
  },
  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
      }
    }
  },
  //在mounted钩子函数中调用nowTimes函数、在beforeDestroy钩子函数中调用clear函数
  mounted () {
    this.deptLabel = this.$store.state.user.dept.length > 0 ? this.$store.state.user.dept[0].label : "";
    this.nowTimes()
    //this.fetchWeather();
  },
  beforeDestroy () {
    this.clear()
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index';
        })
      }).catch(() => {});
    },
    fetchWeather() {
      const apiKey = '47624f91f87143518f11e6a02200f283'; // 替换为你的API Key rzg-zbf-yjs-key
      const cityId = '101120101'; //济南101120101 日照：101121501
      const url = `https://devapi.qweather.com/v7/weather/3d?location=${cityId}&key=${apiKey}`;
     request({
        url: url,
        method: 'get'
      }).then(response => {
       console.log(response.daily[0])
       if (response.daily[0]) {
         this.weatherData = response.daily[0];
       }
     })
       .catch(error => {
         console.error('Error fetching data: ', error);
       });
    },
    //获取当前时间
    timeNow(timeStamp) {
      let year = new Date(timeStamp).getFullYear();
      let month =new Date(timeStamp).getMonth() + 1 < 10? "0" + (new Date(timeStamp).getMonth() + 1): new Date(timeStamp).getMonth() + 1;
      let date =new Date(timeStamp).getDate() < 10? "0" + new Date(timeStamp).getDate(): new Date(timeStamp).getDate();
      let hh =new Date(timeStamp).getHours() < 10? "0" + new Date(timeStamp).getHours(): new Date(timeStamp).getHours();
      let mm =new Date(timeStamp).getMinutes() < 10? "0" + new Date(timeStamp).getMinutes(): new Date(timeStamp).getMinutes();
      let ss =new Date(timeStamp).getSeconds() < 10? "0" + new Date(timeStamp).getSeconds(): new Date(timeStamp).getSeconds();
      this.nowTime = year + "年" + month + "月" + date +"日  "+"星期"+'日一二三四五六'.charAt(new Date().getDay())+" "+hh+":"+mm+':'+ss ;
    },
    // 实时刷新当前时间，格式化
    nowTimes(){
      this.timeNow(new Date());
      setInterval(this.nowTimes,1000);
      this.clear()
    },
    //清除定时器
    clear(){
      clearInterval(this.nowTimes)
      this.nowTimes = null;
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #FFFFFF;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border-bottom: 1px solid #E8E8E8;

  // .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;
  }

  .left-menu {
    margin-left: 45px;
    float: left;
    height: 100%;
    line-height: 50px;
    color: #2C3E50;
    font-size: 16px;
    font-weight: 500;
  }

  .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: 15px;
      color: #2C3E50;
      vertical-align: text-bottom;

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

        &:hover {
          background: rgba(64, 158, 255, 0.08);
          border-radius: 4px;
        }
      }
    }

    .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;
          color: #2C3E50;
        }
      }
    }
  }
}
</style>
