<template>
  <view class="login-container">
    <view class="login-content">
      <view class="logo-block">
        <view class="logo-img"></view>
      </view>
      <view class="account-content">
        <view class="login-type">
          <view
            :class="['login-type-item', { active: loginType == 0 }]"
            @click="handleActiveLoginType(0)"
            >账号登录</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="'red'"
              :clearable="true"
              :maxlength="isPHone ? 11 : 99"
              :placeholder="placeholderArr[loginType]"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color:red;'"
            >
              <template slot="prefix">
                <u-icon
                  v-show="loginType == 0"
                  custom-prefix="custom-icon-custom-user custom-icon"
                  size="20"
                  color="red"
                ></u-icon>
                <u-icon
                  v-show="[1, 2].includes(loginType)"
                  custom-prefix="custom-icon-shouji custom-icon"
                  size="20"
                  color="red"
                ></u-icon>
                <u-icon
                  v-show="loginType == 3"
                  custom-prefix="custom-icon-youxiang custom-icon"
                  size="20"
                  color="red"
                ></u-icon>
              </template>
            </u-input>
            <u-input
              class="input-block"
              v-model="password"
              :type="showPassWord()"
              :color="'red'"
              :border="'false'"
              :clearable="true"
              :placeholder="placeholderArr2[loginType]"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color: red;'"
              style="margin-bottom: 20rpx"
            >
              <template slot="prefix">
                <u-icon
                  v-show="[0, 2, 3].includes(loginType)"
                  custom-prefix="custom-icon-password custom-icon"
                  size="20"
                  color="red"
                ></u-icon>
                <u-icon
                  v-show="loginType == 1"
                  custom-prefix="custom-icon-yanzhengma custom-icon"
                  size="20"
                  color="red"
                ></u-icon>
              </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="'red'"
                  @click="showPwd = !showPwd"
                ></u-icon>
                <u-icon
                  name="eye-fill"
                  v-if="[0, 2, 3].includes(loginType) && showPwd"
                  :color="'red'"
                  @click="showPwd = !showPwd"
                ></u-icon>
              </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>
        <view class="agreement-content">
          登录代表你已经同意<text @click="handerSkip()" class="agreement-item"
            >《用户协议》</text
          >
        </view>
        <view class="third-party-content">
          <view class="title-block">
            <view class="title-line"></view>
            <view class="title-text">其他登录方式</view>
            <view class="title-line"></view>
          </view>
          <view class="third-party-img-block">
            <u-image
              width="80rpx"
              height="80rpx"
              :src="'../../static/weixin.png'"
            ></u-image>
            <u-image
              width="80rpx"
              height="80rpx"
              :src="'../../static/phone.png'"
              @click="handleActiveLoginType(2)"
            ></u-image>
            <u-image
              width="80rpx"
              height="80rpx"
              :src="'../../static/email.png'"
              @click="handleActiveLoginType(3)"
            ></u-image>
          </view>
        </view>
      </view>
    </view>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
import { GetLogin, GetSmsCode, GetSmsLogin } from "../../api/login";
import $cookies from "../../utils/vue-cookies";
export default {
  name: "login",
  data() {
    return {
      account: "",
      password: "",
      isLoading: false,
      registerCode: "",
      loginType: 0,
      placeholderArr: [
        "请输入您的账号",
        "请输入您的手机号",
        "请输入您的手机号",
        "请输入您的邮箱",
      ],
      placeholderArr2: [
        "请输入您的密码",
        "请输入您的验证码",
        "请输入您的密码",
        "请输入您的密码",
      ],
      verifyText: "获取验证码",
      time: 0,
      client_info: {},
      isPHone: false,
      isSMS: false,
      showPwd: false,
    };
  },
  onLoad(options) {},
  onShow() {
    if (uni.getStorageSync("ukey")) {
      uni.switchTab({ url: "/pages/index/index" });
    }
  },
  onHide() {},
  methods: {
    skipuRegister() {
      uni.navigateTo({ url: "/pages/register/register" });
    },
    skipuForgetPwd() {
      uni.navigateTo({ url: "/pages/forgetPassword/forgetPassword" });
    },
    showPassWord() {
      if (this.loginType == 1) {
        return "text";
      }

      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() {
      var isValid = true;
      if (this.loginType == 0) {
        if (!this.account) {
          isValid = false;
          this.$refs["uToast"].show({
            message: "请输入您的账号",
            type: "error",
            duration: 500,
          });
          return;
        }
        if (!this.password) {
          isValid = false;
          this.$refs["uToast"].show({
            message: "请输入您的密码",
            type: "error",
            duration: 1000,
          });
          return;
        }
      }

      if (this.loginType == 2) {
        if (!this.account) {
          isValid = false;
          this.$refs["uToast"].show({
            message: "请输入您的手机号",
            type: "error",
            duration: 500,
          });
          return;
        }
        if (!this.password) {
          isValid = false;
          this.$refs["uToast"].show({
            message: "请输入您的密码",
            type: "error",
            duration: 1000,
          });
          return;
        }
        if (!this.isValidPhoneNumber(this.account)) {
          isValid = false;
          this.$refs["uToast"].show({
            message: "请输入正确的手机号",
            type: "error",
            duration: 1000,
          });
          return;
        }
      }

      if (this.loginType == 3) {
        if (!this.account) {
          isValid = false;
          this.$refs["uToast"].show({
            message: "请输入您的邮箱",
            type: "error",
            duration: 500,
          });
          return;
        }
        if (!this.password) {
          isValid = false;
          this.$refs["uToast"].show({
            message: "请输入您的密码",
            type: "error",
            duration: 1000,
          });
          return;
        }
        if (!this.isValidEmail(this.account)) {
          isValid = false;
          this.$refs["uToast"].show({
            message: "请输入正确的邮箱",
            type: "error",
            duration: 1000,
          });
          return;
        }
      }

      if (!isValid) return;

      let params = {
        user_account: this.account,
        user_password: this.password,
        client_info: this.client_info,
      };

      if (this.loginType == 2) {
        params.user_account = "+86" + this.account;
      }

      this.isLoading = true;

      let res = await GetLogin(params);

      if (res.status == 200) {
        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

        uni.navigateTo({
          url: "/pages/audit/shop1",
        });

        // uni.switchTab({
        //   url: "/pages/index/index",
        // });
      }

      if (res.status == 250) {
        this.$refs["uToast"].show({
          message: res.msg,
          type: "error",
          duration: 1000,
        });
      }
      this.isLoading = false;
    },
    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: "+86" + this.account,
        rand_key: "+86" + this.account,
        verify_code: this.password,
        client_info: this.client_info,
      };

      let res = await GetSmsLogin(params);
      if (res.status == 200) {
        uni.switchTab({
          url: "/pages/index/index",
        });
      }

      if (res.status == 250) {
        this.$refs["uToast"].show({
          message: res.msg,
          type: "error",
          duration: 1000,
        });
      }
    },
    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 = {
        mobile: "+86" + this.account,
      };

      if (this.isSMS) return;

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

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

      let res = await GetSmsCode(params);
      if (res.status == 250) {
        this.$refs["uToast"].show({
          message: res.msg,
          type: "error",
          duration: 500,
        });
      }
    },
    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 {
  height: 100vh;
  background-image: url("../../static/xiaofa-bg.png");
  background-size: 100% 100%;

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

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

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