<template>
  <view class="register-container">
    <view class="title">免费开店</view>
    <view class="register-content">
      <view class="account-content">
        <view class="input-content">
          <view class="account-register">
            <!--账号-->
            <u-input
              class="input-block"
              v-model="account"
              type="number"
              :border="'false'"
              :color="'#555555'"
              :clearable="true"
              :maxlength="11"
              placeholder="请输入您的手机号码"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color: #999999;'"
            >
              <template slot="prefix">
                <text class="input-label">手机号</text>
              </template>
            </u-input>
            <u-input
              class="input-block"
              v-model="registerCode"
              :type="'text'"
              :color="'#555555'"
              :border="'false'"
              :clearable="true"
              placeholder="请输入您的验证码"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color: #999999;'"
              style="margin-bottom: 20rpx"
            >
              <template slot="prefix">
                <text class="input-label">验证码</text>
              </template>
              <template slot="suffix">
                <u-button
                  class="verify-btn"
                  :hair-line="false"
                  size="mini"
                  @click="getSmsCode"
                  >{{ parseInt(verifyText) > 0 ? `${verifyText}S` : verifyText }}</u-button
                >
              </template>
            </u-input>
          </view>
        </view>

        <view class="btn-content">
          <u-button
            class="btn-register"
            :ripple="true"
            :ripple-bg-color="'rgba(0, 0, 0, 0.15)'"
            :loading="isLoading"
            @click="handleRegister"
            >注册</u-button
          >
        </view>
        <view class="agreement-content">
          注册代表你已经同意<text @click="handerSkip()" class="agreement-item"
            >《用户协议》</text
          >
        </view>
      </view>
    </view>
    <u-toast ref="uToast" />
    <u-modal
      v-model="showLogin"
      :content="'注册成功'"
      :confirm="login"
      :cancel="handerSkipLogin"
    ></u-modal>
  </view>
</template>

<script>
import { mapActions, mapState } from "vuex";
import { GetSmsCode } from "../../api/login";

export default {
  name: "login",
  data() {
    return {
      account: "",
      password: "",
      password2: "",
      isLoading: false,
      registerCode: "",
      loginType: 0,
      verifyText: "获取验证码",
      time: 0,
      client_info: {},
      showPwd: false,
      showPwd2: false,
      showLogin: false,
    };
  },
  onLoad(options) {},
  computed: {
    ...mapState(["userInfo"]),
  },
  onShow() {
    if (this.ukey && [1, 2, 3].includes(this.approvalStatus)) {
      uni.navigateTo({ url: "/pages/audit/checkAudit" });
    }

    if (this.ukey && this.approvalStatus == 4) {
      uni.navigateTo({ url: "/pages/audit/shop1" });
    }

    let time = uni.getStorageSync("registerSmsTime") || 0;
    if (time > 0) {
      this.verifyText = time;
      this.loadSmsTime();
    }
  },
  onHide() {},
  methods: {
    ...mapActions("user", ["GetLogin", "GetSmsCode", "GetAccountLogin"]),
    // 手机注册
    async handleRegister() {
      if (!this.account) {
        this.$refs["uToast"].show({
          message: "请输入您的手机号",
          type: "error",
          duration: 1000,
        });
        return;
      }

      if (!this.isValidPhoneNumber(this.account)) {
        this.$refs["uToast"].show({
          message: "请输入正确的手机号",
          type: "error",
          duration: 1000,
        });
        return;
      }

      if (!this.registerCode) {
        this.$refs["uToast"].show({
          message: "请输入验证码",
          type: "error",
          duration: 1000,
        });
        return;
      }

      let params = {
        user_mobile: this.account,
        rand_key: this.account,
        verify_code: this.registerCode,
      };

      await this.GetLogin(params);
    },
    loadSmsTime() {
      clearInterval(this.time);
      this.time = setInterval(() => {
        this.verifyText--;

        uni.setStorageSync("registerSmsTime", this.verifyText);

        if (this.verifyText <= 0) {
          clearInterval(this.time);
          this.verifyText = "获取验证码";
          uni.removeStorageSync("registerSmsTime");
        }
      }, 1000);
    },
    async getSmsCode() {
      if (!this.account) {
        this.$refs["uToast"].show({
          message: "请输入您的手机号",
          type: "error",
          duration: 1000,
        });
        return;
      }
      if (!this.isValidPhoneNumber(this.account)) {
        this.$refs["uToast"].show({
          message: "请输入正确的手机号",
          type: "error",
          duration: 1000,
        });
        return;
      }

      let params = {
        number: this.account,
      };

      if (Number(this.verifyText) > 0) return;

      this.verifyText = 60;
      clearInterval(this.time);
      this.time = setInterval(() => {
        this.verifyText--;
        uni.setStorageSync("registerSmsTime", this.verifyText);

        if (this.verifyText <= 0) {
          clearInterval(this.time);
          this.verifyText = "获取验证码";
          uni.removeStorageSync("registerSmsTime");
        }
      }, 1000);

      await GetSmsCode(params);
    },
    isValidPhoneNumber(phoneNumber) {
      // 定义正则表达式，校验中国大陆手机号码
      const phoneRegex = /^1[3-9]\d{9}$/;
      return phoneRegex.test(phoneNumber);
    },
    handerSkipLogin() {
      this.showLogin = false;
      uni.navigateBack();
    },
    handerSkip() {
      uni.navigateTo({
        url: "/pages/agreement/agreement",
      });
    },
    login() {
      // uni.navigateTo({
      //   url: "/pages/or/index",
      // });
    },
  },
};
</script>

