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

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

    <div class="right-menu">
      <template v-if="device!=='mobile'">
        <el-tooltip content="通知" effect="dark" placement="bottom">
          <el-button id="notice" icon="el-icon-message " class="right-menu-item hover-effect" @click="selNotice" style="border: 0" />
        </el-tooltip>

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

      <el-drawer title="通知公告" :visible.sync="drawer" direction="rtl" :size="'60%'">
        <el-radio-group v-model="readingStatu" size="small">
          <el-radio-button label="">全部</el-radio-button>
          <el-radio-button label="1">已读</el-radio-button>
          <el-radio-button label="0">未读</el-radio-button>
        </el-radio-group>
        <br>
        <el-radio-group v-model="messType" size="small">
          <el-radio-button label="">全部</el-radio-button>
          <el-radio-button label="2">公告</el-radio-button>
          <el-radio-button label="1">通知</el-radio-button>
        </el-radio-group>
        <br>
        <div style="padding: 10px;margin: 10px; border: 2px" v-for="notice in noticeList">
          <el-descriptions
            class="margin-top"
            :title="notice.noticeTitle"
            size="small"
            :column="2"
            v-show="(messType === '' || notice.noticeType === messType) && (readingStatu === '' || notice.sta == readingStatu)"
            border>
            <template slot="extra">
              <el-button type="primary" size="small" @click="findNoticeByNoticeIdAndSta(notice.noticeId,notice.sta)">阅读</el-button>
            </template>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-time"/>发布时间
              </template>
              {{ notice.createTime }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-user"/>发布人
              </template>
              {{ notice.createBy }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-coordinate"/>消息类型
              </template>
              <span v-if="notice.noticeType === '1'">
                <el-tag>通知</el-tag>
              </span>
              <span v-if="notice.noticeType === '2'">
                <el-tag type="success">公告</el-tag>
              </span>
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <i class="el-icon-tickets"/>阅读状态
              </template>
              <span v-show="notice.sta === 0">
                <el-tag>未读</el-tag>
              </span>
              <span v-show="notice.sta === 1">
                <el-tag type="success">已读</el-tag>
              </span>
            </el-descriptions-item>
          </el-descriptions>
        </div>
        <el-dialog
          title="提示"
          :visible.sync="findNoticeFilg"
          width="50%">
          <el-form :model="noticeData">
            <el-form-item label="消息标题">
              {{ noticeData.noticeTitle }}
            </el-form-item>
            <el-form-item label="消息类型">
              <span v-if="noticeData.noticeType === '1'">
                <el-tag>通知</el-tag>
              </span>
              <span v-if="noticeData.noticeType === '2'">
                <el-tag type="success">公告</el-tag>
              </span>
            </el-form-item>
            <el-form-item label="有效时间" v-show="noticeData.noticeType === '2'">
              {{ new Date(noticeData.startData).toLocaleString().substring(0,9) }}
              &nbsp;&nbsp;-&nbsp;&nbsp;
              {{ new Date(noticeData.endData).toLocaleString().substring(0,9) }}
            </el-form-item>
            <el-form-item label="消息内容">
              <span v-html="noticeData.noticeContent" />
            </el-form-item>
            <el-form-item label="发布人">
              {{ noticeData.createBy }}
            </el-form-item>
            <el-form-item label="发布时间">
              {{ noticeData.createTime }}
            </el-form-item>
            <el-form-item label="阅读状态">
              <span v-show="noticeData.sta === 0">
                <el-tag>未读</el-tag>
              </span>
              <span v-show="noticeData.sta === 1">
                <el-tag type="success">已读</el-tag>
              </span>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="findNoticeFilg = false">取 消</el-button>
            <el-button type="primary" @click="findNoticeFilg = false">确 定</el-button>
          </span>
        </el-dialog>
      </el-drawer>
    </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 { findNoticeByNoticeId, findNoticeByNoticeIdAndUserId, listNoticeByUserId } from '../../api/system/notice'

export default {
  data() {
    return {
      drawer: false,
      // 阅读状态 默认为空(全部)
      readingStatu: '',
      // 消息类型 默认不限
      messType: '',
      // 个人消息列表
      noticeList: [],
      // 是否显示详细信息
      findNoticeFilg: false,
      // 阅读详细消息
      noticeData: {
        userId: '',
        sta: '',
        noticeId: '',
        noticeTitle: '',
        noticeType: '',
        noticeContent: '',
        createBy: '',
        createTime: ''
      }
    };
  },
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search
  },
  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: {
    findNoticeByNoticeIdAndSta(noticeId,sta) {
      findNoticeByNoticeIdAndUserId({ noticeId: noticeId, sta: sta }).then(res => {
        this.noticeData = res.data
        this.findNoticeFilg = true
        this.selNotice()
      })
    },
    selNotice() {
      listNoticeByUserId().then(res => {
        this.noticeList = res.data
        this.drawer = true
      })
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index';
        })
      }).catch(() => {
      });
    }
  }
}
</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;
        }
      }
    }
  }
}
</style>
