<template>
  <div class="app-container">
    <div class="box-container">
      <div class="contain-title">重置密码</div>
    </div>
    <div class="content-container">
      <el-row>
        <el-col :span="14">
          <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
            <el-form-item label="新的密码" prop="pass">
              <el-input type="password" v-model="ruleForm2.pass" placeholder="请填写新的密码"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="ruleForm2.checkPass" placeholder="再次输入新的密码"></el-input>
            </el-form-item>
            <el-form-item class="modiaB">
              <el-button type="primary" @click="submitForm('ruleForm2')">确定</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
  import url from '@/api/modifyPassword'
  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        const reg = /^[a-zA-Z0-9]{6,24}$/
        if (value === '') {
          callback(new Error('请设置您的新密码'))
        } else if (!reg.test(value)) {
          callback(new Error('密码由6-24位数字和字母组成，区分大小写'))
        } else {
          callback()
        }
      }
      var validatePass2 = (rule, value, callback) => {
        const reg = /^[a-zA-Z0-9]{6,24}$/
        if (value === '') {
          callback(new Error('请再次输入密码确认'))
        } else if (!reg.test(value)) {
          callback(new Error('密码由6-24位数字和字母组成，区分大小写'))
        } else if (value !== this.ruleForm2.pass && value) {
          callback(new Error('两次输入不一致，请重新输入'))
        } else {
          callback()
        }
      }
      return {
        ruleForm2: {
          pass: '',
          checkPass: ''
        },
        rules2: {
          pass: [
            { validator: validatePass, trigger: 'blur', required: true }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur', required: true }
          ]
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$http.post(url, { npw: this.ruleForm2.pass }, (response) => {
              if (response.success) {
                this.$confirm('密码已修改，快去登录吧！', '修改密码', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  center: true,
                  showCancelButton: false,
                  showClose: false,
                  closeOnClickModal: false,
                  type: 'success'
                }).then(() => {
                  this.$store.dispatch('LogOut').then((response) => {
                    if (response.success) {
                      this.$router.push('/login')
                    }
                  })
                })
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style scoped>
  .modiaB {
    text-align: center;
    margin: 100px 0;
  }
</style>