<template>
  <div>
    <div class="card box-card">
      <div class="card-body">
        <i class="logo xy-v2-aq"></i>
        <p class="title">安全验证</p>
        <div class="box-tips">
          <p v-if="isSendCode">验证码短信已发送至：{{ telephone }}，请注意查收</p>
          <p v-else>为了保障账户安全，请进行安全验证，接收验证码手机号：{{ telephone }}</p>
        </div>
        <div class="t-MT30">
          <el-input
            class="verify-input"
            :maxlength="6"
            type="text"
            autocomplete="off"
            v-model="code"
          />
          <xy-countdown-button @click="sendCode" :is-send.sync="isSend"></xy-countdown-button>
        </div>
        <el-button
          class="verify-button"
          type="primary"
          @click="verifyCode"
          :disabled="code.length !== 6"
        >
          <span>确定</span>
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import XyCountdownButton from '../countdown-button';

export default {
  name: 'XyVerification',
  components: {
    XyCountdownButton,
  },
  props: {
    isSendCode: Boolean,
    telephone: [String, Number],
  },
  data() {
    return {
      code: '',
      isSend: false,
    };
  },
  watch: {
    isSendCode(val) {
      this.isSend = val;
    },
    isSend(val) {
      if (val === false) {
        this.$emit('update:isSendCode', false);
      }
    },
  },
  methods: {
    sendCode() {
      this.$emit('sendCode', true);
    },
    verifyCode() {
      this.$emit('verifyCode', this.code);
    },
  },
};
</script>

<style scoped>
  .card {
    border: 1px solid #ebeef5;
    border-radius: 4px;
    background-color: #fff;
    overflow: hidden;
    color: #303133;
    transition: 0.3s;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  .card .card-body {
    padding: 20px;
  }
  .card.box-card {
    margin: 20px auto 28px auto;
    text-align: center;
  }
  .logo {
    margin-top: 120px;
    font-size: 40px;
    color: #20a0ff;
  }
  .title {
    font-size: 16px;
    color: #303133;
    margin-top: 20px;
    margin-bottom: 28px;
  }
  .box-tips p {
    font-size: 12px;
    margin-top: 7px;
    color: #999;
  }
  .verify-input {
    display: inline-block;
    width: 120px;
  }
  .verify-button {
    width: 300px;
    margin: 30px 0 238px 0;
  }
</style>
