<template>
  <div class="wrapper">
    <img class="logo" src="./image/logo@2x.png" alt="">
    <p class="welcome">您好，妥妥推手</p>
    <ul class="form-wrap">
      <li>
        <label>
          <span v-for="(str, index) in '手机号码：'" :key="index">{{ str }}</span>
        </label>
        <div class="value-wrap">
          <input type="tel" placeholder="请输入手机号码" v-myBlur v-model.trim="postData.partnerPhone" @input="funLimitLength">
        </div>
      </li>
      <li v-if="false">
        <label>
          <span v-for="(str, index) in '图形验证：'" :key="index">{{ str }}</span>
        </label>
        <div class="value-wrap">
          <input type="text" placeholder="图片验证码" v-myBlur v-model.trim="postData.capture">
        </div>
        <img class="capture-img" :src="strCapture" alt="" @click="funUpdateCapture">
      </li>
      <li>
        <label class="label-code">
          <span class="label-code" v-for="(str, index) in '验证码：码'" :key="index">{{ str }}</span>
        </label>
        <div class="value-wrap">
          <input type="tel" placeholder="请输入验证码" v-myBlur v-model.trim="postData.code" @input="funLimitLength">
        </div>
        <button class="get-code" @click="funGetCode">{{[0, 60].includes(numCount) ? '获取': numCount + '秒'}}</button>
      </li>
    </ul>
    <button class="submit" @click="funSubmit">提交</button>
  </div>
</template>

<script>
  import { Toast } from 'vant'
  import { sendSMS } from '../../../api/common'
  import { partnerLogin } from './api'

  const STRCAPTURE = process.env.API_DOMAIN + '/api/captcha'
  export default {
    name: 'login',
    data () {
      return {
        postData: {
          partnerPhone: '',
          capture: '',
          code: ''
        },
        strCapture: STRCAPTURE,
        numCount: 60,
        timer: null,
        arrErrorList: []
      }
    },
    async mounted () {
      await this.$nextTick()
      this.$refreshTitle('登录')
    },
    methods: {
      funLimitLength () {
        const { partnerPhone, code } = this.postData
        this.postData.partnerPhone = partnerPhone.length > 11 ? partnerPhone.slice(0, 11) : partnerPhone
        this.postData.code = code.length > 4 ? code.slice(0, 4) : code
      },
      funCutDown () {
        clearInterval(this.timer)
        this.timer = setInterval(() => {
          if (this.numCount === 0) {
            clearInterval(this.timer)
            this.numCount = 0
            return
          }
          this.numCount--
        }, 1000)
      },
      // 获取验证码
      funGetCode () {
        const numCount = this.numCount
        const { partnerPhone, capture } = this.postData
        if (numCount < 60 && numCount > 0) {
          return
        }
        this.numCount = 60
        if (!(/^1[2|3|4|5|6|7|8|9]\d{9}$/.test(partnerPhone))) {
          Toast('请输入手机号码')
          return
        }

        // if (!capture) {
        //   Toast('请输入图片验证码')
        //   return
        // }
        this.funCutDown()
        sendSMS(partnerPhone, 1, capture).then(res => {
          if (res.status) {
            Toast('发送成功')
          } else {
            Toast(res.msg)
            this.funUpdateCapture()
            clearInterval(this.timer)
            this.numCount = 60
          }
        }).catch(err => {
          Toast(err.msg)
          this.funUpdateCapture()
          clearInterval(this.timer)
          this.numCount = 60
        })
      },
      funUpdateCapture () {
        this.strCapture = STRCAPTURE + '?' + new Date().getTime()
      },
      verifyData () {
        const { partnerPhone, code } = this.postData
        this.arrErrorList = []
        if (!/^1[2|3|4|5|6|7|8|9]\d{9}$/.test(partnerPhone)) {
          this.arrErrorList.push('请输入手机号码')
        }
        if (!(/^\d{4}$/.test(code))) {
          this.arrErrorList.push('请输入验证码')
        }
        return this.arrErrorList.length <= 0
      },
      funSubmit () {
        const { partnerPhone, code } = this.postData
        if (!this.verifyData()) {
          Toast({
            message: this.arrErrorList[0],
            forbidClick: true
          })
        }
        const myToast = Toast.loading({
          message: '提交中...',
          duration: 1000 * 100,
          forbidClick: true
        })
        partnerLogin(partnerPhone, code).then(res => {
          myToast.clear()
          if (res.status) {
            Toast({
              type: 'success',
              message: '提交成功',
              forbidClick: true,
              onClose: () => {
                this.$router.replace({ path: '/partner' })
              }
            })
            return
          }
          Toast(res.msg)
        }).catch(err => {
          myToast.clear()
          Toast(err.msg)
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .wrapper {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    min-height: 100vh;
    background: #fff;
  }

  .logo {
    width: 147px;
    height: 50px;
  }

  .welcome {
    margin-top: 12px;
    line-height: 25px;
    font-size: 18px;
    font-weight: bold;
    color: #666;
  }

  .form-wrap {
    li {
      position: relative;
      left: 0;
      top: 0;
      display: flex;
      align-items: flex-start;
      width: 327px;
      padding: 13px 12px;
      margin-top: 12px;
      border: 1px solid #E8E8E8;
      border-radius: 4px;

      &:nth-of-type(1) {
        margin-top: 79px;
      }
    }

    label {
      display: flex;

      span {
        line-height: 22px;
        font-size: 16px;
        font-weight: 500;
        color: #333;

        &.label-code:nth-of-type(5), {
          visibility: hidden;
        }
      }
    }

    .value-wrap {
      flex: 1;
      display: flex;
      margin-left: 9px;
    }

    input,
    textarea {
      width: 100%;
      min-height: 20px;
      padding-top: 2px;
      line-height: 20px;
      font-size: 15px;
      color: #333;
      word-break: break-all;
      resize: none;
      outline: 0 none;
      overflow: hidden;
      background: transparent;
      -webkit-text-fill-color: #333;
      opacity: 1;

      &::-webkit-input-placeholder {
        color: #999;
        -webkit-text-fill-color: #999;
        opacity: 1;
      }
    }

    .capture-img {
      @include vertical-center;
      right: 14px;
      z-index: 1;
      display: block;
      width: 125px;
      height: 31px;
      border-radius: 4px;
      overflow: hidden;
    }

    .get-code {
      @include vertical-center;
      right: 0;
      z-index: 1;
      display: block;
      min-width: 87px;
      padding: 6px;
      line-height: 22px;
      font-size: 16px;
      font-weight: 500;
      color: #C9A585;
    }
  }

  .submit {
    width: 327px;
    height: 45px;
    margin-top: 41px;
    border-radius: 4px;
    line-height: 45px;
    font-size: 15px;
    font-weight: 500;
    color: #fff;
    background: linear-gradient(90deg, #DBC3A9 0%, #C7AB8A 100%);
    box-shadow: 0 14px 9px -10px rgba(219, 208, 194, 1);
  }
</style>
