<template>
  <div class="change-mobile">
    <div class="change">
      <div class="title">更换手机号</div>
      <el-form
        ref="loginForms"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="right"
        label-width="100px"
      >
        <el-form-item prop="mobile" label="新手机号：">
          <el-input
            ref="mobile"
            v-model="loginForm.mobile"
            placeholder="手机号"
            name="mobile"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </el-form-item>
        <el-form-item prop="code" label="验证码：" class="code-sign">
          <ul class="code-main">
            <li>
              <el-input
                ref="code"
                v-model="loginForm.code"
                placeholder="验证码"
                name="code"
                type="code"
                tabindex="1"
                autocomplete="on"
              />
            </li>
            <li><SocialSign :mobile="loginForm.mobile" /></li>
          </ul>
        </el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          round
          style="width: 98%; margin: 15px 0 0 5%"
          @click.native.prevent="handleLogin"
          >确认修改</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { isMobile, isnumber } from "@/utils/validate";
import SocialSign from "@/components/SendVerificationCode";
import asyncRequest from "@/apis/service/user/index";
import resToken from "@/mixins/resToken";
export default {
  name: "Login",
  components: { SocialSign },
  mixins:[resToken],
  data() {
    const validatemobile = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("手机号不能为空！"));
      } else {
        if (!isMobile(value)) {
          callback(new Error("手机号不正确！"));
        } else {
          callback();
        }
      }
    };
    const validatecode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("验证码不能为空！"));
      } else {
        if (!isnumber(value)) {
          callback(new Error("验证码应为6位数字！"));
        } else if (value.length !== 6) {
          callback(new Error("验证码应为6位数字！"));
        } else {
          callback();
        }
      }
    };
    return {
      loginForm: {
        mobile: "",
        code: "",
      },
      loginRules: {
        mobile: [
          { required: true, trigger: "blur", validator: validatemobile },
        ],
        code: [{ required: true, trigger: "blur", validator: validatecode }],
      },
      loading: false,
      otherQuery: {},
    };
  },

  mounted() {
    if (this.loginForm.mobile === "") {
      this.$refs.mobile.focus();
    }
    if (this.loginForm.code === "") {
      this.$refs.code.focus();
    }
  },
  methods: {
    async handleLogin() {
      this.$refs.loginForms.validate(async (valid) => {
        if (valid) {
          if (!this.loading) {
            this.loading = true;
            asyncRequest.resetmobile(this.loginForm).then(async (res) => {
              if (res && res.code === 0) {
                this.loading = false;
                this.$message.success(res.message);
                await this.logout();
              } else if (res && res.code >= 100 && res.code <= 104) {
                await this.logout();
              } else {
                this.$message.warning(res.message);
                this.loading = false;
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
   
  },
};
</script>

<style lang="scss">
.change {
  padding: 0 18% 0 0;
  background: #f2f2f2;
  min-height: 100%;
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: absolute;
  z-index: 2;
  box-sizing: border-box;
  .title {
    border-width: 0px;
    width: 100%;
    height: 140px;
    display: flex;
    font-weight: 400;
    font-style: normal;
    font-size: 22px;
    text-align: left;
    padding: 0;
    padding: 60px 0 0 100px;
    width: 100%;
    box-sizing: border-box;
  }
}
.change-mobile {
  background: rgba(242, 242, 242, 1);
}
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .change-mobile .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.change-mobile {
  .el-input {
    input {
      color: #000;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    // border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: rgba(242, 242, 242, 1);
$dark_gray: #889aa4;
$light_gray: #eee;

.change-mobile {
  min-height: 100%;
  width: 100%;
  height: 100%;
  background-color: $bg;
  overflow: hidden;
  position: absolute;
  z-index: 2;
  background: #fff;

  .login-form {
    position: relative;
    width: 415px;
    max-width: 100%;
    height: 100%;
    padding: 50px 35px 150px 35px;
    margin: 0 auto;
    overflow: hidden;
    .el-form-item__label {
      height: 49px;
      line-height: 49px;
    }

    .el-form-item__content::after,
    .el-form-item__content::before {
      content: "";
      display: block;
      clear: both;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