<style lang="scss">
.register-container {
  overflow: hidden;
  .title {
    margin: 100rpx 0%;
    font-size: 28px;
    font-weight: bold;
    text-align: center;
  }

  .register-content {
    padding: 14% 40rpx 0 40rpx;

    .register-block {
      width: 200rpx;
      height: 200rpx;
      border-radius: 50%;
      overflow: hidden;
      margin: 0 auto;
    }

    .account-content {
      border-radius: 44rpx;
      background: #fff;
      overflow: hidden;
    }

    .login-type {
      display: flex;

      .login-type-item {
        position: relative;
        width: 50%;
        height: 100rpx;
        line-height: 100rpx;
        text-align: center;
        font-weight: bold;
        background: #f7f7f7;
        font-size: 32rpx;
        color: #5c5c5c;
      }

      .active {
        background: #fff;
        color: #fd806b;

        &::before {
          // position: absolute;
          // content: "";
          // left: 89px;
          // top: 39px;
          // border-top: 9px solid red;
          // //border-left和border-right换成透明色 才能形成三角形 不然是长方形
          // border-left: 12px solid transparent;
          // border-right: 12px solid transparent;
          //background-color: red;
        }
      }
    }

    .input-content {
      margin: 68rpx 44rpx;

      .u-input {
        padding: 16rpx !important;
        margin-bottom: 60rpx;
        height: 80rpx;
        color: #999999;
        border-bottom: 1px solid #000;
        border-radius: 0%;
        color: #555555;
      }

      .input-label {
        min-width: 100rpx;
        margin-right: 20px;
      }

      .input-label {
        margin-right: 20px;
      }
    }

    .tips-content {
      display: flex;
      justify-content: space-between;
      margin: 0 48rpx;
      font-size: 28rpx;
      color: red;
    }

    .btn-content {
      margin: 120rpx 56rpx;

      .btn-register {
        width: 300px;
        height: 50px;
        border: none;
        letter-spacing: 10px;
        font-size: 16px;
        background: #4b71ff;
        color: #fff;
      }
    }

    .agreement-content {
      position: fixed;
      bottom: 10%;
      width: 90%;
      text-align: center;
      font-size: 28rpx;
      color: #ccc;

      .agreement-item {
        color: #4b71ff;
      }
    }

    .third-party-content {
      margin: 40rpx 0;

      .title-block {
        display: flex;
        align-items: center;
        padding: 24rpx 20%;
        color: #949494;
        margin-bottom: 20px;

        .title-text {
          padding: 0 20rpx;
        }

        .title-line {
          flex-grow: 1;
          height: 1px;
          background-color: #949494;
        }
      }
    }

    .third-party-img-block {
      display: flex;
      justify-content: space-evenly;
    }

    .verify-btn {
      width: 152rpx;
      height: 64rpx;
      font-size: 24rpx;
      border: none;
      color: #fff;
      background-color: rgba(245, 154, 35, 1);

      &::after {
        border: none;
      }
    }
  }

  @media screen and (device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2) {
    .login-content {
      padding: 10rpx 60rpx 0 60rpx;

      .account-content {
        margin-top: 40rpx !important;
        background: red;
      }
    }
  }
}
</style>