<template>
  <view class="register-container">
    <view class="register-content">
      <view class="title">注册开店</view>
      <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">
                <u-icon class="input_icon" name="phone"></u-icon>
              </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"
              :maxlength="4"
            >
              <template slot="prefix">
                <u-icon class="input_icon" name="file-text"></u-icon>
              </template>
              <template slot="suffix">
                <u-button
                  class="verify-btn"
                  :hair-line="false"
                  size="mini"
                  plain
                  @click="getSmsCode"
                >
                  {{ parseInt(verifyText) > 0 ? `${verifyText}S` : verifyText }}
                </u-button>
              </template>
            </u-input>
          </view>
        </view>

        <view class="btn-content">
          <view class="tool">
            <view class="right" @click="goLoginPage"
              >已有账号? <text>立即登录</text></view
            >
          </view>
          <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"]),
    goLoginPage() {
      uni.navigateTo({ url: "/pages/login/login" });
    },
    // 手机注册
    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">
@import "@/styles/variables.scss";
.register-container {
  .register-content {
    display: flex;
    align-items: center;
    justify-content: center;
    height: calc(100vh - 88rpx);
    padding: 0 40rpx;
    flex-direction: column;

    .title {
      margin-bottom: 100rpx;
      font-size: 44rpx;
      font-weight: bold;
      text-align: center;
    }
    .account-content {
      height: 350px;
    }

    .input_icon {
       ::v-deep.u-icon__icon {
        font-size: 36rpx !important;
      }
    }

    .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 {
      .u-input {
        height: 96rpx;
        border-bottom: 1px solid rgba(100, 100, 100, 0.1);
        border-radius: 0%;
        color: #555555;
        padding: 0 !important;
      }

      .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-top: 30rpx;

      .tool {
        display: flex;
        justify-content: flex-end;
        font-size: 26rpx;
        color: #7f7f7f;
        .left {
          flex: 1;
        }
        .right {
          display: flex;
          text {
            color: $base-color;
            margin-left: 10rpx;
            display: inline-block;
          }
        }
      }

      .btn-register {
        width: 100%;
        height: 80rpx;
        border: none;
        letter-spacing: 10rpx;
        font-size: 28rpx;
        background: $base-color;
        color: #fff;
        border-radius: 10rpx;
        margin-top: 80rpx;
      }
    }

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

      .agreement-item {
        color: $base-color;
      }
    }

    .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 {
      height: 64rpx;
      font-size: 28rpx;
      border: none;
      color: $base-color;

      &::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>
