<template>
  <sail-page type="forgetPassword" title="忘记密码" isHead>
    <div slot="content">
        <div v-if="!toStep">
          <div class="forget-form">
            <van-cell-group>
              <van-field v-model="phone" placeholder="请输入手机号" />
              <van-field v-model="phoneCode" placeholder="请输入验证码" class="total_box" >
                <van-button :loading="smsLoading" :disabled="isDisable" @click="sendSms" slot="button" size="small" class="send__verfication_code gradient-green">{{message}}</van-button>
              </van-field>
            </van-cell-group>
          </div>
          <van-button :loading="loading" size="large" @click="submit" class="submit-btn" >提交</van-button>
        </div>
        <div v-if="toStep">
          <div class="forget-form">
            <van-cell-group>
              <van-field v-model="newPassword" type="password" placeholder="请输入新密码" />
              <van-field v-model="newPasswordAgain" type="password" placeholder="请确认新密码" />
            </van-cell-group>
          </div>
           <van-button :loading="loading" size="large" @click="submitPassword" class="submit-btn" >提交</van-button>
        </div>
    </div>
  </sail-page>
</template>

<script>
import { mapState } from "vuex";
import sailPage from "@/components/sail-page.vue";
import store from "@/utils/store";
import { Toast } from "vant";
export default {
  components: {
    sailPage
  },
  computed: {
    ...mapState({
      loginInfo: state => state.loginModule.loginInfo
    })
  },
  data: function() {
    return {
      loading: false,
      isDisable: false,
      smsLoading: false,
      phoneCode: "",
      message: "获取验证码",
      toStep: false,
      newPasswordAgain: "",
      newPassword: "",
      phone: "",
      recordFlag: ""
    };
  },
  methods: {
    submitPassword() {
      const passwordReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
      if(!passwordReg.test(this.newPassword)) {
        Toast.fail({message:"请输入6-16位新密码,必须包含字母和数字！",duration:1000});
        return;
      }
      if(!passwordReg.test(this.newPasswordAgain)) {
        Toast.fail({message:"请输入6-16位新密码,必须包含字母和数字！",duration:1000});
        return;
      }
      if(this.newPassword !== this.newPasswordAgain) {
        Toast.fail({message:"两次密码输入不一致,请确认,重新输入！",duration:1000});
        return;
      }
      this.loading = true;
      this.$store.dispatch("loginModule/submitPassword", {
        "confirmPassword": this.newPasswordAgain,
        "password": this.newPassword,
        "phone": this.phone,
        "recordFlag": this.recordFlag
      }).then(res => {
        this.loading = false;
        if(res) {
          store("forgetPassword", true);
          res.Toast.success("提交成功");
          this.$router.push({path: "/login"});
        }
      });
      
    },
    submit() {
      const reg=11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
      if(!reg.test(this.phone)) {
        Toast.fail({message:"手机号格式错误，请重新输入！",duration:1000});
        return;
      }
      if(!this.phoneCode) {
        Toast.fail({message:"请输入验证码！",duration:1000});
        return;
      }
      this.loading = true;
      this.$store.dispatch("loginModule/submitForgetPhoneCode", {
        "phone": this.phone,
        "verificationCode": this.phoneCode
      }).then(res => {
        this.loading = false;
        if(res) {
          this.toStep = true;
          this.recordFlag = res.data.data.recordFlag;
        }
      });
      //
    },
    changeTips() {
      let second = 60;
      this.isDisable = true;
      this.message = `${second}秒后重新获取`;
      const timer = setInterval(() => {
        second--;
        if (second) {
          this.message = `${second}秒后重新获取`;
        } else {
          clearInterval(timer);
          this.isDisable = false;
          this.message = "获取验证码";
        }
      }, 1000);
    },
    sendSms() {
      const reg=11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
      if(!reg.test(this.phone)) {
        Toast.fail({message:"手机号格式错误，请重新输入！",duration:1000});
        return;
      }
      this.smsLoading = true;
      
      
      this.$store.dispatch("loginModule/sendForgetCode", this.phone).then(res => {
        this.smsLoading = false;
        if(res) {
          this.changeTips();
          this.isDisable = true;
        }
      });
    }
  }
};
</script>
<style lang="less">

</style>
<style lang="less" scoped>
.forget-desc {
  text-align: left;
  padding-left: 11px;
  font-size: 11px;
  margin-top: 11.5px;
  margin-bottom: 11.5px;
}
.forget-form {
	margin-top: 10px;
}
.send__verfication_code {
  // min-width: 118px;
  /*height: 34px;*/
  bottom: 10px;
  right: 10px;
  position: absolute;
  font-size: 14px;
  color: #fff;
  border: none;
  /* background: linear-gradient(to right,#5FCEA1,#2BAA76); */
  text-align: center;
  /*line-height: 34px;*/
}
.submit-btn {
  // margin-left: 10px;
}
.van-cell{
  line-height: 30px;
  border-bottom: 1px solid #FAFAFA;
}
.van-cell:not(:last-child):after{
  border: none;
}
</style>
