<template>
  <div class="openCardDivC">
    <div :class="['ui-modal-body', 'animate__animated', animateName]" :style="styleData.uiModalBody" @click="btnClick"></div>
  </div>
</template>

<script>
  import api1 from '../../../../util/apis1.js';
  let { shopGiftMainCheck, drawShopGift } = api1;
  import { closeGiftPop, jumpGetCoupon } from '../../../../util/shopGiftBag.js';
  export default {
    // isMember 1是会员  0不是会员
    props: ['styleData', 'giftBagData', 'isTemplate', 'isMember'],
    data() {
      return {
        animateName: null,
        shopGiftDataAll: null,
        oldData: {},
        supportLink: true,
        autoHalfType: false
      };
    },
    created() {
      var currentURL = window.location.href;
      console.log(currentURL, "currentURL");
      console.log(JSON.parse(sessionStorage.getItem('joinInfo')).actUrl, "currentURL2222");
      console.log(this.isTemplate, 'isTemplate');
      console.log(this.giftBagData, '开卡礼包模板数据');
      console.log(this.giftBagData.autoHalfType, '开卡礼包模板数据autoHalfType');
      console.log(this.isMember, 'isMember');
      this.oldData = {
        flag: this.giftBagData.flag,
        memberUser: this.isMember,
        name: this.giftBagData.name,
        position: this.giftBagData.position,
        visitor: '0'/*  */
      }
      console.log(this.oldData, "oldData");
      this.initData();
    },
    methods: {
      initData() {
        this.styleData.uiModalBody['width'] = this.giftBagData.biGift.imgWidth / 100 + 'rem';
        this.styleData.uiModalBody['height'] = this.giftBagData.biGift.imgHigh / 100 + 'rem';
        this.styleData.uiModalBody['left'] = `calc(50% - ${this.giftBagData.biGift.imgWidth / 100 / 2}rem)`;
        this.styleData.uiModalBody['top'] = `calc(50% - ${this.giftBagData.biGift.imgHigh / 100 / 2}rem)`;

        if (this.giftBagData.biGift) {
          console.log('lys111');
          if (this.giftBagData.biGift.isMember == 0) {
            // 非会员弹窗
            if (this.giftBagData.autoHalfType) {
              console.log('lys3330000', this.giftBagData.autoHalfTyp);
              // 半屏
              // const inJdApp = jmfe.isApp('jd')
              // const isIOS = jmfe.isIOS()
              // const currentVersion = jmfe.getAppVersion('jd')
              // const compareResult = jmfe.versionCompare(currentVersion, '13.6.2')
              // const hasNewOpenCardUrl = this.giftBagData.newOpenCardUrl
              // if (!inJdApp || compareResult != 1 || !hasNewOpenCardUrl || !isIOS){
              //   console.log('安卓手机');
              //   this.supportLink = false
              //   this.animateName = this.giftBagData.enterAnimateName
              //   this.styleData.uiModalBody['background-image'] = `url(${this.giftBagData.biGift.showImg})`;
              // }else{
              //   console.log('苹果手机');
              this.halfFun()
              // }
            } else {
              console.log('lys444');
              // 未开启
              this.animateName = this.giftBagData.enterAnimateName
              this.styleData.uiModalBody['background-image'] = `url(${this.giftBagData.biGift.showImg})`;
            }
          } else {
            console.log('lys555');
            // 会员弹窗
            this.animateName = this.giftBagData.enterAnimateName
            this.styleData.uiModalBody['background-image'] = `url(${this.giftBagData.biGift.showImg})`;
          }
        }
      },
      halfFun() {
        console.log("打印鸿蒙参数909090909090");
        window.newHalfScreenCallback = (result) => {
          let resultObj = JSON.parse(result)
          if (window.navigator.userAgent.indexOf('Android') > -1) {
            let datas = JSON.parse(resultObj.data)
            resultObj.data = datas
          }
          if (resultObj.status == '0') {
            if (resultObj.data.type == 'onLayerReady') {
            }
            if (resultObj.data.type == 'layerShowSucces') {
            }
            if (resultObj.data.type == 'onLayerLoading') {
            }
            if (resultObj.data.type == 'clickClose') {
              alert('点击了关闭弹窗按钮')
            }
            if (resultObj.data.type == 'layerClose') {
              this.closeHalf()
              // alert('已经关闭了半屏弹窗')
            }
          } else if (resultObj.status == '-1') {
            console.log("错误,错误信息:" + resultObj.msg)
          } else if (resultObj.status == '-2') {
            console.log("方法不存在")
          }
        }
        //京东app版本号大于13.6.3才支持使用半屏弹窗
        const inJdApp = jmfe.isApp('jd')
        const isIOS = jmfe.isIOS()
        const currentVersion = jmfe.getAppVersion('jd')
        const compareResult = jmfe.versionCompare(currentVersion, '13.6.2')
        const compareResult15 = jmfe.versionCompare(currentVersion, '15.0.20')
        const hasNewOpenCardUrl = this.giftBagData.newOpenCardUrl
        console.log(isIOS, "isIOS");
        console.log(currentVersion, "currentVersion");
        console.log(compareResult15, "compareResult15");
        // console.log(compareResult,"compareResult");
        // console.log(hasNewOpenCardUrl,"hasNewOpenCardUrl-------");
        // console.log("打印鸿蒙参数");
        // console.log(jmfe,"jmfe")
        // console.log(jmfe.getAppVersion,"jmfe.getAppVersion")
        // console.log(jmfe.versionCompare,"jmfe.versionCompare")
        // console.log(window.XWebView,"window.XWebView")
        if (!jmfe || !jmfe.isApp || !jmfe.getAppVersion || !jmfe.versionCompare || !window.XWebView || !window.XWebView.callNative) {
          this.supportLink = false
          this.animateName = this.giftBagData.enterAnimateName
          this.styleData.uiModalBody['background-image'] = `url(${this.giftBagData.biGift.showImg})`;
          return
        } else {
          if (!inJdApp || compareResult != 1 || !hasNewOpenCardUrl || (!isIOS && compareResult15 == -1)) {
            this.supportLink = false
            this.animateName = this.giftBagData.enterAnimateName
            this.styleData.uiModalBody['background-image'] = `url(${this.giftBagData.biGift.showImg})`;
          } else {
            let cardUrl = hasNewOpenCardUrl + '&memberSceneType=xview&closeBtn=0'
            console.log(cardUrl, 'cardUrlLys');
            let params = {
              url: encodeURIComponent(cardUrl),
              targetType: 2,
              heightRateFromContainer: 0.7,
              layoutType: 1,
              backgroundColor: '0000009b',
              pageMultiTimes: 1,
              blankCls: 1,
              needAutoClose: 1,
              hideNativeCloseBtn: 0,
              // targetName: JSON.parse(sessionStorage.getItem('joinInfo')).actUrl,
              // targetName: window.location.href,
            }
            window.XWebView && window.XWebView.callNative(
              "JDXViewPlugin",//默认参数，固定值JDXViewPlugin
              "showXView2",//默认参数，固定值showXView2
              JSON.stringify(params),//上面的参数params转成JSON字符串
              "newHalfScreenCallback",//上面定义好的回调函数
              "1"//默认参数，固定值1 
            )
          }
        }
      },
      closeHalf() {
        this.doFrontBuryPoint({ code: 'spq-cksp1', value: '', event: 'closeBag' })
        shopGiftMainCheck(shopGiftMain => {
          this.shopGiftDataAll = shopGiftMain.data;
          this.isMember = this.shopGiftDataAll.memberUser;
          if (this.isMember == 0) {
            this.giftBagData.outAnimateName = this.shopGiftDataAll.outAnimateName
            this.giftBagData.enterAnimateName = this.shopGiftDataAll.enterAnimateName
            this.giftBagData.biGift = this.shopGiftDataAll.biGift
            this.animateName = this.giftBagData.enterAnimateName
            this.styleData.uiModalBody['background-image'] = `url(${this.giftBagData.biGift.showImg})`;
          } else {
            this.closePop()
          }
        })
      },
      closePop() {
        if (this.isTemplate) {
          this.$toast('仅供预览');
        } else {
          if (this.isMember == 0) {
            // 非会员
            // closeGiftPop(this, () => { });
          } else {
            // 成为会员直接关闭弹窗
            this.animateName = this.giftBagData.outAnimateName
            drawShopGift(this.oldData, drawData => {
              this.$toast('收下礼包成功');
            });
            closeGiftPop(this, () => { });
          }
        }
      },
      btnClick() {
        if (this.giftBagData.autoHalfType) {
          if (this.isMember == 0) {
            if (this.supportLink) {
              this.halfFun()
            } else {
              this.animateName = this.giftBagData.outAnimateName
              drawShopGift(this.oldData, drawData => {
                this.$toast('收下礼包成功');
              });
              closeGiftPop(this, () => { });
              jumpGetCoupon(this.giftBagData.biGift.url, () => { });
            }
          } else {
            // 直接领奖关闭弹窗
            this.animateName = this.giftBagData.outAnimateName
            drawShopGift(this.oldData, drawData => {
              this.$toast('收下礼包成功');
            });
            closeGiftPop(this, () => { });
            jumpGetCoupon(this.giftBagData.biGift.url, () => { });
          }
        } else {
          this.animateName = this.giftBagData.outAnimateName
          drawShopGift(this.oldData, drawData => {
            this.$toast('收下礼包成功');
          });
          closeGiftPop(this, () => { });
          jumpGetCoupon(this.giftBagData.biGift.url, () => { });
        }

      }
    }
  };
</script>

<style lang="scss" scoped="scoped">
  .openCardDivC {
  }
</style>
