<template>
  <div>
    <!--标题-->
    <ToPBarNormal :title="title"></ToPBarNormal>
    <!--提示信息-->
    <div class="result-cont">
      <p style="padding:10px">我们已经给您的邮箱<a> {{email}} </a>发送了一条验证码，请及时查看邮件。</p>
      <div class="phone_edit">
        <group>
          <x-input placeholder="请输入验证码" v-model="code">
            <span slot="right" v-show="selected" class="phone_vcode" @click="_getCode"><a>重新发送</a></span>
            <span slot="right" v-show="!selected" class="phone_vcode">重新发送({{second}}s)</span>
          </x-input>
        </group>
      </div>
      <div style="margin-top: 20px;background: #ccc" class="login-exit" v-show="!code">完成</div>
      <div style="margin-top: 20px" class="login-exit" @click="_ok" v-show="code">完成</div>
    </div>
  </div>
</template>

<script>
  import ToPBarNormal from '../../../components/topBar/TopBarNormal'
  import {XInput, Group} from 'vux'

  export default {
    components: {
      ToPBarNormal,
      XInput,
      Group
    },
    data() {
      return {
        title: '请输入验证码',
        code: '',
        email: '',
        second: 59,
        selected: false,
        id: '',
        obj: {}
      }
    },
    mounted() {
      this._getQuery();
    },
    methods: {
      _getQuery() {
        this.email = this.$route.query.email;
        this.obj = JSON.parse(localStorage.getItem('fLoginUser'));
        this.id = this.obj.id;
        this.selected = false;
        this.second = 59;
        this._count();
      },
      //提示
      _msg(msg) {
        this.$vux.toast.show({
          type: 'text',
          width: '10em',
          text: msg,
          time: 1000
        });
      },
      //完成
      _ok() {
        this.$store.dispatch('SystemManage/bindEmail', {code: this.code}).then(() => {
          if (this.$store.state.SystemManage.model) {
            this._msg('修改成功');
            this.$router.go(-2);
          }
        });
      },
      //发送验证码
      _getCode() {
        this.selected = false;
        this.second = 59;
        let data = {
          email: this.email,
        };
        this.$store.dispatch('SystemManage/getEmailCode', data).then(() => {
          if (this.$store.state.SystemManage.success) {
            this._count();
          } else {
            this._msg('验证码发送失败');
          }
        });
      },
      //计数
      _count() {
        let second = this.second;
        if (second === 0) {
          this.selected = true;
          this.second = 59;
          return;
        }
        setTimeout(() => {
          this.second = second - 1;
          this._count();
        }, 1000)
      },
    }
  }
</script>
