<template>
  <div>
    <el-form :model="pwdForm" :rules="pwdRules" ref="pwdForm" class="login-form">
      <el-form-item prop="phone">
        <el-input
          @keyup.enter.native="submit"
          v-model="pwdForm.phone"
          auto-complete="off"
          placeholder="请输入您的手机号"
        ></el-input>
      </el-form-item>

      <el-form-item v-if="this.website.captchaMode" prop="imgCode">
        <el-input
          @keyup.enter.native="handleLogin"
          v-model="pwdForm.imgCode"
          auto-complete="off"
          placeholder="请输入图形验证码"
          class="code-input"
        ></el-input>
        <div class="login-code">
          <img :src="pwdForm.image" class="login-code-img" @click="refreshCode" />
        </div>
      </el-form-item>

      <el-form-item prop="code">
        <el-input
          @keyup.enter.native="submit"
          v-model="pwdForm.code"
          auto-complete="off"
          placeholder="请输入短信验证码"
          class="code-input"
        ></el-input>
        <div class="login-code">
          <el-button type="text" @click="handleSend" class="msg-text">{{msgText}}</el-button>
        </div>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          @keyup.enter.native="submit"
          v-model="pwdForm.password"
          type="password"
          auto-complete="off"
          placeholder="请输入6-20位包含数字和字母的密码"
        ></el-input>
      </el-form-item>
      <div>
        <el-button
          size="small"
          type="primary"
          @click.native.prevent="submit"
          class="login-submit"
          style="margin-top: 30px;"
        >确定</el-button>
      </div>
      <div class="forget-pwd">
        <el-button size="small" type="text" @click="backLogin">返回登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { isvalidatemobile, password } from "@/util/validate";
import { mapGetters } from "vuex";
import { getCaptcha, sendCodeFgt, forgetPwd } from "@/api/tenant/account";
import md5 from 'js-md5'
import { getStore } from '@/util/store'
const validatePassDef = (rule, value, callback) => {
  if (password(value)) {
    callback(new Error("密码需要包含数字和字母，并且长度为6-20位"));
  } else {
    callback();
  }
};
export default {
  name: "codelogin",
  data () {
    const validatePhone = (rule, value, callback) => {
      if (isvalidatemobile(value)[0]) {
        callback(new Error(isvalidatemobile(value)[1]));
      } else {
        callback();
      }
    };
    const validateCode = (rule, value, callback) => {
      if (value.length !== 4) {
        callback(new Error("验证码不正确"));
      } else {
        callback();
      }
    };
    return {
      msgText: "获取验证码",
      msgTime: 120,
      msgKey: false,
      pwdForm: {
        phone: "",
        password: '',
        code: "",
        imgCode: '',
        key: "",
        //预加载白色背景
        image: "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
      },
      pwdRules: {
        phone: [{ required: true, trigger: "blur", validator: validatePhone }],
        imgCode: [{ required: true, message: '请输入图形验证码', trigger: "blur" }],
        code: [{ required: true, trigger: "blur", validator: validateCode }],
        password: [{ required: true, message: '请输入您的密码', trigger: "blur" },
          {
            validator: validatePassDef,
            trigger: "blur",
          }],
      }
    };
  },
  created () {
    this.refreshCode();
  },
  mounted () { },
  computed: {
    ...mapGetters(["website", "tagWel"])
  },
  props: [],
  methods: {
    backLogin(){
      var status = getStore({name: 'workStationStatus'})
      if (status && status === 'workstation') {
        this.$emit('change', 'workstation')
      } else {
        this.$emit('change','login')
      }
    },
    refreshCode () {
      getCaptcha().then(res => {
        const data = res.data;
        this.pwdForm.key = data.key;
        this.pwdForm.image = data.image;
      })
    },
    /*发送短信验证码 */
    handleSend () {
      if (this.msgKey) return;
      this.$refs.pwdForm.validateField('phone', (error) => {
        if (!error) {
          this.$refs.pwdForm.validateField('imgCode', (error) => {
            if (!error) {
              sendCodeFgt(this.pwdForm).then(() => {
                this.msgText = '剩余' + 120 + 's';
                this.msgKey = true;
                const time = setInterval(() => {
                  this.msgTime--;
                  this.msgText = '剩余' + this.msgTime + 's';
                  if (this.msgTime === 0) {
                    this.msgTime = 120;
                    this.msgText = '重新获取';
                    this.msgKey = false;
                    clearInterval(time);
                  }
                }, 1000);
              }, error => {
                window.console.log(error);
                this.$message.error('发送失败!')
              });
            }
          })
        }
      })
    },
    submit () {
      this.$refs.pwdForm.validate(valid => {
        if (valid) {
          var status = getStore({name: 'workStationStatus'})
          var authType = 0
          if (status && status === 'workstation') {
            authType = 2
          }
          forgetPwd(this.pwdForm.phone, this.pwdForm.code, md5(this.pwdForm.password), authType).then(() => {
            this.$message({
              message: '修改密码成功',
              type: 'success'
            });
            setTimeout(() => {
              this.$emit('change', 'login')
            }, 1000);
          }, error => {
            window.console.log(error);
          });
        }
      });
    }
  }
};
</script>
<style scoped>
.login-form {
  margin-top: -30px !important;
}
.forget-pwd {
  text-align: center !important;
  margin-top: 30px !important;
}
</style>
<style lang="scss">
@import "@/styles/login.scss";
</style>
