<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" />

        <error-log class="errLog-container right-menu-item hover-effect" />

        <screenfull id="screenfull" class="right-menu-item hover-effect" />

        <el-tooltip content="Global Size" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip>

      </template> -->
      <div v-if="selectProject && showChangeProject" class="change-project" @click="showProjectDialog()">
        <span>{{ selectProject.item_name }}［点击切换］</span>
      </div>

      <el-dropdown v-if="!isChildren" class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <!-- <img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar"> -->
          <span>您好! {{ name }}</span>
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <!-- <router-link to="/profile/index">
            <el-dropdown-item>Profile</el-dropdown-item>
          </router-link>
          <router-link to="/">
            <el-dropdown-item>Dashboard</el-dropdown-item>
          </router-link>
          <a target="_blank" href="https://github.com/PanJiaChen/vue-element-admin/">
            <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 divided @click.native="logout"> -->
          <!-- <el-dropdown-item @click.native="jumpDetail">
            <span style="display:block;">账号信息</span>
          </el-dropdown-item> -->
          <el-dropdown-item @click.native="handerReset">
            <span style="display:block;">重置密码</span>
          </el-dropdown-item>
          <el-dropdown-item @click.native="logout">
            <span style="display:block;">退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <el-dialog
      title="修改密码"
      :visible="pwdDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      width="400px"
    >
      <div>
        <el-form
          ref="pwdForm"
          :rules="pwdRules"
          :model="pwdForm"
          label-width="80px"
        >
          <el-form-item label="旧密码: " prop="old">
            <el-input v-model="pwdForm.old" placeholder="请输入旧密码" />
          </el-form-item>
          <el-form-item label="新密码: " prop="new">
            <el-input v-model="pwdForm.new" placeholder="请输入新密码" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="pwdDialogClose">取消</el-button>
        <el-button type="primary" @click="changePwd">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
// import ErrorLog from '@/components/ErrorLog'
// import Screenfull from '@/components/Screenfull'
// import SizeSelect from '@/components/SizeSelect'
// import Search from '@/components/HeaderSearch'
import { reset } from '@/api/staff'

export default {
  components: {
    Breadcrumb,
    Hamburger,
    // ErrorLog,
    // Screenfull,
    // SizeSelect,
    // Search
  },
  data() {
    return {
      projectDialog: false,
      isInit: false,
      projectOptions: [],
      form: {
        projectId: ''
      },
      rules: {
        projectId: [{ required: true, trigger: 'change', message: '请选择' }]
      },
      selectProject: null,
      showChangeProject: false,

      pwdDialog: false,
      pwdForm: {
        old: '',
        new: ''
      },
      pwdRules: {
        old: [{ required: true, trigger: 'change', message: '请输入旧密码' }],
        new: [{ required: true, trigger: 'change', message: '请输入新密码' }]
      },

      isChildren: self !== top
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'device',
      'name',
      'rolename',
      'userInfo'
    ])
  },
  created() {
    this.initSelectProject()
  },
  methods: {
    initSelectProject() { // 项目经理选择项目
      const [role] = this.$store.getters.roles
      const selectProjectStr = localStorage.getItem('selectProject')
      if (role === 100) {
        this.showChangeProject = true
        if (!selectProjectStr) {
          this.showProjectDialog(true)
        } else {
          this.selectProject = JSON.parse(selectProjectStr)
          this.$store.dispatch('project/select', JSON.parse(selectProjectStr))
        }
      }
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      localStorage.removeItem('selectProject')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      this.$store.dispatch('project/remove')
    },
    async showProjectDialog(isInit) {
      this.isInit = isInit
      await this.setProjectOptions()
      if (this.selectProject) this.form.projectId = this.selectProject.id
      this.projectDialog = true
    },
    closed() {
      this.$store.dispatch('user/logout')
      this.$router.replace({
        path: '/login'
      })
    },
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const { projectId } = this.form
          const selectProject = this.projectOptions.find(item => item.id === projectId)
          localStorage.setItem('selectProject', JSON.stringify(selectProject))
          this.$store.dispatch('project/select', selectProject)
          this.selectProject = selectProject
          this.projectDialog = false
          this.$router.go(0)
        }
      })
    },
    pwdDialogClose() {
      this.$refs['pwdForm'].clearValidate()
      this.$refs['pwdForm'].resetFields()
      this.pwdDialog = false
    },
    jumpDetail() {
      const { id, type } = this.userInfo
      this.$router.push({
        path: '/staff/detail',
        query: { id, type }
      })
    },
    handerReset() {
      this.$prompt('请输入新密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        inputValidator: (value) => {
          if (!value) return '请输入新密码'
          if (value.length < 6) return '请输入6位以上密码'
          return true
        },
        inputErrorMessage: '密码不正确'
      }).then(({ value }) => {
        reset({
          id: this.userInfo.id,
          password: value
        }).then(res => {
          this.$message({
            type: 'success',
            message: '重置成功, 请重新登录!'
          })
          this.closed()
        })
      })
    },
    changePwd() {
      this.$refs['pwdForm'].validate((valid) => {
        if (valid) {
          reset(this.pwdForm).then(() => {
            this.$message({
              type: 'success',
              message: '修改成功, 请重新登录!'
            })
            this.closed()
          })
        }
      })
    }
  }
}
</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;
    display: flex;
    align-items: center;

    &: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)
        }
      }
    }

    .change-project{
      margin-right: 80px;
      cursor: pointer;
    }
    .avatar-container {
      margin-right: 10px;

      .avatar-wrapper {
        // margin-top: 5px;
        position: relative;
        display: flex;
        align-items: center;

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