<!-- 前台头部 -->
<template>
  <el-header>
    <div class="header-container">
      <div class="first-menus">
        <!-- <div
          v-for="item in frontMenus"
          :key="item.alias"
          :class="
            `first-menus-item ${currentMenu == item.alias ? 'active-menu' : ''}`
          "
          @click="handleClickMenu(item)"
        >
          <svgIcon
            :icon-class="item.menuIcon ? item.menuIcon : ''"
            icon-style="icon header-icon"
          />
          {{ item.name }}
        </div> -->
      </div>
      <div class="header-left">
        <span class="title">
          您好，<a>{{ $store.state.login.currentUser.username }}！</a></span
        >
      </div>
      <div class="header-right">
        <el-popover
          :visible-arrow="false"
          popper-class="header-popper"
          trigger="hover"
          width="250"
        >
          <HtAvatar slot="reference" :user="currentUser" class="avatar" />
          <template>
            <div>
              <div class="flex user-box-header">
                <HtAvatar :user="currentUser" style="margin-right: 15px" />
                <div>
                  <h3
                    v-if="$store.state.login.currentUser"
                    style="margin: 5px 0"
                  >
                    {{ $store.state.login.currentUser.username }}
                  </h3>
                </div>
              </div>
              <div>
                <ul class="user-link-ul">
                  <li @click="openPersonalCenter">
                    <i class="el-icon-user"></i>
                    个人中心
                  </li>
                  <li @click="openResetPwdDialog">
                    <i class="el-icon-setting"></i>
                    修改密码
                  </li>
                  <li v-if="userType !== 2" @click="handleSysPlat">
                    <i class="el-icon-monitor"></i>
                    系统管理
                  </li>
                </ul>
              </div>
              <div class="flex user-box-logout" @click="logout">
                <el-link :underline="false">退出系统</el-link>
              </div>
            </div>
          </template>
        </el-popover>
      </div>
    </div>

    <el-dialog
      id="modlfy-dialog"
      :before-close="() => (modifyPwdDialogVisible = false)"
      :close-on-click-modal="false"
      :visible.sync="modifyPwdDialogVisible"
      title="修改密码"
      width="28%"
    >
      <el-form
        ref="pwdForm"
        :model="pwdForm"
        :rules="pwdRules"
        label-width="120px"
        size="mini"
      >
        <el-form-item label="旧密码" prop="old">
          <el-input
            v-model="pwdForm.old"
            placeholder="请输入旧密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new">
          <el-input
            v-model="pwdForm.new"
            placeholder="请输入新密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="重复新密码" prop="newRepeat">
          <el-input
            v-model="pwdForm.newRepeat"
            placeholder="请再次输入新密码"
            type="password"
          ></el-input>
        </el-form-item>
      </el-form>
      <span class="dialog-footer" style="text-align: center; bottom: auto">
        <el-button size="small" type="primary" @click="submitForm()"
          >确认</el-button
        >
        <el-button size="small" @click="modifyPwdDialogVisible = false"
          >取 消</el-button
        >
      </span>
    </el-dialog>
  </el-header>
</template>

<script>
import HtAvatar from '@/components/common/HtAvatar.vue'
import {mapState} from 'vuex'

