<template>
  <van-popup
    v-model="isShow"
    @click-overlay="cancle"
    position="bottom"
  >
    <section class="bind-cart-wrapper">
      <article class="title">
        <span>激活详情</span>
        <img @click="cancle" src="../../images/cme/close.png" />
      </article>
      <article class="tip">
        <div class="left">
          <div class="img-wrapper"><img :class="{'android': isAndroid}" src="../../images/cme/phrase2/info.png"/></div>
          <span>激活码用于学习课程，您可以从订单详情或您的实体卡查看激活码。激活码一旦使用，不可退回。</span>
        </div>
      </article>
      <section class="input-wrapper">
        <div class="code">
          <van-field
            maxlength="26"
            v-model="activationCode"
            label="激活码"
            placeholder="请输入激活码"
            @click.native="onInputClick"
          />
          <!-- <span>激活码</span>
          <input type="text" maxlength="26" v-model="activationCode" placeholder="请输入激活码"> -->
          <div v-show="!isAndroid" class="cover"></div>
        </div>
        <span class="error">{{errorMsg}}</span>
        <img v-show="!!activationCode" @click="clear" src="../../images/cme/phrase2/close.png" />
      </section>

      <article class="bottom">
        <van-button @click="confirm" size="large" round color="#449284">确认激活</van-button>
      </article>
    </section>
  </van-popup>
</template>
<script>
export default {
  props: {
    isShow: {
      type: Boolean,
      default: true
    },
    changeErrorMsg: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      isAndroid: __isAndroid,
      activationCode: '',
      errorMsg: ''
    };
  },

  //
  watch: {
    activationCode(val) {
      if(!val) {
        this.errorMsg = '请输入激活码';
      } else {
        this.errorMsg = '';
      }
      this.$nextTick(() => {
        this.activationCode = val.replace(/\s/g,'').replace(/.....(?!$)/g,'$& ');
      });
    },
    changeErrorMsg(val) {
      if(val) {
        this.errorMsg = val;
      }
    },
    isShow(val) {
      this.activationCode = '';
      this.errorMsg = '';
    }
  },

  methods: {
    cancle() {
      this.$emit("cancle");
    },
    confirm() {
      this.$sendBuriedData({
        // component_tag: `882#88207`
        component_tag: `882#88225`
      });
      if(!this.activationCode) {
        this.errorMsg = '请输入激活码';
        return;
      };
      if(this.activationCode.length != 23) {
        this.errorMsg = '请输入正确的激活码';
        return;
      }
      let aCode = JSON.parse(JSON.stringify(this.activationCode));
      aCode = aCode.replace(/\s/g,'');
      this.$emit("confirm", aCode);
    },
    clear() {
      this.activationCode = ''
    },
    onInputClick() {
      this.$sendBuriedData({
        component_tag: `882#88208`
      });
    }
  }
};
</script>
<style  lang="less" scoped>
.bind-cart-wrapper {
  width: 100%;
  display: flex;
  line-height: 1;
  padding: 10px 15px;
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 1);
  // box-shadow: 0px -2px 10px 0px rgba(0, 0, 0, 0.1);
  .title {
    display: flex;
    flex-direction: row;
    font-size: 18px;
    font-weight: 700;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin-top: 20px;
    img {
      position: relative;
      top: 0;
      right: -120px;
      width: 12px;
      height: 12px;
    }
  }
  .tip {
    display: flex;
    margin-top: 40px;
    .left {
      display: flex;
      line-height: 1.2;
      font-size: 12px;
      .img-wrapper {
        width: 20px;
        // margin-right: 4px;
        img {
          position: relative;
          top: 1px;
          width: 12px;
          height: 12px;
        }
        img.android {
          top: 1.5px;
          width: 11.5px;
        }
      }

      span {
        display: inline-block;
        margin-left: 4px;
      }
      color: #979899;
    }
  }
  .input-wrapper {
    position: relative;
    .code {
      position: relative;
      display: flex;
      flex-direction: column;
      padding: 20px 0;
      padding-top: 0;
      .cover {
        width: 286px;
        position: absolute;
        top: 21.5px;
        z-index: 1000;
        border-top: 4px solid #fff;
        margin-left: 57px;
        margin-right: 1px;
        border-radius: 4px;
      }
    }
    .error {
      position: absolute;
      top: 66px;
      left: 56px;
      display: inline-block;
      color: red;
      font-size: 12px;
    }

    img {
      position: absolute;
      top: 33px;
      right: 12px;
      width: 15px;
      height: 15px;
    }
  }

  .bottom {
    display: flex;
    margin-bottom: 40px;
  }
}
</style>
