<template>
  <view class="register-container">
    <view class="register-content">
      <view class="register-block">
        <view class="register-img"></view>
      </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="'red'"
              :clearable="true"
              :maxlength="11"
              placeholder="请输入您的手机号码"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color:red;'"
            >
              <template slot="prefix">
                <u-icon
                  custom-prefix="custom-icon-shouji custom-icon"
                  size="20"
                  color="red"
                ></u-icon>
              </template>
            </u-input>
            <!--密码-->
            <u-input
              class="input-block"
              v-model="password"
              :type="showPwd ? 'text' : 'password'"
              :color="'red'"
              :border="'false'"
              :clearable="true"
              :placeholder="'请输入您的密码'"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color: red;'"
              style="margin-bottom: 20rpx"
            >
              <template slot="prefix">
                <u-icon
                  custom-prefix="custom-icon-password custom-icon"
                  size="20"
                  color="red"
                ></u-icon>
              </template>
              <template slot="suffix">
                <u-icon
                  name="eye"
                  v-if="!showPwd"
                  :color="'red'"
                  @click="showPwd = !showPwd"
                ></u-icon>
                <u-icon
                  name="eye-fill"
                  v-if="showPwd"
                  :color="'red'"
                  @click="showPwd = !showPwd"
                ></u-icon>
              </template>
            </u-input>
            <!--确认密码-->
            <u-input
              class="input-block"
              v-model="password2"
              :type="showPwd2 ? 'text' : 'password'"
              :color="'red'"
              :border="'false'"
              :clearable="true"
              :placeholder="'请输入确认密码'"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color: red;'"
              style="margin-bottom: 20rpx"
            >
              <template slot="prefix">
                <u-icon
                  custom-prefix="custom-icon-password custom-icon"
                  size="20"
                  color="red"
                ></u-icon>
              </template>
              <template slot="suffix">
                <u-icon
                  name="eye"
                  v-if="!showPwd2"
                  :color="'red'"
                  @click="showPwd2 = !showPwd2"
                ></u-icon>
                <u-icon
                  name="eye-fill"
                  v-if="showPwd2"
                  :color="'red'"
                  @click="showPwd2 = !showPwd2"
                ></u-icon>
              </template>
            </u-input>
            <!--验证码-->
            <u-input
              class="input-block"
              v-model="registerCode"
              :type="'text'"
              :color="'red'"
              :border="'false'"
              :clearable="true"
              placeholder="请输入您的验证码"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color: red;'"
              style="margin-bottom: 20rpx"
            >
              <template slot="prefix">
                <u-icon
                  custom-prefix="custom-icon-yanzhengma custom-icon"
                  size="20"
                  color="red"
                ></u-icon>
              </template>
              <template slot="suffix">
                <u-button
                  class="verify-btn"
                  :hair-line="false"
                  size="mini"
                  @click="getSmsCode"
                  >{{ 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 { GetRegister } from "../../api/register";
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) {},
  onShow() {
    let time = uni.getStorageSync("registerSmsTime") || 0;
    if (time > 0) {
      this.verifyText = time;
      this.loadSmsTime();
    }
  },
  onHide() {},
  methods: {
    // 账号密码登录
    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.password) {
        this.$refs["uToast"].show({
          message: "请输入您的密码",
          type: "error",
          duration: 1000,
        });
        return;
      }

      if (this.password.length < 6) {
        this.$refs["uToast"].show({
          message: "密码少于6位",
          type: "error",
          duration: 1000,
        });
        return;
      }

      if (!this.password2) {
        this.$refs["uToast"].show({
          message: "请再次输入您的密码",
          type: "error",
          duration: 1000,
        });
        return;
      }

      if (this.password != this.password2) {
        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_account: "+86" + this.account,
        user_mobile: "+86" + this.account,
        user_email: "",

        user_nickname: "",
        user_password: this.password,
        user_avatar: this.user_avatar,

        rand_key: "+86" + this.account,
        verify_code: this.registerCode,
        source_user_id: "",
        source_ucc_code: "",
        activity_id: "",
        user_avatar:
          "https://media-mall-prod-1259811287.cos.ap-guangzhou.myqcloud.com/static/demodata/assets/data/avatar.png",
        bind_type: 1,
        user_company_id: "",
      };

      let res = await GetRegister(params);
      if (res.status == 200) {
        this.showLogin = true;
        uni.setStorageSync("uid", res.data.user_id);
        uni.setStorageSync("ukey", res.data.key);

        // #ifdef H5
        $cookies.set("uid", res.data.user_id);
        $cookies.set("ukey", res.data.key);
        // #endif
      }

      if (res.status == 250) {
        this.$refs["uToast"].show({
          message: res.msg,
          type: "error",
          duration: 1000,
        });
      }
    },
    loadSmsTime() {
      this.time = setInterval(() => {
        this.verifyText += 1;

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

        if (this.verifyText == 60) {
          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 = {
        mobile: "+86" + this.account,
      };

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

      this.verifyText = 1;
      this.time = setInterval(() => {
        this.verifyText += 1;

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

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

      let res = await GetSmsCode(params);
      if (res.status == 250) {
        this.$refs["uToast"].show({
          message: res.msg,
          type: "error",
          duration: 500,
        });
      }
    },
    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/index/index",
      });
    },
  },
};
</script>

<style lang="scss">
.register-container {
  height: 100vh;
  background-image: url("../../static/xiaofa-bg.png");
  background-size: 100% 100%;

  .register-content {
    padding: 100rpx 60rpx 0 60rpx;

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

      .register-img {
        background-image: url("../../static/xiaofa-logo.png");
        background-size: 100% 100%;
        width: 100%;
        height: 100%;
      }
    }

    .account-content {
      margin-top: 120rpx;
      border-radius: 44rpx;
      background: #fff;
      box-shadow: 1px 2px 10px 0px #f59d8d;
      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;

      .input-block {
        margin: 40rpx;
        background: #f59d8d;
        opacity: 0.4;
        color: red;
      }

      .u-input {
        padding: 0 20rpx !important;
        height: 80rpx;
      }
    }

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

    .btn-content {
      margin: 40rpx 88rpx;

      .btn-register {
        height: 44px;
        border: none;
        letter-spacing: 10px;
        font-size: 16px;
        background-image: linear-gradient(120deg, #f6d365 0%, #fda085 100%);
        box-shadow: 1px 2px 10px 0px #f59d8d;
        color: #fff;
      }
    }

    .agreement-content {
      margin-bottom: 40rpx;
      text-align: center;
      font-size: 28rpx;
      color: #ccc;

      .agreement-item {
        color: red;
      }
    }

    .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 {
      background-color: red;
      color: #fff;
      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>