<template>
  <div class="wrapper">
    <img
      class="logo"
      src="./image/logo@2x.png" alt="">
    <ul class="form-wrap">
      <li>
        <label>
          <span
            v-for="(str, index) in '+86'"
            :key="index">{{ str }}</span>
        </label>
        <div class="value-wrap">
          <input
            type="tel"
            placeholder="请输入手机号码"
            v-myBlur
            v-model.trim="postData.phone"
            @input="funLimitLength">
        </div>
      </li>
      <li>
        <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 class="footer">
      <van-checkbox
        v-model="checked"
        checked-color="#D32323"></van-checkbox>
      <p class="tip-text">
        <span
          class="label"
          v-for="(str, index) in '登录听邦即代表您已同意'"
          :key="'a' + index">{{ str }}</span>
        <span
          class="value"
          v-for="(str, index) in '《用户协议》'"
          :key="'b' + index" @click="jumpAgreement">{{ str }}</span>
        <span
          class="label">与</span>
        <span
          class="value"
          v-for="(str, index) in '《隐私协议》'"
          :key="'c' + index" @click="jumpPrivacy">{{ str }}</span>
      </p>
    </div>
  </div>
</template>

<script>
import { Toast, Checkbox } from 'vant'
import { sendSMS } from '../api/common'
import { login } from './api'
import { isMobile, isSmscode } from '../utils/validate'
import { clearLoginInfo } from '../utils'

export default {
  name: 'login',
  components: {
    'van-checkbox': Checkbox
  },
  data () {
    return {
      postData: {
        phone: '',
        code: ''
      },
      numCount: 60,
      timer: null,
      arrErrorList: [],
      checked: false
    }
  },
  async mounted () {
    await this.$nextTick()
  },
  methods: {
    jumpAgreement () {
      location.href = '//tingbang-h5.codedreamit.com/agreement'
    },
    jumpPrivacy () {
      location.href = '//tingbang-h5.codedreamit.com/privacy'
    },
    funLimitLength () {
      const { phone, code } = this.postData
      this.postData.phone = phone.replace(/[^\d]/g, '').slice(0, 11)
      this.postData.code = code.replace(/[^\d]/g, '').slice(0, 6)
    },
    funCutDown () {
      clearInterval(this.timer)
      this.timer = setInterval(() => {
        if (this.numCount === 0) {
          clearInterval(this.timer)
          this.numCount = 0
          return
        }
        this.numCount--
      }, 1000)
    },
    // 获取验证码
    async funGetCode () {
      const numCount = this.numCount
      const { phone } = this.postData
      if (numCount < 60 && numCount > 0) {
        return
      }
      this.numCount = 60
      if (!isMobile(phone)) {
        Toast('请输入手机号码')
        return
      }

      this.funCutDown()
      try {
        const { status, msg } = await sendSMS(phone)
        if (status) {
          Toast('发送成功')
        } else {
          Toast(msg)
          clearInterval(this.timer)
          this.numCount = 60
        }
      } catch (err) {
        clearInterval(this.timer)
        this.numCount = 60
      }
    },
    verifyData () {
      const { phone, code } = this.postData
      this.arrErrorList = []
      if (!isMobile(phone)) {
        this.arrErrorList.push('请输入手机号码')
      }
      if (!isSmscode(code)) {
        this.arrErrorList.push('请输入验证码')
      }
      if (!this.checked) {
        this.arrErrorList.push('请勾选用户协议及隐私政策')
      }
      return this.arrErrorList.length <= 0
    },
    async funSubmit () {
      const { phone, code } = this.postData
      if (!this.verifyData()) {
        Toast({
          message: this.arrErrorList[0],
          forbidClick: true
        })
        return
      }
      const myToast = Toast.loading({
        message: '提交中...',
        duration: 1000 * 100,
        forbidClick: true
      })
      try {
        const { status, data, msg } = await login(phone, code)
        myToast.clear()
        if (status) {
          const { token } = data
          clearLoginInfo()
          this.$nextTick(() => {
            this.$store.commit('common/UPDATE_PHONE', phone)
            this.$store.commit('common/UPDATE_TOKEN', token)

            this.$cookie.set('afhousephone', phone, '30d')
            this.$cookie.set('afhousetoken', token, '30d')
          })

          Toast({
            type: 'success',
            message: '登录成功',
            forbidClick: true,
            onClose: () => {
              this.$router.go(-1)
            }
          })
          return
        }
        Toast(msg)
      } catch (err) {
        myToast.clear()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  position: relative;
  left: 0;
  top: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  padding-bottom: 50px;
  background: linear-gradient(180deg, #000000 0%, #272727 100%);
}

.logo {
  width: 121px;
  height: 138px;
}

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

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

  label {
    display: flex;
    align-items: center;
    margin-right: 9px;

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

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

  .value-wrap {
    flex: 1;
    display: flex;
    align-items: center;
  }

  input,
  textarea {
    width: 100%;
    height: 22px;
    font-size: 16px;
    color: #fff;
    word-break: break-all;
    resize: none;
    outline: 0 none;
    overflow: hidden;
    background: transparent;
    -webkit-text-fill-color: #fff;
    opacity: 1;

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

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

.submit {
  width: 343px;
  height: 50px;
  margin-top: 39px;
  background: #D32323;
  border-radius: 25px;
  font-size: 18px;
  color: #FFFFFF;
  line-height: 50px;
  letter-spacing: 1px;
}

.footer {
  position: absolute;
  left: 0;
  bottom: 24px;
  z-index: 1;
  display: flex;
  align-items: center;
  width: 100%;
  justify-content: center;

  .tip-text {
    display: flex;
    align-items: center;
    margin-left: 6px;

    span {
      font-size: 12px;
      font-family: STSongti-SC-Bold, STSongti-SC;
      font-weight: bold;
      color: #848484;
      line-height: 17px;

      &.value {
        color: #D32323;
      }
    }
  }
}
</style>
