<template>
  <div style="margin-top: 20px" v-loading="loading">
    <el-form label-width="80px" style="width:400px" :rules="rules" :model="phoneBindingForm">
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="phoneBindingForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="verifyCode">
        <el-input placeholder="验证码内容" v-model="phoneBindingForm.verifyCode" class="input-with-select">
          <el-button slot="append" @click="getVerifyCode" :disabled="hasVerifyCode">{{hasVerifyCode ? codeTime :
            '获取验证码'}}
          </el-button>
        </el-input>
        <span v-if="hasVerifyCode">验证码已发送到您手机上</span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="bindingMobile">完成绑定</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
  import Http from '@/js/http.js'
  import {storageGet} from '@/js/index'
  import {notify} from '@/constants/index'

  export default {
    name: "BindingMobile",
    data() {
      return {
        phoneBindingForm: {
          phone: '',
          verifyCode: ''
        },
        loading: false,
        hasVerifyCode: false,
        codeTime: 60,
        user: {},
        rules: {
          verifyCode: [
            {required: true, message: '请输入验证码', trigger: 'blur'},
            {min: 6, max: 6, message: '验证码长度为6个字符', trigger: 'blur'}
          ],
          phone: [
            {required: true, message: '请输入手机号', trigger: 'blur'},
            {min: 11, max: 11, message: '手机号不合法', trigger: 'blur'}

          ],
        }
      }
    },
    methods: {
      setUserInfo() {
        let userId = storageGet('user') && storageGet('user').userVO.id;
        Http.get(`/api/user/${userId}`).then(res => {
          this.user = res.userVO
        })
      },
      getVerifyCode() {
        let params = {
          id: this.user.id,
          mobile: this.phoneBindingForm.phone
        }
        Http.put('/api/verify/mobile', params).then((res) => {
          if (res.msg == "ERROR") {
            notify('error', res.data);
          } else {
            notify('success', res.data);
            this.hasVerifyCode = true;
            let _this = this;
            let codeTimer = setInterval(function () {
              if (_this.codeTime > 0) {
                _this.codeTime--;
              } else {
                clearInterval(codeTimer);
                _this.hasVerifyCode = false;
                this.codeTime = 10;
              }
            }, 1000)
          }
        }).catch(err => {
          notify('error', '绑定手机失败:' + err.data);
        })
      },
      bindingMobile() {
        let params = {
          id: this.user.id,
          mobile: this.phoneBindingForm.phone,
          verifyCode: this.phoneBindingForm.verifyCode
        };

        Http.put('/api/user/mobile', params).then(res => {
          if (res.msg == "ERROR") {
            notify('error', '绑定失败：' + res.data);
            this.active = 1;
            this.phoneBindingForm.phone = '';
            this.phoneBindingForm.verifyCode = '';
            this.codeTime = 60;
            this.hasVerifyCode = false;
          } else {
            notify('success', '绑定成功');
            this.$router.push({path: '/personal/phoneBinding'});
          }
        }).catch(err => {
          notify('error', err.data);
        })
      },
      showLoading() {
        this.loading = true
      },
      hideLoading() {
        this.loading = false
      },
    },
    mounted() {
      this.setUserInfo()

    }
  }
</script>

<style scoped>

</style>
