<template>
  <div class="nav-menu">
    <div class="header-logo">
      <span>SAAS 管理平台</span>
    </div>
    <el-menu
      ref="menu"
      router
      mode="horizontal"
      :default-active="this.$route.path"
      text-color="#fff"
      style="flex: 1"
      background-color="#1565C0"
      @click.native="onClickMenus"
      active-text-color="#ffd04b"
    >
      <template v-for="(nav, index) in menuList">
        <el-menu-item
          :key="index"
          v-if="nav.children.length === 0"
          :index="nav.index"
        >
          <template slot="title">
            <i :class="nav.icon"></i>
            <span class="label">{{ nav.label }}</span>
          </template>
        </el-menu-item>
        <el-submenu :key="index" v-else :index="nav.index">
          <template slot="title">
            <i :class="nav.icon"></i>
            <span class="label">{{ nav.label }}</span>
          </template>
          <el-menu-item
            :index="subs.index"
            v-for="(subs, index) in nav.children"
            :key="index"
          >
            <template slot="title">
              <i :class="subs.icon"></i>
              <span class="label">{{
                subs.label
              }}</span></template
            >
          </el-menu-item>
        </el-submenu>
      </template>
    </el-menu>
    <div class="user-area">
      <span class="user-avatar">
        <i class="el-icon-user"></i>
      </span>
      <el-dropdown>
        <div size="mini" style="color: #fff">
          {{ userInfo && userInfo.realName
          }}<i
            class="el-icon-arrow-down el-icon--right"
          ></i>
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="updatePassword"
            >修改密码</el-dropdown-item
          >
          <el-dropdown-item @click.native="logout"
            >退出登录</el-dropdown-item
          >
        </el-dropdown-menu>
      </el-dropdown>

      <!-- <el-button
        type="text"
        style="color: #fff"
        @click="logout"
        >退出登录</el-button
      > -->
    </div>
    <el-dialog
      :close-on-click-modal="false"
      title="修改密码"
      :visible.sync="dialogVisible"
      width="400px"
    >
      <el-form
        :inline="true"
        label-width="100px"
        size="small"
      >
        <el-form-item label="原密码">
          <el-input
            show-password
            v-model="oldPwd"
            placeholder="原密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input
            show-password
            v-model="newPwd"
            placeholder="新密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码">
          <el-input
            show-password
            v-model="confimPwd"
            placeholder="确认新密码"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button
          size="small"
          @click="dialogVisible = false"
          >取 消</el-button
        >
        <el-button
          size="small"
          type="primary"
          @click="resetPasswordSumbit"
        >
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapState } from 'vuex'
import { logout, resetPassword } from '@/common/api/login'
import { setToken, setUser } from '@/utils/auth'
export default {
  name: 'navMenu',
  data() {
    return {
      menuList: [
        {
          id: 1,
          path: '',
          label: 'ERP管理',
          icon: 'el-icon-menu',
          index: '/saas/manage',
          children: [],
        },
        {
          id: 3,
          path: '',
          label: 'ERP菜单管理',
          icon: 'el-icon-menu',
          index: '/saas/menu',
          children: [],
        },
        {
          id: 4,
          path: '',
          label: '工单管理',
          icon: 'el-icon-s-order',
          index: '/saas/taskManage',
          children: [],
        },
        {
          id: 2,
          path: '',
          label: '系统管理',
          icon: 'el-icon-setting',
          index: '/system',
          children: [
            {
              id: 4,
              path: '',
              label: '用户管理',
              icon: 'el-icon-s-order',
              index: 'user',
              children: [],
            },
            {
              id: 4,
              path: '',
              label: '角色管理',
              icon: 'el-icon-s-order',
              index: 'user',
              children: [],
            },
            {
              id: 4,
              path: '',
              label: '汇率币种',
              icon: 'el-icon-bank-card',
              index: '/saas/currency',
              children: [],
            },
            {
              id: 4,
              path: '',
              label: '定时任务',
              icon: 'el-icon-message-solid',
              index: '/saas/timed_task',
              children: [],
            },
            {
              id: 5,
              path: '',
              label: '公告管理',
              icon: 'el-icon-s-promotion',
              index: '/saas/announceManage',
              children: [],
            },

            {
              id: 6,
              path: '',
              label: '国家代码',
              icon: 'el-icon-s-flag',
              index: '/saas/countryCode',
              children: [],
            },
            {
              id: 7,
              path: '',
              label: '州省简称',
              icon: 'el-icon-s-help',
              index: '/saas/abbreviation',
              children: [],
            },
            {
              id: 8,
              path: '',
              label: '客户管理',
              icon: 'el-icon-s-custom',
              index: '/saas/management',
              children: [],
            },
          ],
        },
      ],
      dialogVisible: false,
      oldPwd: '',
      newPwd: '',
      confimPwd: '',
    }
  },
  computed: {
    ...mapGetters('tags', ['currentTag']),
    ...mapState(['userInfo']),
  },
  watch: {
    currentTag(v) {
      if (!v) return
      const parent = this.menuList.find((e) => {
        if (e.path === v.path) return true
        if (e.children) {
          return e.children.some((ee) => ee.path === v.path)
        }
        return false
      })
      if (parent) {
        this.$refs.menu.open(parent.path)
      }
    },
  },
  methods: {
    updatePassword() {
      this.dialogVisible = true
    },
    async resetPasswordSumbit() {
      if (!this.oldPwd) {
        this.$message.warning('请输入原密码')
        return
      }
      if (!this.newPwd) {
        this.$message.warning('请输入新密码')
        return
      }
      if (!this.confimPwd) {
        this.$message.warning('确认新密码')
        return
      }
      if (this.newPwd !== this.confimPwd) {
        return this.$message.warning('新密码两次输入不一致')
      }
      try {
        const res = await resetPassword({
          account: this.userInfo.account,
          oldPwd: this.oldPwd,
          newPwd: this.newPwd,
        })
        if (res.code === 200) {
          this.dialogVisible = false
          this.$message.success('密码修改成功')
          this.logout()
        }
        console.log(res)
      } catch (e) {
        console.error(e)
      }
    },
    async logout() {
      try {
        await logout()
      } catch (e) {
        console.error(e)
      }

      setUser(null)
      setToken('')
      localStorage.setItem('tags', null)
      this.$store.commit('tags/removeAllTags')
      this.$router.push('/login')
    },
    onClickMenus() {},
  },
}
</script>
<style lang="scss" scoped>
.nav-menu {
  display: flex;
  height: 60px;
  overflow: hidden;
  background-color: #1565c0;

  &::v-deep {
    .el-menu {
      border-bottom: none;
    }
    .el-submenu__title i {
      color: #fff;
    }
  }
  i {
    color: #fff;
  }
}

.header-logo {
  font-size: 22px;
  color: #fff;
  font-weight: bold;
  padding: 14px 20px;
  background-color: #1565c0;
}

.label {
  font-size: 16px;
}
.user-area {
  float: right;
  display: flex;
  align-items: center;
  flex-shrink: 0;
  padding-right: 16px;
  color: #fff;
  .user-name {
    margin-right: 10px;
  }
}

.user-avatar {
  width: 24px;
  height: 24px;
  border: 1px solid #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  font-size: 18px;
}
</style>
