<template>
   <d2-container class="page">
       <center>
        <el-form ref="stdRegInfo" :model="stdRegInfo" :rules="stdRegInfoRules" label-width="120px" size="mini" style="width: 500px;">
          <el-row>
            <el-col :span="24">
              <el-form-item label="旧密码：" required prop="password_old">
                <el-input type="password" v-model="stdRegInfo.password_old"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="新密码：" required prop="password_new1">
                <el-input type="password" v-model="stdRegInfo.password_new1" placeholder="包含字母、数字、特称字符，至少10个字符"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="确定新密码：" required prop="password_new2">
                <el-input type="password" v-model="stdRegInfo.password_new2"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-button @click="goUrl('/index')">取 消</el-button>
        <el-button type="primary" @click="save">修 改</el-button>
      </center>
   </d2-container>
</template>

<script>
import { mapState, mapActions } from "vuex";
import util from '@/libs/util.js'
import { log } from 'util';

export default {
  computed: {
    ...mapActions('d2admin/account', [
      'logout'
    ]),
    ...mapState({
      stdReg: state => state.enrol.StdReg,
    })
  },
  data() {
    let validateOldPass = (rule, value, callback) => {
      //  console.log(window['VM'].stdReg['std_pass']);
      //  console.log(this.md5(value).toUpperCase().substring(0, 8));
       
      //  if (window['VM'].stdReg['std_pass'] != this.md5(value).toUpperCase().substring(0, 8)) {
      //      callback(new Error('旧密码不对,请重新输入'));
     //   } else {
            //密码复杂度检查
            callback();
    //    }
    }
    var validateConfirmPass1 = (rule, value, callback) => {
        var regex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{10,30}');
        // var regex =   new RegExp("^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$)^.{8,}$");
        if (value === '') {
            callback(new Error('请再次输入密码'));
        } else if (window['VM'].stdRegInfo.password_new2 && value !== window['VM'].stdRegInfo.password_new2) {
            callback(new Error('两次输入密码不一致!'));
        } else if(!regex.test(value)){
            callback(new Error('密码中必须包含字母、数字、特殊字符至少10个字符!'));
        } else {
            //密码复杂度检查
            callback();
        }
    };
    var validateConfirmPass = (rule, value, callback) => {
        var regex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{10,30}');
        // var regex =   new RegExp("^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$)^.{8,}$");
        if (value === '') {
            callback(new Error('请再次输入密码'));
        } else if (value !== window['VM'].stdRegInfo.password_new1) {
            callback(new Error('两次输入密码不一致!'));
        } else if(!regex.test(value)){
            callback(new Error('密码中必须包含字母、数字、特殊字符至少10个字符!'));
        } else {
            //密码复杂度检查
            callback();
        }
    };
    return {
      showEdit: false,
      stdRegInfo: {
        password_old: "",
        password_new1: "",
        password_new2: ""
      },
      stdRegInfoRules: {
        password_old: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
          { validator: validateOldPass, trigger: "blur" }
        ],
        password_new1: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { validator: validateConfirmPass1, trigger: "blur" }
        ],
        password_new2: [
          { required: true, message: "请再输入新密码", trigger: "blur" },
          { validator: validateConfirmPass, trigger: "blur" }
        ]
      }
    };
  },
  mounted(){
      window['VM'] = this;
  },
  destroyed(){
      delete window['VM'];
  },
  methods: {
    async save() {
        this.$refs['stdRegInfo'].validate(async (valid) =>  {
            if (valid) {
                 let {data} = await this.$http.post("/std/password.htm",this.Qs.stringify({
                     std_pass_old: this.md5(this.stdRegInfo.password_old),
                     std_pass_new: this.md5(this.stdRegInfo.password_new2),
                 }));
                 
                if(data.success){
                    this.alert("密码修改成功,请重新登录!","信息提示");
                     // 删除cookie
                    //util.cookies.remove('token')
                    //util.cookies.remove('uuid')
                    //window.location.href='login/index.html'
                }else{
                    this.alert(data.errorMsg);
                }
            }
        });
    },
    cancel() {
      this.goUrl("/index");
    }
  }
};
</script>

<style scoped>
</style>