<template>
  <view class="login-container">
    <u-navbar :autoBack="true" :safeAreaInsetTop="true">
      <template slot="right">
        <view class="btn-register" @click="skipuRegister">开店</view>
      </template>
    </u-navbar>
    <view class="login-content">
      <view class="account-content">
        <view class="login-type">
          <view
            :class="['login-type-item', { active: loginType == 0 }]"
            @click="handleActiveLoginType(0)"
            >账号登录</view
          >
          <view class="login-type-line"></view>
          <view
            :class="['login-type-item', { active: loginType == 1 }]"
            @click="handleActiveLoginType(1)"
            >验证码登录</view
          >
        </view>
        <view class="input-content">
          <view class="account-login">
            <u-input
              class="input-block"
              v-model="account"
              :type="isPHone ? 'number' : 'text'"
              :border="'false'"
              :color="'#555555'"
              :clearable="true"
              :maxlength="isPHone ? 11 : 99"
              :placeholder="placeholderArr[loginType]"
              :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="password"
              :type="showPassWord()"
              :color="'#555555'"
              :border="'false'"
              :clearable="true"
              :placeholder="placeholderArr2[loginType]"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color: #999999;'"
              style="margin-bottom: 20rpx"
            >
              <template slot="prefix">
                <text class="input-label">{{
                  loginType == 0 ? "密码" : "验证码"
                }}</text>
              </template>
              <template slot="suffix">
                <u-button
                  v-if="loginType == 1"
                  class="verify-btn"
                  :hair-line="false"
                  size="mini"
                  @click="getSmsCode"
                  >{{ verifyText }}</u-button
                >
                <u-icon
                  name="eye"
                  v-if="[0, 2, 3].includes(loginType) && !showPwd"
                  :color="'#555555'"
                  size="24"
                  @click="showPwd = !showPwd"
                ></u-icon>
                <u-icon
                  name="eye-fill"
                  v-if="[0, 2, 3].includes(loginType) && showPwd"
                  :color="'#555555'"
                  size="24"
                  @click="showPwd = !showPwd"
                ></u-icon>
              </template>
            </u-input>
            <u-input
              v-if="loginType == 0"
              class="input-block"
              v-model="accountCode"
              :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--image
                  :showLoading="true"
                  :src="verifyImgUrl"
                  width="120px"
                  height="40px"
                  @click="getVerifyCode"
                ></u--image>
              </template>
            </u-input>
          </view>
          <!-- <view class="tips-content">
            <view class="tips-item" @click="skipuRegister">立即注册</view>
            <view class="tips-item" @click="skipuForgetPwd">忘记密码？</view>
          </view> -->
        </view>

        <view class="btn-content">
          <u-button
            class="btn-login"
            :ripple="true"
            :ripple-bg-color="'rgba(0, 0, 0, 0.15)'"
            :loading="isLoading"
            @click="handleLogin"
            >登录</u-button
          >
          <view class="forget-pwd" @click="skipuForgetPwd">忘记密码</view>
        </view>
        <view class="agreement-content">
          登录代表你已经同意<text @click="handerSkip()" class="agreement-item"
            >《用户协议》</text
          >
        </view>
      </view>
    </view>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
