<template>
  <div class="navbar">
    <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container"
      @toggleClick="toggleSideBar" />
    <breadcrumb id="breadcrumb-container" class="breadcrumb-container" />
    <div class="right-menu">
      <!-- <template v-if="device !== 'mobile'">
        <search id="header-search" class="right-menu-item" />
        <el-tooltip content="项目文档" effect="dark" placement="bottom">
          <Doc class="right-menu-item hover-effect" />
        </el-tooltip>
        <el-tooltip content="全屏缩放" effect="dark" placement="bottom">
          <screenfull id="screenfull" class="right-menu-item hover-effect" />
        </el-tooltip>
        <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="hover">
        <div class="avatar-wrapper">
          <img :src="logo || Avatar" class="user-avatar">
          <i class="el-icon-caret-bottom" />
          <span class="shop_name">{{ shopName }}</span>
        </div>
        <el-dropdown-menu slot="dropdown">
          <span style="display:block;" @click="show = true">
            <el-dropdown-item>
              布局设置
            </el-dropdown-item>
          </span>
          <router-link to="/shop/shop/shop_configuration">
            <el-dropdown-item>
              个人中心
            </el-dropdown-item>
          </router-link>
          <span @click="dialogVisible = true">
            <el-dropdown-item>
              修改密码
            </el-dropdown-item>
          </span>
          <span style="display:block;" @click="open">
            <el-dropdown-item divided>
              退出登录
            </el-dropdown-item>
          </span>
        </el-dropdown-menu>
      </el-dropdown> -->
    </div>
    <el-dialog title="修改密码" modal-append-to-body append-to-body :visible.sync="dialogVisible" width="400px">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="旧密码" prop="oldPass">
          <el-input :type="ip1Type" v-model="form.oldPass" placeholder="请输入旧密码">
            <i slot="suffix" :class="`el-input__icon ${ip1Type == 'text' ? 'el-icon-view' : 'el-icon-remove-outline'}`"
              @click.stop="changeInputType('ip1Type')"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPass">
          <el-input :type="ip2Type" v-model="form.newPass" placeholder="请输入新密码">
            <i slot="suffix" :class="`el-input__icon ${ip2Type == 'text' ? 'el-icon-view' : 'el-icon-remove-outline'}`"
              @click.stop="changeInputType('ip2Type')"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="rnewPass">
          <el-input :type="ip3Type" v-model="form.rnewPass" placeholder="请再次输入新密码">
            <i slot="suffix" :class="`el-input__icon ${ip3Type == 'text' ? 'el-icon-view' : 'el-icon-remove-outline'}`"
              @click.stop="changeInputType('ip3Type')"></i>
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="formLoading" @click="submitHandle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Doc from '@/components/Doc'
import Screenfull from '@/components/Screenfull'
import SizeSelect from '@/components/SizeSelect'
import Search from '@/components/HeaderSearch'
import Avatar from '@/assets/images/avatar.png'
import { updatePass } from '@/api/login.js'
import { encrypt } from '@/utils/rsaEncrypt'

export default {
  components: {
    Breadcrumb,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search,
    Doc
  },
  data() {
    const validateNewPass = (rule, value, callback) => {
      if (!this.form.newPass) {
        callback(new Error(' '))
      } else if (this.form.newPass === this.form.oldPass) {
        callback(new Error('请输入与旧密码不同的新密码'))
      } else {
        callback()
      }
    }
    const validateRnewPass = (rule, value, callback) => {
      if (!this.form.rnewPass) {
        callback(new Error(' '))
      } else if (this.form.rnewPass !== this.form.newPass) {
        callback(new Error('两次密码输入不一致'))
      } else {
        callback()
      }
    }
    return {
      Avatar: Avatar,
      dialogVisible: false,
      logo: localStorage.getItem('logo'),
      shopName: localStorage.getItem('shopName'),
      ip1Type: 'password',
      ip2Type: 'password',
      ip3Type: 'password',
      formLoading: false,
      form: {
        oldPass: '',
        newPass: '',
        rnewPass: ''
      },
      rules: {
        oldPass: [
          {
            required: true,
            message: ' ',
            trigger: 'blur'
          }
        ],
        newPass: [
          {
            required: true,
            validator: validateNewPass,
            trigger: 'blur'
          }
        ],
        rnewPass: [
          {
            required: true,
            validator: validateRnewPass,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'device',
      'user',
      'baseApi'
    ]),
    show: {
      get() {
        return this.$store.state.settings.showSettings
      },
      set(val) {
        this.$store.dispatch('settings/changeSetting', {
          key: 'showSettings',
          value: val
        })
      }
    }
  },
  methods: {
    // 修改密码框类型
    changeInputType(key) {
      if (this[key] == 'text') {
        this[key] = 'password'
      } else {
        this[key] = 'text'
      }
    },
    // 提交修改密码
    submitHandle() {
      this.$refs.form.validate(async vaild => {
        if (vaild) {
          try {
            this.formLoading = true
            const res = await updatePass({
              loginType: localStorage.getItem('loginType'),
              oldPass: encrypt(this.form.oldPass),
              newPass: this.form.newPass
            })
            this.$notify.success('修改成功，请重新登陆')
            setTimeout(() => {
              this.logout()
            }, 1000)
          } catch (error) {
            this.formLoading = false
            console.log(error);
          }
        }
      })
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    open() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.setItem('logoutHandle', true)
        this.logout()
      })
    },
    logout() {
      this.$store.dispatch('LogOut').then(() => {
        location.reload()
      })
    }
  }
}
</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;
  }

  .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: 10px;

      .avatar-wrapper {
        position: relative;
        display: flex;
        align-items: center;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 50%;
        }

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

        .shop_name {
          font-size: 16px;
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
