<script>
import { clientLogin, sendMsg, captchaImage, getValidateCaptcha } from '@/api/newLogin.js'
export default {
  data() {
    return {
      uuid: '',
      phone: '',
      password: '',
      countdown: 60,
      timer: null,
      isSendingCode: false,
      isBinding: false,
      isPwdHide: true,
      isPasswordLogin: true,
      useCodeLogin: false,

      showCaptcha: false, // 弹窗显隐
      imgCode: '',        // 用户填写的图形验证码
      captchaImg: '',     // 图形验证码图片地址
      captchaKey: ''      // 后端返回验证码标识
    }
  },
  computed: {
    canGetCode() {
      return this.phone && this.validatePhone(this.phone) && !this.isSendingCode && this.countdown === 60
    },
    // canBind() {
    //   return this.phone && this.validatePhone(this.phone) && this.code && this.code.length === 6 && !this.isBinding
    // }

  },
  onShow() {
    this.password = ''
  },
  methods: {
    validatePhone(phone) {
      const reg = /^1[3-9]\d{9}$/
      return reg.test(phone)
    },
    //下一步
    nextStep() {
      if (!this.phone) {
        uni.showToast({
          title: '请输入手机号',
          icon: 'none'
        })
        return
      }

      if (!this.validatePhone(this.phone)) {
        uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        })
        return
      }
      if (!this.isPasswordLogin) {
        this.getCaptcha()
        this.showCaptcha = true
      }

    },
    //登录
    loginPhoneFun() {

      var _this = this;
      if (this.isBinding) return

      if (!this.phone) {
        uni.showToast({
          title: '请输入手机号',
          icon: 'none'
        })
        return
      }

      if (!this.validatePhone(this.phone)) {
        uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        })
        return
      }

      if (!this.password && this.isPasswordLogin) {
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        })
        return
      }

      this.isBinding = true

      clientLogin({
        //openId: uni.getStorageSync('wx_copenid'),
        userType: '2',//1-用户，2-商户
        phone: _this.phone,
        codeSwitch: 'false',//是否开启验证码登录 true-是 false-否
        passWord: _this.password
      }).then(res => {
        console.log('-----', res);
        if (res.data.code == 200) {
          uni.showToast({
            title: '登录成功',
            icon: 'success'
          })
          uni.setStorageSync('wx_copenid', res.data.data.copenid || '')
          uni.setStorageSync('access-token', res.data.data.token)
          uni.setStorageSync('phone', res.data.data.cphone)
          uni.setStorageSync('wx_phone', res.data.data.cphone)

          _this.isBinding = false;
           this.back()
          // uni.navigateTo({
          //   url: '/pages/index/index'
          // })
        } else {
          uni.showToast({
            title: res.data.msg || '登录失败',
            icon: 'none'
          })
          _this.isBinding = false;
        }
      })
    },
    //获取图形验证码
    getCaptcha() {
      captchaImage().then(res => {
        if (this.showCaptcha) {
          this.captchaImg = "data:image/gif;base64," + res.data.img;
          this.uuid = res.data.uuid;
        }
      })
    },
    //返回
    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'
        })
      }
    },
    //图形验证码提交
    submitImgCode() {
      if (!this.imgCode) {
        uni.showToast({
          title: '请输入验证码',
          icon: 'none'
        })
        return 
      }
      
      getValidateCaptcha({ uuid: this.uuid, code: this.imgCode,phone:this.phone }).then(res => {
        console.log(res)
        if (res.data.code == 200) {
          uni.navigateTo({
            url: `/setting/myNew/useCodeLogin?phone=` + this.phone
          })
        }
      })

    },
    //验证码登录
    useCodeLoginFun() {
      this.phone = ''
      this.password = ''
      this.isPasswordLogin = false
    },
    //忘记密码
    forgetPassword() {
      uni.navigateTo({
        url: `/setting/myNew/forgetPassword?phone=` + this.phone
      })

    }


  },
  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="greeting-area">
      <view class="bubble-text">您好，欢迎使用广誉源！</view>
    </view>
    <view class="greeting-area1"></view>
    <view class="content">
      <view class="input-box">
        <u-input v-model="phone" type="number" border="none" maxlength="11" placeholder="请输入手机号"
          placeholder-style="font-weight: 400;font-size: 30rpx;color: #C9CDD4;" class="input-item">
          <!-- <template slot="prefix">
            <view class="label">
              <text>+86</text>
              <u-line length="38rpx" color="#ccc" margin="0 12rpx 0 20rpx" direction="col"></u-line>
            </view>
          </template> -->
        </u-input>
      </view>

      <view class="input-box" v-if="isPasswordLogin">
        <u-input v-model="password" customStyle="font-family: monospace" autocomplete="off" border="none"
          :password="isPwdHide" maxlength="20" customClass="pwd-star" placeholder="请输入密码"
          placeholder-style="font-weight: 400;font-size: 30rpx;color: #C9CDD4;" class="input-item">
          <!-- <template #suffix>
            <image v-if="isPwdHide" src="/static/login/closeEye.png" @click="isPwdHide = !isPwdHide" class="captcha-img"></image>
            <image v-if="!isPwdHide" src="/static/login/openEye.png" @click="isPwdHide = !isPwdHide" class="captcha-img"></image>
             </template> -->
        </u-input>
      </view>
      <view class="bind-btn">
        <u-button v-if="isPasswordLogin || useCodeLogin" :loading="isBinding" type="primary"
          @click="loginPhoneFun">登录</u-button>
        <u-button v-if="!isPasswordLogin" :loading="isBinding" type="primary" @click="nextStep">下一步</u-button>
      </view>
      <view class="textBox">
        <view class="text1" v-if="isPasswordLogin" @click="useCodeLoginFun">验证码登录</view>
        <view class="text1" v-if="!isPasswordLogin" @click="isPasswordLogin = true">密码登录</view>
        <view class="text2" v-if="isPasswordLogin" @click="forgetPassword">忘记密码</view>
      </view>
    </view>



    <!-- //myNew/forgetPassword -->

    <!-- 图形验证码弹窗 -->
    <u-popup :show="showCaptcha" mode="center" border-radius="12">
      <view class="captcha-box">
        <view class="captcha-title">
          <view>请输入下方图形验证码</view>
          <u-icon custom-style="position: relative;left:90rpx ;" name="close" color="#C9CDD4" size="20"
            @click="showCaptcha = false"></u-icon>
        </view>
        <view style="padding: 0 48rpx;margin-top: 48rpx;">
          <view style="display: flex;align-items: center;justify-content: space-between;height: 106rpx;border-bottom: 1rpx solid #E7E7E7;">

            <!-- 验证码输入框，唤起全字符键盘 -->
            <u-input v-model="imgCode" placeholder="请输入图形验证码" :focus="showCaptcha" type="text" maxlength="4"
              border="none" placeholder-style="font-weight: 400;font-size: 30rpx;color: #C9CDD4;"></u-input>
            <!-- 图形验证码图片，点击刷新 -->
             <!-- <img :src="captchaImg" @click="getCode" class="register-code-img"/> -->
            <image :src="captchaImg" @click="refreshCaptcha" class="captcha-img"></image>
          </view>
          <view class="text" @click="getCaptcha">看不清？ 换一张</view>
          <u-button class="submitImgCode" @click="submitImgCode" block>确定</u-button>

        </view>
      </view>
    </u-popup>



  </view>
