<template>
  <view class="forget-pwd-container">
    <view class="forget-pwd-content">
      <!-- <view class="forget-pwd-block">
        <view class="forget-pwd-img"></view>
      </view> -->
      <view class="account-content">
        <view class="input-content">
          <view class="account-forget-pwd">
            <!--账号-->
            <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="password"
              :type="showPwd ? 'text' : 'password'"
              :color="'#555555'"
              :border="'false'"
              :clearable="true"
              :placeholder="'请输入新密码'"
              :height="60"
              :auto-height="true"
              :placeholder-style="'color: #999999;'"
            >
              <template slot="prefix">
                <u-icon
                  class="input_icon"
                  name="lock"
                ></u-icon>
              </template>
              <template slot="suffix">
                <u-icon
                  name="eye"
                  v-if="!showPwd"
                  @click="showPwd = !showPwd"
                ></u-icon>
                <u-icon
                  name="eye-fill"
                  v-if="showPwd"
                  @click="showPwd = !showPwd"
                ></u-icon>
              </template>
            </u-input>
            <!--验证码-->
            <u-input
              class="input-block"
              v-model="forgetPwdCode"
              :type="'text'"
              :border="'false'"
              :clearable="true"
              placeholder="请输入验证码"
              :height="60"
              :auto-height="true"
              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"
                  @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">
          若你忘记了密码，可在此重置新密码。
        </view>
      </view>
    </view>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
import { GetForgePassword } from "../../api/forgetPassword";
import { GetSmsCode } from "../../api/login";
export default {
  name: "login",
  data() {
    return {
      account: "",
      password: "",
      isLoading: false,
      forgetPwdCode: "",
      loginType: 0,
      verifyText: "获取验证码",
      time: 60,
      client_info: {},
      showPwd: false,
    };
  },
  onLoad(options) {},
  onShow() {
    let time = uni.getStorageSync("forgetPwdSmsTime") || 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.forgetPwdCode) {
        this.$refs["uToast"].show({
          message: "请再次输入验证码",
          type: "error",
          duration: 1000,
        });
        return;
      }

      let params = {
        number: this.account,
        verify_code: this.forgetPwdCode,
        new_password: this.password,
      };

      this.isLoading = true;

      let res = await GetForgePassword(params);
      if (res.status == 200) {
        this.$refs["uToast"].show({
          message: "重置成功",
          type: "success",
          duration: 1000,
        });

        setTimeout(() => {
          uni.navigateTo({
            url: "/pages/login/login",
          });
        }, 2000);
      }

      this.isLoading = false;
    },
    loadSmsTime() {
      this.time = setInterval(() => {
        this.verifyText--;
        uni.setStorageSync("forgetPwdSmsTime", this.verifyText);

        if (this.verifyText == 0) {
          clearInterval(this.time);
          this.verifyText = "获取验证码";
          uni.removeStorageSync("forgetPwdSmsTime");
        }
      }, 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;
      this.time = setInterval(() => {
        this.verifyText--;

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

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

      let res = await GetSmsCode(params);
    },
    isValidPhoneNumber(phoneNumber) {
      // 定义正则表达式，校验中国大陆手机号码
      const phoneRegex = /^1[3-9]\d{9}$/;
      return phoneRegex.test(phoneNumber);
    },
  },
};
</script>

<style lang="scss">
@import "@/styles/variables.scss";
.forget-pwd-container {
  overflow: hidden;

  .forget-pwd-content {
    display: flex;
    align-items: center;
    justify-content: center;
    height: calc(100vh - 88rpx);
    padding: 0 40rpx;

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

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

    .account-content {
      width: 100%;
      height: 440px;
    }

    .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_icon {
      ::v-deep .u-icon__icon {
        font-size: 36rpx !important;
      }
    }

    .input-content {
      .u-input {
        height: 96rpx;
        border-bottom: 1px solid rgba(100, 100, 100, 0.1);
        border-radius: 0%;
        color: #555555;
        padding: 0 !important;
      }
    }

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

    .btn-content {
      margin: 120rpx 0;

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

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