<script>
import { resetPassword, sendMsg } from '@/api/newLogin.js'
export default {
  data() {
    return {
      phone: '', code: '',
      countdown: 60,
      // 整体8-20位，只能大小写+数字
      regPwd: /^[A-Za-z0-9]{8,20}$/,
      isPwdHide: true,
      isBinding: false,
      password: '',
    }
  },
  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;

    }

  },
  watch: {
    password(val) {
      // 只保留字母+数字，其余全部删除
      let filterStr = val.replace(/[^A-Za-z0-9]/g, '')
      filterStr = filterStr.slice(0, 20)
      // 不一致就强制覆盖，删掉#
      if (filterStr !== val) {
        //this.$set(this.password,filterStr)
        this.$nextTick(() => {
          this.password = filterStr
        });
        
      }
    }
  },
  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)
    },
    // handleInputFilter(val) {
    //   // 过滤掉非字母、数字字符
    //   const filterStr = val.replace(/[^A-Za-z0-9]/g, "");
    //   // 限制最大20位
    //   this.password = filterStr.slice(0, 20);

    // },
    submitFun() {
      if (!this.code) {
        uni.showToast({
          title: '请输入验证码',
          icon: 'none'
        })
        return
      }
      if (!this.password) {
        uni.showToast({
          title: '请输入新密码',
          icon: 'none'
        })
        return
      }
      // 2.位数+格式校验：8~20位字母数字
      if (!this.regPwd.test(this.password) || this.password.length < 8) {
        uni.showToast({ title: "请输入8-20位数字/字母组合", icon: "none" });
        return;
      }
      uni.showToast({
        title: '验证成功',
        icon: 'none'
      })

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

    },
    back() {
      let pages = getCurrentPages();
      if (pages.length >= 2) {
        let prevPage = pages[pages.length - 2];
        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">
    <input type="text" name="username" style="position:absolute;opacity:0;width:0;height:0;z-index:-99;" />
    <input type="password" name="password" style="position:absolute;opacity:0;width:0;height:0;z-index:-99;" />

    <view class="input-box">
      <u-input v-model="phone" :disabled="true" border="none" placeholder="" placeholder-style="color: #999"
        class="input-item">
        <template slot="prefix">
          <view class="label">
            <text>手机号</text>
            <text style="margin-left: 30rpx;">+86</text>
          </view>
        </template>
      </u-input>
    </view>


    <view class="input-box code-box">
      <u-input v-model="code" type="number" border="none" maxlength="6" placeholder="" placeholder-style="color: #999"
        class="input-item code-input">
        <template slot="prefix">
          <view class="label">
            <text>验证码</text>
          </view>
        </template>
      </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 class="input-box">
      <u-input v-model="password" autocomplete="off" border="none" :password="isPwdHide" maxlength="20"
        placeholder="8-20位数字/字母组合" placeholder-style="color: #999" class="input-item" >
        <template slot="prefix">
          <view class="label">
            <text>新密码</text>
          </view>
        </template>
        <template #suffix>
          <u-icon :name="isPwdHide ? 'eye-off' : 'eye'" @click="isPwdHide = !isPwdHide" size="32rpx" color="#999" />
        </template>
      </u-input>
    </view>
    <view class="bind-btn">
      <u-button :loading="isBinding" type="primary" @click="submitFun">确定</u-button>
    </view>


  </view>
</template>

<style scoped lang="scss">
.sett-box {
  width: 750rpx;
  height: 100vh;
  background: #F5F8F8;
  padding: 36rpx 40rpx;
}

.input-box {
  width: 678rpx;
  height: 92rpx;
  background: #FFFFFF;
  border-radius: 24rpx;
  margin-bottom: 36rpx;
  display: flex;
  align-items: center;
  padding: 0 30rpx;

  .label {
    display: flex;
  }

  .input-item {
    flex: 1;
  }
}

.code-box {
  .code-input {
    flex: 1;
  }

  .code-btn {
    width: 200rpx;
    text-align: right;
    font-size: 28rpx;
    color: #2DB1A8;
    line-height: 88rpx;

    &.disabled {
      color: #2DB1A8;
    }
  }
}

.bind-btn {
  margin-top: 120rpx;

  ::v-deep .u-button {
    width: 678rpx;
    height: 84rpx;
    border-radius: 60rpx;
    font-size: 28rpx;
    background: linear-gradient(90deg, #2DB1A8 0%, #33BCB0 100%);
    border: none;
  }

  ::v-deep .u-button--disabled {
    background: #E0E0E0 !important;
    color: #999 !important;
  }
}
</style>
