<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="密码："  prop="password_old">
                <el-input type="password" v-model="stdRegInfo.password_old" placeholder="请输入密码"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="电子邮箱："  prop="std_email">
                <el-input type="text" v-model="stdRegInfo.std_email" maxlength="60" placeholder="请输入正确的电子邮箱"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="验证码："  prop="code" >
                <el-input type="text" v-model="stdRegInfo.code" maxlength="6" placeholder="请点击发送邮件获取邮件验证码"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-button type="primary" :disabled="secCountDown>0" @click="sendEmail">{{secCountDownInfo}}</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) => {
     
       
       // 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 =  /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
        if (value === '') {
            callback(new Error('请输入电子邮箱'));
        } else if(!regex.test(value)){
            callback(new Error('请输入正确的电子邮箱'));
        } else {
            
            callback();
        }
    };
    
    return {
      showEdit: false,
      secCountDown: 0,
      secCountDownInfo:"发送邮件",
      stdRegInfo: {
        password_old: "",
        std_email: "",
        code: ""
      },
      stdRegInfoRules: {
        password_old: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: validateOldPass, trigger: "blur" }
        ],
        std_email: [
          { required: true, message: "请输入电子邮箱", trigger: "blur" },
          { validator: validateConfirmPass1, trigger: "blur" }
        ]
      }
    };
  },
  mounted(){
      window['VM'] = this;
  },
  destroyed(){
      delete window['VM'];
  },
  methods: {
    async save() {
        this.$refs['stdRegInfo'].validate(async (valid) =>  {
            if (valid) {
                if(!this.stdRegInfo.code){
                  this.alert("请输入验证码!","信息提示");
                  return;
                }
                 let {data} = await this.$http.post("/std/bindEmail.htm",this.Qs.stringify({
                     std_pass: this.md5(this.stdRegInfo.password_old),
                     std_email: this.stdRegInfo.std_email,
                     code: this.stdRegInfo.code
                 }));
                if(data.success){
                    this.alert("电子邮箱绑定成功!","信息提示");
                    
                }else{
                    this.alert(data.errorMsg);
                }
            }
        });
    },
    async sendEmail() {
        var _this = this;
        this.$refs['stdRegInfo'].validate(async (valid) =>  {
            if (valid) {
                
                 let {data} = await this.$http.post("/std/sendEmail.htm",this.Qs.stringify({
                     std_pass: this.md5(this.stdRegInfo.password_old),
                     std_email: this.stdRegInfo.std_email
                 }));
                if(data.success){
                   _this.secCountDown = 60;
                   var int = setInterval(function(){
                    _this.secCountDown--;
                    if(_this.secCountDown == 0){
                      clearInterval(int);
                      //恢复 发送按钮
                      _this.secCountDownInfo = "发送验证码";
                    }else{
                      //倒计时提示
                      _this.secCountDownInfo = _this.secCountDown+"秒后可重新发送";
                    }
                  },1000);

                    this.alert("验证码已发送，请登陆邮箱获取!","信息提示");
                    
                }else{
                    this.alert(data.errorMsg);
                }
            }
        });
    }
  }
};
</script>

<style scoped>
</style>