<script>
import { loginPhone, sendMsg } from '@/api/newLogin.js'
export default {
  data() {
    return {
      phone: '', code: '',
      countdown: 60,
    }
  },
  computed: {
    canGetCode() {
      return !this.isSendingCode && this.countdown === 60
    },
    canBind() {
      return this.code && this.code.length === 6 && !this.isBinding
    }
  },
  onLoad(options) {
    if (options.phone) {
      this.phone = options.phone;
      clearInterval(this.timer)
      this.timer = null
      this.startCountdown()
    }

  },
  methods: {
    validatePhone(phone) {
      const reg = /^1[3-9]\d{9}$/
      return reg.test(phone)
    },
    getCode() {
      if (this.isSendingCode || this.countdown < 60) return

      this.isSendingCode = true
      sendMsg({ phone: this.phone }).then(res => {
        console.log(res)

        uni.showToast({
          title: '验证码已发送',
          icon: 'success'
        })

        this.startCountdown()
        this.isSendingCode = false

      })


    },
    startCountdown() {
      this.countdown = 60
      this.timer = setInterval(() => {
        this.countdown--
        if (this.countdown <= 0) {
          clearInterval(this.timer)
          this.countdown = 60
          this.timer = null
        }
      }, 1000)
    },
    // 满6位自动校验
    autoCheckCode(val) {
      // 输入刚好6位时自动提交
      if (val.length === 6) {

        loginPhone({
          //openId: uni.getStorageSync('wx_copenid'),
          userType: '2',
          phone: this.phone,
          codeSwitch: 'true',//是否开启验证码登录 true-是 false-否
          phoneMsg: this.code
        }).then(res => {
          if (res.data.code == 200) {
            // uni.showToast({
            //   title: '登录成功',
            //   icon: 'success'
            // })
            uni.setStorageSync('wx_phone', this.phone)
            this.back()
          }
        })
      }

    },
    back() {
      let pages = getCurrentPages();
      if (pages.length >= 2) {
        let prevPage = pages[pages.length - 3];
        let prevRoute = prevPage.route;
        if (prevRoute && prevRoute !== 'pages/index/index') {
          if (prevRoute.startsWith('pages/')) {
            uni.switchTab({
              url: '/' + prevRoute
            })
          } else {
            uni.navigateBack({
              delta: 1,
              fail: () => {
                uni.switchTab({
                  url: '/pages/index/index'
                })
              }
            })
          }
        } else {
          uni.switchTab({
            url: '/pages/index/index'
          })
        }
      } else {
        uni.switchTab({
          url: '/pages/index/index'
        })
      }
    },
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer)
      this.timer = null
    }
  }
}
</script>

<template>
  <view class="sett-box">
    <view class="greeting-area">
      <view class="bubble-box">
        <view class="bubble-text">请输入验证码</view>
        <view class="sendTag">验证码已发送至：+86 {{ phone }}</view>
      </view>
    </view>
    <view class="greeting-area1"></view>
    <view style="display: flex;justify-content: center;padding: 0 64rpx;">
      <view class="input-box">
        <u-input v-model="code" type="number" border="none" maxlength="6" placeholder="请输入手机验证码"
          placeholder-style="font-weight: 400;font-size: 30rpx;color: #C9CDD4;" class="input-item code-input"
          @input="autoCheckCode"></u-input>
        <view class="code-btn" :class="{ disabled: !canGetCode }" @click="getCode">
          <text v-if="countdown === 60">获取验证码</text>
          <text v-else>{{ countdown }}s 后重新获取</text>
        </view>
      </view>
    </view>




  </view>
</template>

<style scoped lang="scss">
.sett-box {
  background: #ffffff;
  width: 100%;
  min-height: 100vh;
  line-height: 1;
  position: relative;

  .sendTag {
    margin-top: 25rpx;
    font-weight: 400;
    font-size: 28rpx;
    color: #86909C;
    text-align: left;

  }

  //padding: 36rpx 40rpx;
  .greeting-area1 {
    height: 438rpx;
  }

  .greeting-area {
    background-image: url('/static/login/loginBg.png');
    background-size: 100%;
    background-repeat: no-repeat;
    padding-left: 64rpx;
    padding-top: 256rpx;

    top: 0;
    z-index: 4;
    height: 438rpx;
    position: absolute;
    overflow: hidden;
    width: 100%;

    .bubble-text {
      font-weight: 500;
      font-size: 52rpx;
      color: #1D2129
    }

  }

  .code-btn{
    font-weight: 400;
    font-size: 30rpx;
    color: #19D29B;
    text-align: center;
    font-style: normal;
    text-transform: none;
  }


}

.input-box {
  width: 622rpx;
  height: 110rpx;
  background: #FFFFFF;
  display: flex;
  align-items: center;
  border-bottom: 1rpx solid #E7E7E7;

  .label {
    display: flex;
  }

  .input-item {
    flex: 1;
  }

  ::v-deep .uni-input-input {
    font-weight: 500;
    font-size: 30rpx;
    color: #1D2129;
  }
}
</style>
