<template>
  <div class="navbar">
    <div
      style="
        display: flex;
        justify-content: center;
        color: white;
        line-height: 47px;
      "
    >
      <div class="cursor index" @click="handlerIndex">
        <span class="icon">&#xe712;</span>
        <div class="mtt">首页</div>
      </div>
      <div class="cursor index">
        <top-nav></top-nav>
      </div>
      <search />
      <hamburger
        id="hamburger-container"
        :is-active="sidebar.opened"
        v-show="isShowHamBurger"
        class="hamburger-container"
        @toggleClick="toggleSideBar"
      />
    </div>
    <breadcrumb
      id="breadcrumb-container"
      class="breadcrumb-container"
      v-if="!topNav"
    />
    <div class="right-menu">
      <div class="cursor index right-menu-item">
        <header-notice-vue />
      </div>
      <el-dropdown
        class="avatar-container right-menu-item hover-effect"
        trigger="click"
      >
        <div class="avatar-wrapper">
          <img :src="avatar" class="user-avatar" />
          <div class="cons">
            <div class="nickName">{{ user.nickName }}</div>
            <div>{{ user.deptName ? user.deptName : "无部门" }}</div>
          </div>
          <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>

    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :visible.sync="open"
      :append-to-body="true"
      title="修改密码"
      width="70%"
    >
      <p>注：由于您首次登录请您尽快修改密码，保护您的账号安全！</p>
      <el-row>
        <el-col :span="24">
          <div style="display: flex; align-items: center; margin-bottom: 20px">
            <p style="width: 100px; color: #606266">新密码：</p>
            <el-input type="password" show-password v-model="newPassword"></el-input>
          </div>
        </el-col>
      </el-row>
      <div
        style="display: flex; justify-content: center; align-items: center"
        class="dialog-footer"
      >
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { resetUserPwd } from "@/api/system/user.js";
import { getInfo } from "@/api/login.js";
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 HeaderNoticeVue from "./HeaderNotice.vue";

export default {
  data() {
    return {
      newPassword: null,
      userData: {},
      open: false,
      // 菜单列表显示、隐藏
      isShowMenu: false,
      user: this.$store.state.user,
      // 伸缩按钮显示、隐藏
      isShowHamBurger: false,
      routeImgs: [
        require("@/assets/route_images/u211.svg"),
        require("@/assets/route_images/u217.svg"),
        require("@/assets/route_images/u226.svg"),
        require("@/assets/route_images/u214.svg"),
        require("@/assets/route_images/u223.svg"),
        require("@/assets/route_images/u229.svg"),
        require("@/assets/route_images/u220.svg"),
        require("@/assets/route_images/u232.svg"),
      ],
    };
  },
  components: {
    HeaderNoticeVue,
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search,
    RuoYiGit,
    RuoYiDoc,
  },
  computed: {
    ...mapGetters([
      "sidebar",
      "avatar",
      "device",
      "sidebarRouters",
      "deptName",
      "nickName",
    ]),
    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;
      },
    },
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    submitForm() {
      let reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}$/;


      if(reg.test(this.newPassword)){

        resetUserPwd(this.user.userId, this.newPassword, false).then(
          (res) => {
            if (res.code == 200) {
              this.$message.success("修改成功");
              this.open = false;
            }
          }
        );
      }else{
        this.$message.error("密码需包含大小写字母 + 数字 + 特殊字符,且不小于8位");
      }
    },

    getUserInfo() {
      getInfo().then((res) => {
        this.userData = res.user;
        if (res.user.type == true) this.open = 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(() => {});
    },
    handlerShowMenu() {
      this.isShowMenu = !this.isShowMenu;
    },

    handlerIndex() {
      this.isShowHamBurger = false;
      this.$router.push("/");
    },
  },

  watch: {
    "$route.fullPath": {
      handler: function (newV, oldV) {
        if (newV == "/index") {
          this.isShowHamBurger = false;
        } else {
          this.isShowHamBurger = true;
        }
      },
      // deep:true,
    },
  },
};
</script>

<style lang="scss" scoped>
.navbar {
  height: 60px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #2561ef;

  .index {
    width: 100px;
    text-align: center;
    .mtt {
      margin-top: -25px;
      font-size: 14px;
    }
  }

  .cursor {
    cursor: pointer;
  }

  .menu-hover {
    padding: 10px;

    &:hover {
      background-color: rgb(221, 235, 248);
    }
  }

  .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;
        }
        .cons {
          float: right;
          font-size: 13px;
          margin-top: -4px;
          margin-left: 10px;
          //max-width: 100px;
          color: white;
          width: auto;
          .nickName {
            text-align: left;
            height: 15px;
            margin-top: -7px;
            margin-bottom: 5px;
          }
          .deptName {
          }
          .desc {
            margin-top: 7px;
          }
        }
        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 16px;
          font-size: 12px;
          color: white;
        }
      }
    }
  }
}
</style>
