<template>
  <div class="app-container common-list-page">
    <el-form
      ref="temp"
      :model="temp"
      :rules="rules"
      status-icon
      label-width="100px"
    >
      <el-form-item label="旧密码：" prop="oldpassword">
        <el-input v-model="temp.oldpassword" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item label="新密码：" prop="password">
        <el-input ref="password" v-model="temp.password" :type="passwordType" placeholder="请输入8位以上密码,必须包含英文字母、数字和特殊字符" :clearable="false" auto-complete="off"  />
        <span class="show-pwd" @click="showPwd('1')">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item label="确认密码：" prop="password1">
        <el-input ref="password1" v-model="temp.password1" :type="passwordType1" placeholder="请输入8位以上密码,必须包含英文字母、数字和特殊字符" :clearable="false" auto-complete="off"  />
        <span class="show-pwd" @click="showPwd('2')">
          <svg-icon :icon-class="passwordType1 === 'password' ? 'eye' : 'eye-open'" />
        </span>
        <!-- <el-input v-model="temp.password1" placeholder="请输入8位以上密码,必须包含英文字母、数字和特殊字符" type="password" auto-complete="off" /> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native.prevent="toAmend">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { ExecDataByConfig } from '@/api/common'
export default {
  data() {
    var oldPass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入旧密码'))
      } else if (value.toString().length < 6 || value.toString().length > 18) {
        callback(new Error('密码长度为6-18位'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      const re = new RegExp(`^(?=.*[a-z])(?=.*\\d)(?=.*\\W)[^]{8,32}$`)
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (!re.test(value)) {
        callback(new Error('密码由8位以上数字，大小写字母，特殊字符组成'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.temp.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      resetForm: {
        // name: 'updatePwd',
        // returntype: 'Map',
        // parammaps: {
        //   password: '',
        //   username: this.$store.state.user.name
        // }
      },
      temp: {},
      passwordType: 'password',
      passwordType1: 'password',
      rules: {
        oldpassword: [
          { required: true, validator: oldPass, trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        password1: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    showPwd(item) {
      if(item == 1){
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      }else{
        if (this.passwordType1 === 'password') {
          this.passwordType1 = ''
        } else {
          this.passwordType1= 'password'
        }
        this.$nextTick(() => {
          this.$refs.password1.focus()
        })
      }
    },
    toAmend() {
      console.log(111)
      this.$refs['temp'].validate(valid => {
        if (valid) {
          this.resetForm.common = { 'returnmap': '0' }
          this.resetForm.data = []
          this.resetForm.data[0] = { 'name': 'checkPassword', 'type': 'v', 'parammaps': {
            'id': this.$store.state.user.employeid,
            'password': this.temp.oldpassword
          }}

          this.resetForm.data[1] = { 'name': 'updatePwd', 'type': 'e', 'parammaps': {
            'password': this.temp.password,
            'username': this.$store.state.user.name
          }}

          ExecDataByConfig(this.resetForm).then(response => {
            console.log('保存发送参数', this.resetForm)
            if (response.msg === 'fail') {
              this.$notify({
                title: '保存失败',
                message: response.data,
                type: 'warning',
                duration: 2000
              })
            } else {
              setTimeout(() => {
                this.logout() // 调用跳转到登陆页的方法
              }, 1000)
              this.$notify({
                title: '',
                message: '保存成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    // 这是修改成功后重新返回登陆页的方法，看个人需要自行调整
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login`)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  width: 60%;
  margin: 50px auto 0;
  text-align: center;
  button {
    margin: 20px 0 0;
  }
}
.show-pwd {
      position: absolute;
      right: 40px;
      font-size: 16px;
      cursor: pointer;
      user-select: none;
    }
</style>