export default {
  name: 'navigator',
  components: {HtAvatar},
  props: ['menuShow', 'showTopMenu'],
  data() {
    var validateNew = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      }
      let config = this.pwdStrategy
      if (config.enable == 1) {
        let pwdRule = config.pwdRule
        let pwdLength = config.pwdLength
        if (pwdRule) {
          if (value.length < pwdLength) {
            callback(new Error('新密码长度至少为' + pwdLength))
          } else if (pwdRule == 2) {
            var re = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])')
            if (!re.test(value)) {
              callback(new Error('新密码必须包含数字、字母'))
            } else {
              callback()
            }
          } else if (pwdRule == 3) {
            var re = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9])')
            if (!re.test(value)) {
              callback(new Error('新密码必须包含数字、字母、特殊字符'))
            } else {
              callback()
            }
          } else if (pwdRule == 4) {
            var re = new RegExp(
              '(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9])'
            )
            if (!re.test(value)) {
              callback(new Error('新密码必须包含数字、大小字母、特殊字符'))
            } else {
              callback()
            }
          } else {
            callback()
          }
        }
      } else {
        if (value.length < 6) {
          callback(new Error('新密码长度至少为6'))
        } else {
          callback()
        }
      }
    }
    var validateold = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧密码'))
      } else {
        callback()
      }
    }
    var validateNewReapeat = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else {
        callback()
      }
    }
    return {
      currentMenu: 'examKhMenu',
      modifyPwdDialogVisible: false,
      pwdForm: {
        old: '',
        new: '',
        newRepeat: ''
      },
      pwdRules: {
        old: [{validator: validateold, trigger: 'blur'}],
        new: [{validator: validateNew, trigger: 'blur'}],
        newRepeat: [{validator: validateNewReapeat, trigger: 'blur'}]
      }
    }
  },
  created() {},
  beforeMount() {
    // let arr = window.location.pathname.split('/')
    // let num = arr.length
    // this.currentMenu = arr[num - 1]
    // console.log(this.currentMenu, arr)
  },
  mounted() {
    !this.currentUser && this.$store.dispatch('user/loadCurrentUserDetail')
    const currentUser = this.$store.state.login.currentUser
    if ((!this.frontMenus || this.frontMenus.length == 0) && currentUser) {
      this.$store.dispatch('menu/actionFrontMenus').then(res => {
        this.activePage()
      })
    }
  },
  computed: {
    ...mapState({
      currentUser: state => state.user.currentUserDetail,
      token: state => state.login.currentUser.token,
      frontMenus: state => {
        return state.menu.frontMenus
      },
      sysMenus: state => state.menu.sysMenus,
      userType: function() {
        if (this.currentUser && this.currentUser.user) {
          return this.currentUser.user.userType
        }
      },
      pwdStrategy: state => state.user.pwdStrategy
    })
  },
  watch: {
    $route: 'activePage'
  },
  methods: {
    submitForm() {},
    goIndex() {
      this.$router.push({
        path: `/frontPlat/gis`
      })
    },
    openResetPwdDialog() {
      this.$store.dispatch('user/getDefPwdStrategy')
      this.modifyPwdDialogVisible = true
      if (this.$refs['pwdForm']) {
        this.$refs['pwdForm'].resetFields()
      }
    },
    openPersonalCenter() {
      this.$router.push({
        path: '/frontPlat/PersonalInfo'
      })
    },
    handleSysPlat() {
      if (!this.sysMenus.length) {
        this.$store.dispatch('menu/actionSysMenus').then(res => {
          if (res) {
            window.open(
              window.context.manage +
                // "http://localhost:8081/mvue"+
                '/sysplat/' +
                `${this.sysMenus[0].children[0].alias}` +
                '?token=' +
                this.token,
              '_blank'
            )
          }
        })
      } else {
        window.open(
          window.context.manage +
            // "http://localhost:8081/mvue"+
            '/sysplat/' +
            `${this.sysMenus[0].children[0].alias}` +
            '?token=' +
            this.token,
          '_blank'
        )
      }
    },
    logout() {
      this.$store.dispatch('menu/clearMenus')
      this.$store.dispatch('user/clearUser')
      this.$store.dispatch('menu/setCopyMenus', []) //菜单
      let loginRouthPath =
        localStorage.getItem(
          this.$store.state.login.currentUser.account + 'loginRoutePath'
        ) || '/login'
      this.$store.dispatch('login/logoutAndCleanUp').then(() => {
        switch (window.ssoConfig.mode) {
          case 'cas':
          case 'oauth':
            window.sessionStorage.removeItem('currentUser')
            window.location.href =
              window.ssoConfig.logout +
              '?service=' +
              window.location.href.split('?')[0]

            break
          default:
            this.$router.push({
              path: loginRouthPath
            })
            break
        }
      })
    },
    handleClickMenu(item) {
      if (item.alias === 'examKhMenu') {
        this.$router.push('/frontPlat/examKhMenu/kzzxMenu')
      } else {
        this.$router.push('/frontPlat/examDataMenu/examObjectData/orgDataMenu')
      }
      this.currentMenu = item.alias
      return
      let hasSideMenu = item.children.length ? true : false
      this.$emit('update:menuShow', hasSideMenu)
      this.currentMenu = item.alias
      if (item.children.length !== 0) {
        let childrens = item.children
        let subPath = childrens[0].alias
        if (childrens[0].children && childrens[0].children.length !== 0) {
          let threeChildrens = childrens[0].children
          let threePath = threeChildrens[0].alias
          this.$router.push(
            '/frontPlat/' + item.alias + '/' + subPath + '/' + threePath
          )
        } else {
          this.$router.push('/frontPlat/' + item.alias + '/' + subPath)
        }
      } else {
        this.$router.push('/frontPlat/' + item.alias)
        this.currentMenu = item.alias
      }
    },

    activePage() {
      let fullPath = this.$route.path
      let path = fullPath.split('/')
      let urlAnchor = path[path.indexOf('frontPlat') + 1]
      this.frontMenus.filter(item => {
        if (item.alias == urlAnchor) {
          this.currentMenu = urlAnchor
          // console.log(this.currentMenu)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/css/element-variables.scss';

.el-header {
  padding: 0;
  display: flex;
  align-items: center;
}

.header-container {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  // box-shadow: 0 1px 4px 0 rgba(0, 21, 41, 0.12);
  color: rgba(0, 0, 0, 0.9);
  // color: #fff;
  z-index: 10;
}

.header-left {
  font-size: 18px;
  margin-left: 20px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  cursor: pointer;

  .logo {
    position: relative;

    top: 5px;
    height: 60px;
    vertical-align: middle;
  }

  .title {
    position: relative;
    right: 13%;
    top: 5px;
    padding: 0%;
    margin: 0%;
    font-size: 18px;
    vertical-align: middle;

    a {
      font-size: 18px;
      color: #1289fe;
    }
  }
}

.first-menus {
  width: 78%;
  display: flex;
  align-items: center;
}

.first-menus-item {
  margin: 0 10px;
  padding: 0 5px 0;
  position: relative;
  cursor: pointer;
  font-size: 18px;
  color: rgba(0, 0, 0, 0.9);

  .header-icon {
    font-size: 18px;
  }
}

.first-menus-item.active-menu,
.first-menus-item:hover {
  color: #1289fe; //#00ccff;
}

.header-right {
  margin-right: 10px;
}

.el-avatar {
  width: 40px;
  height: 40px;
  line-height: 40px;
}

.el-avatar i {
  font-size: 16px;
  color: #c0c4cc;
}

.user-box-header {
  padding: 10px;
  background: $--biz-main-color;

  h3 {
    color: #fff;
  }
}

.user-link-ul {
  list-style: none;
  padding: 0;
  margin: 0;
  background: $--biz-main-color;

  > li {
    padding: 8px 10px;
    cursor: pointer;
    color: $--biz-main-text-color;
  }

  > li > i {
    margin-right: 5px;
    font-size: 16px;
    font-weight: bold;
  }

  > li:hover {
    color: #fff;
    background-color: #405769;
  }

  .el-link {
    color: #606266 !important;

    i {
      font-size: 16px;
      margin-right: 5px;
      vertical-align: middle;
    }
  }
}

.user-box-logout {
  justify-content: center;
  background: $--biz-main-color;
  border-top: 1px solid #405769;
  // margin-top: 10px;
  color: $--biz-main-text-color !important;
}

.user-box-logout:hover {
  background: $--hover-bgcolor;
}

.user-box-logout > .el-link {
  line-height: 46px;
  width: 100%;
  color: $--biz-main-text-color !important;
  background: $--biz-main-color;
}

.user-box-logout > .el-link:hover,
.user-box-logout > .el-link:focus {
  color: #fff !important;
}
</style>
