<template>
  <div class="register-container">
    <!-- <van-icon name="arrow-left" @click="$router.go(-1)" /> -->
    <img src="@/assets/images/logo.png" class="logo-img" alt="" />
    <div class="form-content">
      <van-cell-group>
        <van-field
          v-model="form.userId"
          placeholder="请输入用户名"
          :border="hasBorder"
        >
          <img
            class="icon-user"
            src="@/assets/images/icon-user.png"
            slot="left-icon"
            alt=""
          />
        </van-field>
        <van-field
          v-model="form.sms"
          placeholder="请输入验证码"
          :border="hasBorder"
        >
          <img
            class="icon-user"
            src="@/assets/images/icon-code.png"
            slot="left-icon"
            alt=""
          />
          <van-button
            slot="button"
            size="small"
            type="primary"
            class="verify-code"
            :disabled="disabled"
            @click="sendSms"
          >
            <span v-if="!disabled">发送验证码</span>
            <span v-else>{{ time }}秒后重新发送</span>
          </van-button>
        </van-field>
        <van-field
          v-model="form.beInvitedCode"
          placeholder="请输入推荐人邀请码（非必填）"
          :border="hasBorder"
        >
          <img
            class="icon-user"
            src="@/assets/images/icon-invite-code.png"
            slot="left-icon"
            alt=""
          />
        </van-field>
      </van-cell-group>
      <van-button
        class="btn-submit"
        :disabled="registerBtnDisabled"
        @click="handleResister"
      >
        注册
      </van-button>
    </div>
    <van-overlay :show="show" class-name="registerEorr" @click="onClickHide">
      <div class="wrapper" @click.stop>
        <div class="title">{{ title }}</div>
        <div class="error-tip"></div>
        <div class="tip">{{ tip }}</div>
        <van-button @click="show = false">确定</van-button>
      </div>
    </van-overlay>
  </div>
</template>
<script>
import { smsCode } from "@/api/base";
import { register } from "@/api/user";
export default {
  name: "Register",
  data() {
    return {
      form: {
        userId: "",
        sms: "",
        beInvitedCode: ""
      },
      disabled: false,
      registerBtnDisabled: false,
      sendMsg: "发送验证码",
      value: "",
      hasBorder: false,
      show: false,
      title: "注册失败",
      tip: "",
      time: 60,
      timer: null
    };
  },
  mounted() {
    let params = window.location.search;
    if (params) {
      let beInvitedCode = params.split("=")[1];
      if (beInvitedCode) {
        this.form.beInvitedCode = beInvitedCode;
      }
    }
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
  methods: {
    onClickHide() {
      this.show = false;
    },
    sendSms() {
      const _this = this;
      // if (_this.timer) {
      //   return false;
      // }
      // 校验手机号
      let phoneReg = /^(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[57])[0-9]{8}$/;
      //如果手机号码输入为空
      if (!_this.form.userId) {
        _this.$toast("请输入手机号");
        return false;
      }
      //验证输入的电话号码是否是11位数字
      if (!phoneReg.test(_this.form.userId)) {
        _this.$toast("请输入正确手机号");
        return false;
      }
      _this.time = 60;
      _this.disabled = true;
      _this.handelSendSmsApi();
      _this.timer = setInterval(() => {
        if (_this.time === 0) {
          clearInterval(_this.timer);
          _this.disabled = false;
        }
        _this.time--;
      }, 1000);
    },
    handelSendSmsApi() {
      // 检验手机号
      const params = {
        codeType: 0,
        phone: this.form.userId
      };
      smsCode(params).then(res => {
        console.log(res);
      });
    },
    handleResister() {
      // 校验文本框必填项
      const _this = this;
      if (!_this.form.userId) {
        this.$toast("请输入用户名");
        return false;
      }
      if (!_this.form.sms) {
        this.$toast("请输入验证码");
        return false;
      }
      _this.registerBtnDisabled = true;
      this.$toast.loading({
        duration: 5000, // 持续展示 toast
        message: "加载中...",
        forbidClick: true
      });
      register(_this.form).then(res => {
        this.$toast.clear();
        _this.registerBtnDisabled = false;
        if (res.code === 0) {
          this.$toast.success("注册成功");
          //_this.$toast.sucess(res.msg);
        } else {
          _this.show = true;
          _this.tip = res.msg;
          // _this.$toast.fail(res.msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.register-container {
  height: 100vh;
  background-color: #ffffff;
  padding: 54px 15px 0 15px;
  box-sizing: border-box;
  overflow: hidden;
  .van-icon-arrow-left {
    position: absolute;
    left: 15px;
    top: 9px;
    &::before {
      font-size: 20px;
      color: #333;
    }
  }
  .logo-img {
    width: 102px;
    height: 102px;
    display: block;
    margin: 0 auto 50px;
  }
  .form-content {
    padding: 0 15px;
    ::v-deep .van-field__left-icon {
      display: flex;
      align-items: center;
    }
    .van-cell-group::after {
      display: none;
    }
    .van-cell {
      background-color: #f9f9f9;
      border-radius: 25px;
      margin-bottom: 20px;
    }
  }
  .icon-user {
    width: 18px;
    height: 18px;
  }
  .icon-code {
    width: 16px;
    height: 18px;
  }
  .icon-code {
    width: 17px;
    height: 17px;
  }
  .btn-submit {
    width: 100%;
    height: 44px;
    background-image: linear-gradient(72deg, #88c678 0%, #95e87f 100%),
      linear-gradient(#999999, #999999);
    background-blend-mode: normal, normal;
    border-radius: 22px;
    color: #ffffff;
    font-size: 18px;
    margin-top: 20px;
  }
  .verify-code {
    background-color: transparent;
    border: none;
    color: #88c677;
  }
  .van-overlay.registerEorr {
    .wrapper {
      width: 315px;
      height: 293px;
      position: absolute;
      background-color: #ffffff;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -60%);
      padding: 30px 0;
      text-align: center;
      box-sizing: border-box;
      border-radius: 4px;
      .title {
        font-size: 20px;
        font-weight: normal;
        font-stretch: normal;
        line-height: 20px;
        letter-spacing: 0px;
        color: #333333;
      }
      .error-tip {
        width: 72px;
        height: 72px;
        border: 6px solid #ff0000;
        border-radius: 50%;
        font-size: 48px;
        font-weight: bold;
        color: #ff0000;
        margin: 25px auto;
        position: relative;
        &:before,
        &:after {
          content: "";
          display: inline-block;
          width: 6px;
          height: 30px;
          background-color: #ff0000;
          border-radius: 4px;
          transform-origin: center;
          position: absolute;
          left: 50%;
          top: 50%;
        }
        &:before {
          transform: translate(-50%, -50%) rotate(45deg);
        }
        &:after {
          transform: translate(-50%, -50%) rotate(-45deg);
        }
      }
      .tip {
        font-size: 14px;
        font-weight: normal;
        font-stretch: normal;
        line-height: 20px;
        color: #999999;
      }
      .van-button {
        width: 160px;
        height: 36px;
        background-color: #88c678;
        border-radius: 18px;
        color: #ffffff;
        margin-top: 25px;
      }
    }
  }
}
</style>