</template>

<style scoped lang="scss">
::v-deep .u-popup__content {
  background-color: #00000000;
}

.captcha-box {
  width: 622rpx;
  background: #FFFFFF;
  border-radius: 32rpx 32rpx 32rpx 32rpx;

  .captcha-title {
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 500;
    font-size: 32rpx;
    color: #333333;
    text-align: center;
    padding: 32rpx 0;
    border-bottom: 1rpx solid #E7E7E7;

  }

  .captcha-img {
    //width: 100%;
    width: 146rpx;
    height: 60rpx;
  }

  .submitImgCode {
    width: 526rpx;
    height: 88rpx;
    background: #333335;
    border-radius: 60rpx 60rpx 60rpx 60rpx;
    margin: 48rpx 0;

    font-weight: 500;
    font-size: 32rpx;
    color: #FFFFFF;
    text-align: center;
  }

  .text {
    margin-top: 12rpx;
    font-weight: 400;
    font-size: 28rpx;
    color: #86909C;
    text-align: right;

  }

  .u-input {}
}





.sett-box {
  background: #ffffff;
  width: 100%;
  min-height: 100vh;
  line-height: 1;
  position: relative;

  //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
    }

  }

  .content {
    padding: 0 64rpx;

    .textBox {
      display: flex;
      justify-content: space-between;
      font-size: 28rpx;
      padding: 16rpx 42rpx 0 42rpx;
    }

    .text1 {
      font-weight: 400;
      font-size: 28rpx;
      color: #19D29B;
    }

    .text2 {
      font-weight: 400;
      font-size: 28rpx;
      color: #86909C;
      text-align: center;
    }


  }
}

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

  .captcha-img {
    width: 30rpx;
    height: 30rpx;
  }
}

.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: 622rpx;
    height: 88rpx;
    background: #333335;
    border-radius: 60rpx 60rpx 60rpx 60rpx;
    border: none;

    font-weight: 500;
    font-size: 32rpx;
    color: #fff;
    text-align: center;
  }

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