<template>
  <div class="navbar">
    <hamburger
      :is-active="sidebar.opened"
      class="hamburger-container"
      @toggleClick="toggleSideBar"
    />
    <breadcrumb class="breadcrumb-container" />
    <!-- wesley -->
    <div class="right-menu" style="display:none">
      <!-- <template v-if="device !== 'mobile'"> -->
      <!-- <search
          id="header-search"
          class="right-menu-item"
        />
        <screenfull
          id="screenfull"
          class="right-menu-item hover-effect"
        />
      </template> -->
      <el-dropdown
        class="avatar-container"
        trigger="click"
      >
        <div
          class="avatar-wrapper"
          style="margin-left: 20px"
        >
          <img
            :src="userAvatar"
            class="user-avatar"
          >
          <span class="user-name">{{ name }}</span>
        </div>
        <el-dropdown-menu
          slot="dropdown"
          class="user-dropdown"
        >
          <!-- <router-link to="/">
            <el-dropdown-item>Home</el-dropdown-item>
          </router-link>
          <a target="_blank" href="https://github.com/PanJiaChen/vue-admin-template/">
            <el-dropdown-item>Github</el-dropdown-item>
          </a>
          <a target="_blank" href="https://panjiachen.github.io/vue-element-admin-site/#/">
            <el-dropdown-item>Docs</el-dropdown-item>
          </a>-->
          <el-dropdown-item @click.native="changeHead">
            <span style="display: block">修改头像</span>
          </el-dropdown-item>
          <el-dropdown-item @click.native="changePwd">
            <span style="display: block">修改密码</span>
          </el-dropdown-item>
          <el-dropdown-item
            divided
            @click.native="logout"
          >
            <span style="display: block">退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <!--  修改密码  -->
    <el-dialog
      title="修改密码"
      :visible.sync="changePwdShow"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="新密码"
          prop="newPassword"
        >
          <el-input
            v-model="ruleForm.newPassword"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="确认密码"
          prop="confirmPass"
        >
          <el-input
            v-model="ruleForm.confirmPass"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="primary"
          @click="confirmChangePwd"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!--  修改头像  -->
    <el-dialog
      title="修改头像"
      :visible.sync="changeHeadObj.show"
      width="30%"
      :before-close="handleHeadClose"
    >
      <div class="center-content">
        <el-upload
          class="avatar-uploader"
          :data="dataObj"
          :action="action"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="changeHeadObj.data.image"
            :src="changeHeadObj.data.image"
            class="avatar"
            width="160"
            height="160"
          >
          <i
            v-else
            class="el-icon-plus avatar-uploader-icon"
          />
        </el-upload>
      </div>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="handleHeadClose">取 消</el-button>
        <el-button
          type="primary"
          @click="HandleChangeHeadSubmit"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Screenfull from '@/components/Screenfull'
import Search from '@/components/HeaderSearch'
import { getToken, getUserAvatar, removeToken, setUserAvatar } from '@/utils/auth'
import { changePwd, changeHeader } from '@/api/user'
import { uploadUrl } from '@/utils/request'

export default {
  components: {
    Breadcrumb,
    Hamburger,
    Screenfull,
    Search
  },
  data () {
    const newPassFn = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        if (this.ruleForm.newPassword !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    const confirmPassFn = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      userAvatar: null,
      changePwdShow: false,
      changeHeadObj: {
        show: false,
        data: { image: null }
      },
      action: uploadUrl,
      headers: {
        'Authorization-admin': getToken()
      },
      dataObj: {
        folderId: 1
      },
      ruleForm: {
        password: '',
        newPassword: '',
        confirmPass: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, validator: newPassFn, trigger: 'blur' }
        ],
        confirmPass: [
          { required: true, validator: confirmPassFn, trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.userAvatar = getUserAvatar()
  },
  computed: {
    name () {
      return localStorage.getItem('roleName')
    },
    ...mapGetters(['sidebar', 'avatar', 'device'])
  },
  methods: {
    toggleSideBar () {
      this.$store.dispatch('app/toggleSideBar')
    },
    // 更换头像
    changeHead () {
      this.changeHeadObj.show = true
      this.changeHeadObj.data.image = this.userAvatar
    },
    // 提交更换头像
    async HandleChangeHeadSubmit () {
      if (!this.changeHeadObj.data.image) return this.$message.error('请先上传头像')
      const postData = {
        avatar: this.changeHeadObj.data.image,
        name: '更换'
      }
      await changeHeader(postData)
      this.$message.success('更换成功')
      setUserAvatar(postData.avatar)
      this.userAvatar = postData.avatar
      this.handleHeadClose()
    },
    handleAvatarSuccess (data) {
      if (!data.data.url) return this.$message.error('上传失败，请稍后重试')
      this.changeHeadObj.data.image = data.data.url
    },
    handleHeadClose () {
      this.changeHeadObj.data.image = null
      this.changeHeadObj.show = false
    },
    changePwd () {
      this.ruleForm.newPassword = ''
      this.ruleForm.password = ''
      this.ruleForm.confirmPass = ''
      this.changePwdShow = true
    },
    // 修改密码
    confirmChangePwd () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const obj = {
            password: this.ruleForm.password,
            newPassword: this.ruleForm.newPassword
          }
          changePwd(obj).then(res => {
            if (res.code === '') {
              this.changePwdShow = false
              this.$message({
                message: '修改成功，请重新登录！',
                type: 'success'
              })
              this.$nextTick(() => {
                this.$refs['ruleForm'].clearValidate()
              })
              setTimeout(() => {
                localStorage.clear()
                removeToken()
                this.$router.push(`/login?redirect=${this.$route.fullPath}`)
              }, 2000)
            }
          }).catch(() => {
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose () {
      this.changePwdShow = false
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    async logout () {
      localStorage.clear()
      removeToken()
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    }
  }
}
</script>

<style
  lang="scss"
  scoped
>
.navbar {
  width: 100%;
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

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

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

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}

.user-name {
  vertical-align: top;
  font-size: 1rem;
  margin-left: 5px;
  margin-top: -4px;
  display: inline-block;
}

.el-icon-plus {
  width: 160px;
  height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #a1a0a0;
  border-radius: 5px;
}

.center-content {
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