import { mapActions, mapState } from "vuex";
import { GetVerifyCode, GetSmsCode } from "@/api/login";
export default {
  name: "login",
  data() {
    return {
      account: "",
      password: "",
      isLoading: false,
      registerCode: "",
      loginType: 0,
      placeholderArr: [
        "请输入您的账号/手机号",
        "请输入您的手机号",
        "请输入您的手机号",
        "请输入您的邮箱",
      ],
      placeholderArr2: [
        "请输入您的密码",
        "请输入您的验证码",
        "请输入您的密码",
        "请输入您的密码",
      ],
      verifyText: "获取验证码",
      time: 60,
      client_info: {},
      isPHone: false,
      isSMS: false,
      showPwd: false,
      verify_token: "",
      verifyImgUrl: "",
      accountCode: "",
    };
  },
  computed: {
    ...mapState("user", ["ukey", "approvalStatus"]),
  },
  onShow() {
    if (this.ukey && [1, 2, 3].includes(this.approvalStatus)) {
      uni.navigateTo({ url: "/pages/audit/checkAudit" });
      return;
    }

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

    let time = uni.getStorageSync("loginSmsTime") || 0;
    if (time > 0) {
      this.verifyText = time;
      this.loadSmsTime();
    }
    this.getVerifyCode();
  },
  onHide() {},
  methods: {
    ...mapActions("user", ["GetLogin", "GetAccountLogin"]),
    loadSmsTime() {
      this.time = setInterval(() => {
        this.verifyText--;

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

        if (this.verifyText == 0) {
          clearInterval(this.time);
          this.verifyText = "获取验证码";
          uni.removeStorageSync("loginSmsTime");
        }
      }, 1000);
    },
    skipuRegister() {
      uni.navigateTo({ url: "/pages/register/register" });
    },
    skipuForgetPwd() {
      uni.navigateTo({ url: "/pages/forgetPassword/forgetPassword" });
    },
    showPassWord() {
      if (this.loginType == 1) {
        return "number";
      }
      if ([0, 2, 3].includes(this.loginType) && !this.showPwd) {
        return "password";
      } else {
        return "text";
      }
    },
    handleActiveLoginType(index) {
      this.loginType = index;
      if ([1, 2].includes(this.loginType)) {
        this.isPHone = true;
      } else {
        this.isPHone = false;
      }
      this.account = "";
      this.password = "";
    },
    // 账号密码登录
    async accountLogin() {
      if (!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.accountCode) {
        this.$refs["uToast"].show({
          message: "请输入您的验证码",
          type: "error",
          duration: 1000,
        });
        return;
      }

      let params = {
        user_account: this.account,
        user_password: this.password,
        verify_code: this.accountCode,
        verify_token: this.verify_token,
      };

      this.isLoading = true;
      await this.GetAccountLogin(params);
      this.isLoading = false;
    },
    // 验证码 登录 通用验证码为9999
    async smsLogin() {
      if (!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;
      }

      let params = {
        user_mobile: this.account,
        rand_key: this.account,
        verify_code: this.password,
      };
      this.isLoading = true;
      await this.GetLogin(params);
      this.isLoading = false;
    },
    async handleLogin() {
      switch (this.loginType) {
        case 0:
          await this.accountLogin();
          break;
        case 1:
          await this.smsLogin();
          break;
        case 2:
          await this.accountLogin();
          break;
        case 3:
          await this.accountLogin();
          break;
        default:
          break;
      }
    },
    async getSmsCode() {
      console.log(this.account);
      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 (this.isSMS) return;

      this.verifyText = 60;
      this.isSMS = true;
      this.time = setInterval(() => {
        this.verifyText--;

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

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

      await GetSmsCode(params);
    },
    async getVerifyCode() {
      this.verify_token = new Date().getTime();
      let res = await GetVerifyCode({ verify_token: this.verify_token });
      this.verifyImgUrl = `https://mall.gpxscs.cn/api/admin/shop/shop-base-config/image?verify_token=${this.verify_token}`;
    },
    handerSkip() {
      uni.navigateTo({
        url: "/pages/agreement/agreement",
      });
    },
    isValidPhoneNumber(phoneNumber) {
      // 定义正则表达式，校验中国大陆手机号码
      const phoneRegex = /^1[3-9]\d{9}$/;
      return phoneRegex.test(phoneNumber);
    },
    isValidEmail(email) {
      var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
      return emailRegex.test(email);
    },
  },
};
</script>

<style lang="scss">
.login-container {
  .btn-register {
    color: #f59a23;
  }

  .login-content {
    padding: 34% 40rpx 0 40rpx;

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

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

    .login-type {
      display: flex;
      align-items: center;

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

      .login-type-line {
        border-left: 2px solid black; /* 左边框 */
        border-right: 0; /* 右边框 */
        border-top: 0; /* 上边框 */
        border-bottom: 0; /* 下边框 */
        height: 30px; /* 根据需要设置高度 */
      }

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

        &::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;
      padding: 40rpx 0;

      .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;
      }
    }

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

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

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

      .forget-pwd {
        margin-top: 60rpx;
        font-size: 26rpx;
        text-align: center;
        color: #7f7f7f;
      }
    }

    .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>