<template>
  <div class="header_container" style="overflow:hidden;">
    <!-- <div class="headerTitle l-float">中国国家博物馆建党百年展点播院线服务平台</div> -->
    <el-dropdown class="adminlogo r-float H100" @command="handleCommand" trigger="click">
      <div class="box">
        <span class="el-dropdown-link">
           {{loginInf.username}}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
      </div>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="editPwd">修改密码</el-dropdown-item>
        <el-dropdown-item command="singout">退出</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <span class="header_icon r-float">
      <img class="bell" src="../../../static/images/icon/bell.png" alt="" />
      <img :src="loginInf.avatar" class="avator" />
    </span>
    <el-dialog title="修改密码" class="hotEvent" :visible.sync="editManege" width="30%" :before-close="close">
      <el-form :model="form" :rules="Rules" id="ruleo" ref="form" label-position="right" label-width="100px">
        <el-form-item label="旧密码：" prop="oldPass">
          <el-input  type="password" size="small"  v-model="form.oldPass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPass">
          <el-input  type="password" size="small"  v-model="form.newPass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码：" prop="checkPass">
          <el-input  type="password" size="small"  v-model="form.checkPass" auto-complete="off"></el-input>
        </el-form-item>
        <!--password-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="close">取消</el-button>
        <el-button type="primary" size="mini" @click="submitForm">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value == '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.newPass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        activeIndex: "",
        loginInf: {},
        editManege: false,
        form: {
          oldPass: '',
          newPass: '',
          checkPass: ''
        },
        Rules: {
          oldPass: [
            {required: true, message: '请输入旧密码', trigger: 'blur'}
          ],
          newPass: [
            {required: true, message: '请输入新密码', trigger: 'blur'},
            {min: 6, message: '密码至少为6个字符', trigger: 'blur'},
            {pattern: /^[a-zA-Z0-9_-]{0,20}$/, message: '请设置合格的密码(可用字母,数字,下划线,减号)'}
          ],
          checkPass: [
            {required: true, message: '请再次输入密码', trigger: 'blur'},
            {validator: validatePass, trigger: 'blur'}
          ]
        },
      }
    },
    mounted() {
      this.initData();
    },
    computed: {},
    methods: {
      initData () {
        let inf={
          avatar:'',
          username:'',
        }
        inf.avatar = localStorage.getItem('avatar');
        inf.username = localStorage.getItem('username');
        this.loginInf=inf;
      },
      handleCommand(command) {
        command == 'singout' ? this.singout() : this.editPwd();
      },
      singout() {
        let vm = this;
        vm.$https({
          url: `logout?token=${localStorage.getItem('backToken')}`,
          method: 'get',
        }).then(res=>{
          vm.removeSession()
          if (res.status == 200) {
            vm.$message({
              type: 'success',
              message: '退出登录!'
            });
            setTimeout(function () {
              vm.$router.push('/');
            },500);
          }
        })
      },
      removeSession () {
        if (localStorage.getItem('backToken')) {
           localStorage.clear();
        }
      },
      editPwd() {
        let _this = this;
        for (let key in _this.form) {
          _this.form[key] = null;
        }
        this.editManege = true;
      },
      submitForm() {
        let _this = this;
        let searchObj = {
          "oldPassWord": _this.form.oldPass,
          "password": _this.form.newPass
        };
        _this.$refs.form.validate((valid) => {
          if (valid) {
            _this.$https({
              url: 'employee/editPwd',
              method: 'put',
              authType: this.backToken
            }, _this.$qs.stringify(searchObj))
              .then((res) => {
                if (res.status == 200) {
                    _this.$message({
                      type: 'success',
                      message: res.data.message
                    });
                    _this.close();
                    _this.removeSession();
                    setTimeout(function () {
                      _this.$router.push('/');
                    },500);
                  } else {
                    _this.$message({
                      type: 'error',
                      message: res.data.message
                    });
                  }
                }, (error) => {
                  _this.$message({
                    type: 'error',
                    message: error
                  });
                }
              )
          } else {
            return false;
          }
        });
      },
      close() {
        this.editManege = false;
        this.$refs['form'].resetFields();
      },
    }
  }
</script>
<style lang="less">
  @import '../../style/common';
  .header_container {
    width: 100%;
    height: 63px;
    background-color: #dd44;
    .headerTitle {
        // width: 100%;
        /*height: 8%;*/
        height: 63px;
        line-height: 63px;
        // background: #002140;
        font-size: 24px;
        font-weight: 600;
        // color: rgba(255, 255, 255, 1);
        // text-align: center;
      }
    .header_icon{
      line-height:63px;
      margin-right:20px;
      img{
        vertical-align: middle;
        &.bell{margin-right:25px;}
        &.avator{background-color:#1D6CAD;border-radius:50%;width:24px; height: 24px;}
      }
    }
    .box {
      cursor: pointer;
      float: right;
      margin-right: 25px;
      margin-top: 20px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;

    }
    /*!*弹窗*!*/
    .el-dialog{
      width: 600px;
      background:rgba(255,255,255,1);
      box-shadow:0px 4px 12px 0px rgba(0,0,0,0.2);
      border-radius:4px;
      .el-dialog__header{
        padding: 15px;
        border-bottom: 1px solid rgba(0,0,0,0.09);
      }
      .el-dialog__body{
        padding: 0px 20px;
        border: 1px solid transparent;
        margin-top: 10px;
        .form_box{
          padding-bottom: 40px;
          .el-form {
            margin-bottom: 20px;
            .el-form-item{
              margin: 10px  50px 20px;
              .el-form-item__content{
                width: 280px;
              }
            }
            .el-input{
              width: 100%;
            }
          }
          .dialog-footer{
            border-top: 1px solid rgba(0,0,0,0.09);
            padding-top: 8px;
            .el-button{
              margin-right: 10px;
              padding: 8px 16px;
            }
          }
        }
      }
    }
  }

  .el-table::before {
    background-color: transparent;
  }
</style>
