<template>
  <div
    class="activityImg"
    :style="styleData.activityImg"
    ref="hbFriendsHelp"
    @click="styleClick('click', 'hbFriendsHelp')"
    @mousemove="styleClick('mousemove', 'hbFriendsHelp')"
    @mouseout="styleClick('mouseout')"
  >
    <img
      :style="styleData.activityTheme"
      :src="styleData.activityTheme.imageUrl"
      alt=""
    />
    <div :style="styleData.activityHb">
      <template
        v-if="
          (friendsHelpList.isClick && friendsHelpList.canAssist == 2) ||
          (friendsHelpList.isClick && friendsHelpList.canAssist == 4)
        "
      >
        <div :style="styleData.startTime"></div>
        <div v-show="isShowEveryOpenPacketMoney" :style="styleData.hbDiv">
          <div :style="styleData.redEnvelopeTips" style="">本次红包拆得</div>
          <div
            :class="{ marginTop1: isShowEveryOpenPacketMoney }"
            :style="styleData.hbDiv2"
          >
            &yen;{{ curOpenPacketMoney }}
          </div>
        </div>
        <div
          :style="styleData.startInviteContent1"
          :class="{ marginTop1: isShowEveryOpenPacketMoney }"
        >
          每邀请<span :style="styleData.hbText3">{{
            friendsHelpList.hbShareCount
          }}</span
          >名好友助力可拆<span :style="styleData.hbText3">1</span>次红包
        </div>
        <div
          :style="styleData.startInviteContent2"
          v-if="friendsHelpList.dayHbLimit == 1"
        >
          优惠金挺好的，今天可拆{{ friendsHelpList.dayHbCount }}次！点击抢！
        </div>
        <div :style="styleData.startInviteContent2" v-else>
          优惠金挺好的，今天不限拆红包次数！点击抢！
        </div>

        <div
          :style="styleData.startInviteBtn"
          :class="{ marginTop2: isShowEveryOpenPacketMoney }"
          v-lz-click="'2'"
          @click="invite"
          v-if="
            friendsHelpList.hbCount == 0 && friendsHelpList.involvementFlag == 0
          "
        ></div>
        <div
          :style="styleData.startInviteBtn2"
          :class="{ marginTop2: isShowEveryOpenPacketMoney }"
          v-lz-click="'2'"
          @click="invite"
          v-else-if="
            friendsHelpList.hbCount == 0 && friendsHelpList.involvementFlag == 1
          "
        ></div>

        <div
          :style="styleData.startOpenPacketBtn"
          id="invite-btn"
          :class="{ marginTop2: friendsHelpList.hbCount > 0 }"
          v-lz-click="'16'"
          @click="openPacket"
          v-else-if="friendsHelpList.involvementFlag == 0"
        ></div>
        <div
          :style="styleData.startOpenPacketBtn2"
          id="invite-btn"
          :class="{ marginTop2: friendsHelpList.hbCount > 0 }"
          v-lz-click="'16'"
          @click="openPacket"
          v-else-if="friendsHelpList.involvementFlag == 1"
        ></div>

        <div
          :style="styleData.startOpenPacketBtnText"
          v-if="friendsHelpList.hbCount > 0"
        >
          可拆红包次数：{{ friendsHelpList.hbCount }}次
        </div>
      </template>
      <template
        v-else-if="
          friendsHelpList.canAssist != 2 && friendsHelpList.canAssist != 4
        "
      >
        <div :style="styleData.activityStartTime"></div>
        <div :style="styleData.activityContent">我正在拆红包，快来帮帮我~</div>
        <div :style="styleData.chbStyle">
          <img
            v-if="friendsHelpList.involvementFlag == 0"
            :style="styleData.activityHelpBtn"
            src="https://img10.360buyimg.com/imgzone/jfs/t1/38491/28/2153/6412/5cbfd567Ef532eef3/a45f72be6db33488.png"
            alt=""
            @click="help"
          />
          <img
            v-else
            :style="styleData.activityHelpBtn"
            src="https://img10.360buyimg.com/imgzone/jfs/t1/92740/30/15082/12926/5e6ee42aEc1b04a89/6dfcc01a2bb9d809.png"
            alt=""
            v-lz-click="'3'"
            @click="help"
          />
          <img
            :style="styleData.activityMyAlsoOpen"
            src="https://img10.360buyimg.com/imgzone/jfs/t1/32323/17/14604/6304/5cbfd567E183c50e0/e00634d5bb4c1d4c.png"
            alt=""
            @click="joinInThisActivity"
          />
        </div>
      </template>
      <template v-else>
        <div :style="styleData.startTime"></div>
        <div :style="styleData.startContent">
          <div :style="styleData.hbText4">
            每邀请<span :style="styleData.hbText3">{{
              friendsHelpList.hbShareCount
            }}</span
            >名好友助力
          </div>
          <div :style="styleData.hbText4">
            可拆<span :style="styleData.hbText3">1</span>次红包
          </div>
          <div :style="styleData.hbText2">
            活动内最多可拆<span :style="styleData.hbText3">{{
              friendsHelpList.activityHbCount
            }}</span
            >次
          </div>
          <div :style="styleData.hbText">~红包满天飞，快来抢啊~</div>
        </div>
        <!-- v-if="friendsHelpList.involvementFlag == 0" -->
        <div
          :style="styleData.startBtn"
          @click="sendHb"
        ></div>
        <!-- <div
          v-else
          :style="styleData.startBtn2"
          class="start_btn2"
          @click="sendHb"
        ></div> -->
      </template>
    </div>
  </div>
</template>

<script>
import apis1 from "../../../util/apis1.js";
import dateUtil from "../../../util/dateUtil.js";
import store from "../../../store/store.js";
import {
  HbFriendsHelpPopComService,
  HbPrizePopComService,
} from "../../../util/comCreate.js";
import { allShare } from "../../../js/share.js";
let {
  hbShareActivityList,
  setShare,
  hbShareHelpFriendsList,
  fansDrawList,
  hbShareFollowList,
} = apis1;

export default {
  components: {
    store,
  },
  props: [
    "isTemplate",
    "datas",
    "keyData",
    "styleData",
    "templateData",
    "isWebClick",
    "modifyContent",
  ],
  data() {
    return {
      friendsHelpList: [],
      shareUserId: "",
      isShowEveryOpenPacketMoney: false,
      curOpenPacketMoney: 0,
      url: "",
      isDisabled: false,
      styleDataOld: {},
    };
  },

  created() {
    //深拷贝重置需要
    this.styleDataOld = JSON.parse(JSON.stringify(this.styleData));
    this.initData();
  },
  mounted() {},
  methods: {
    styleClick(type, refName) {
      if (this.isWebClick) {
        //传值给B端
        let byValue = {
          modifyContent: this.modifyContent,
          refName: refName,
          styleData: this.styleData,
          c_this: this,
          styleDataOld: this.styleDataOld,
        };
        this.$emit("opertaionCom", type, byValue);
      }
    },
    initData() {
      var datasAll = this.templateData;

      if (this.isTemplate == true) {
        // this.$toast("活动预览，仅供查看");
        if (datasAll) {
          this.friendsHelpList.isClick = false;
          this.friendsHelpList.hbShareCount =
            datasAll.draw.prizeList[0].invitationCount;
          this.friendsHelpList.involvementFlag =
            datasAll.activeConditions.involvementFlag;
          this.friendsHelpList.canAssist = 4;
          if (datasAll.hbShare.activityHbLimit == "0") {
            this.friendsHelpList.activityHbCount = "不限";
          } else {
            this.friendsHelpList.activityHbCount =
              datasAll.hbShare.activityHbCount;
          }
        } else {
          this.friendsHelpList.isClick = false;
          this.friendsHelpList.hbShareCount = 5;
          this.friendsHelpList.hbCount = 1;
          this.friendsHelpList.canAssist = 4;
        }
      } else {
        this.shareUserId = store.state.shareUserId;
        var shareUserId = this.shareUserId;
        this.url = location.href;
        hbShareActivityList(shareUserId, (data) => {
          this.friendsHelpList = data.data;
          if (this.friendsHelpList.activityHbLimit == 0) {
            this.friendsHelpList.activityHbCount = "不限";
          }
          this.publicData.$emit("hbCanAssist", this.friendsHelpList.canAssist);
        });
      }
    },

    help() {
      if (this.isTemplate == true) {
        // this.$toast("活动预览，仅供查看");
        return;
      }
      let actStatus = sessionStorage.getItem("actStatus");

      if (actStatus == 2) {
        this.$toast("活动已结束");
        return;
      }
      if (actStatus == 1) {
        this.$toast("活动尚未开始");
        return;
      }
      this.shareUserId = store.state.shareUserId;
      var shareUserId = this.shareUserId;
      hbShareHelpFriendsList(shareUserId, (data) => {
        if (data.data == 0) {
          HbFriendsHelpPopComService.create(0, this.url, (submit) => {
            HbFriendsHelpPopComService.close();
          });
        } else if (data.data == 1) {
          HbFriendsHelpPopComService.create(1, this.url, (submit) => {
            HbFriendsHelpPopComService.close();
          });
        } else if (data.data == 2) {
          HbFriendsHelpPopComService.create(2, this.url, (submit) => {
            HbFriendsHelpPopComService.close();
          });
        } else if (data.data == 3) {
          this.$toast("助力次数已达上限！");
        }
      });
    },
    joinInThisActivity() {
      this.friendsHelpList.canAssist = 4;
    },
    openPacket() {
      if (this.isTemplate == true) {
        // this.$toast("活动预览，仅供查看");
        return;
      }
      let actStatus = sessionStorage.getItem("actStatus");

      if (actStatus == 2) {
        this.$toast("活动已结束");
        return;
      }
      if (actStatus == 1) {
        this.$toast("活动尚未开始");
        return;
      }

      // 判断活动内限制
      if (
        this.friendsHelpList.activityHbLimit == 1 &&
        this.friendsHelpList.hbActivityCount <= 0
      ) {
        this.$toast("当前活动您的拆红包次数用光了哦~");
        return;
      }

      // 判断每日内限制
      if (
        this.friendsHelpList.dayHbLimit == 1 &&
        this.friendsHelpList.hbDayCount <= 0
      ) {
        this.$toast("您今天的拆红包次数用光了哦~");
        this.isDisabled = false;
        return;
      }
      if (this.isDisabled) {
        // this.$toast("您今天的拆红包次数用光了哦1~");
        return;
      }
      this.isDisabled = true;
      // 抢红包
      fansDrawList(this.friendsHelpList.prizeInfoId, 1, (data) => {
        this.isDisabled = false;
        this.friendsHelpList.hbCount = this.friendsHelpList.hbCount - 1;
        this.friendsHelpList.hbDayCount = this.friendsHelpList.hbDayCount - 1;
        this.friendsHelpList.hbActivityCount =
        this.friendsHelpList.hbActivityCount - 1;

        if (data.resp_code == 60010) {
           this.isShowEveryOpenPacketMoney = false;
           HbPrizePopComService.create(
            data,
            this.friendsHelpList.shopId,
            (submit) => {
              HbPrizePopComService.close();
            }
          );
          return;
        }
        if (data.resp_code == 60002) {
           this.isShowEveryOpenPacketMoney = false;
          HbPrizePopComService.create(
            data,
            this.friendsHelpList.shopId,
            (submit) => {
              HbPrizePopComService.close();
            }
          );
          return;
        }
        if (data.data.result == true) {
          if (data.data.status == 1) {
            this.isShowEveryOpenPacketMoney = true;
            // 价格处理 -> 后台的单位是分,前台除以 100
            if (data.data.priceInfo) {
              data.data.priceInfo = data.data.priceInfo / 100;
            }
            this.curOpenPacketMoney = data.data.priceInfo;
            // this.friendsHelpList.hbCount = this.friendsHelpList.hbCount - 1;
            // this.friendsHelpList.hbDayCount = this.friendsHelpList.hbDayCount - 1;
            // this.friendsHelpList.hbActivityCount =
            //   this.friendsHelpList.hbActivityCount - 1;
            HbPrizePopComService.create(
              data,
              this.friendsHelpList.shopId,
              (submit) => {
                HbPrizePopComService.close();
              }
            );
          } else if (data.data.status == 0) {
            // this.isDisabled = false;
            this.isShowEveryOpenPacketMoney = false;
            HbPrizePopComService.create(
              data,
              this.friendsHelpList.shopId,
              (submit) => {
                HbPrizePopComService.close();
              }
            );
          }
        } else {
          this.isShowEveryOpenPacketMoney = false;
          HbPrizePopComService.create(
            data,
            this.friendsHelpList.shopId,
            (submit) => {
              HbPrizePopComService.close();
            }
          );
        }
      });
    },
    sendHb() {
      if (this.isTemplate == true) {
        // this.$toast("活动预览，仅供查看");
        return;
      }
      let actStatus = sessionStorage.getItem("actStatus");

      if (actStatus == 2) {
        this.$toast("活动已结束");
        return;
      }
      if (actStatus == 1) {
        this.$toast("活动尚未开始");
        return;
      }
      if (this.friendsHelpList.followStoreFlag == 1) {
        hbShareFollowList((data) => {
          this.friendsHelpList.isClick = true;
          this.publicData.$emit(
            "isSuccessSaveInviterRecord",
            this.friendsHelpList.isClick
          );
          setShare((data) => {});
        });
      } else {
        this.friendsHelpList.isClick = true;
        this.publicData.$emit(
          "isSuccessSaveInviterRecord",
          this.friendsHelpList.isClick
        );
        setShare((data) => {});
      }
    },
    invite() {
      if (this.isTemplate == true) {
        // this.$toast("活动预览，仅供查看");
        return;
      }
      let actStatus = sessionStorage.getItem("actStatus");

      if (actStatus == 2) {
        this.$toast("活动已结束");
        return;
      }
      if (actStatus == 1) {
        this.$toast("活动尚未开始");
        return;
      }

        console.log('this.friendsHelpList',this.friendsHelpList)
      //不限次数
      if (this.friendsHelpList.activityHbLimit == 0) {
        if (this.friendsHelpList.dayHbLimit == 0) {
          allShare(this.isTemplate, this, (callBackData) => {
            console.log(callBackData, "callBackData");
            // return;
          });
        }
        // return;
      }
      //限次数
      if (this.friendsHelpList.hbActivityCount > 0) {
        //活动内剩余的抽奖次数
        if (this.friendsHelpList.hbDayCount > 0) {
          //每天剩余抽奖次数
          allShare(this.isTemplate, this, (callBackData) => {
            console.log(callBackData, "callBackData");
            // return;
          });
        } else {
          this.$toast("您今天的拆红包次数用光了哦~");
          return;
        }
      } else {
        this.$toast("当前活动您的拆红包次数用光了哦~");
        return;
      }
    },
  },
  filters: {
    //时间转换
    dateTime(val) {
      if (val) {
        return dateUtil.formatTime(val, "yyyy-MM-dd hh:mm");
      }
      return val;
    },
    twostar: function (value) {
      if (!value) {
        return "";
      }
      return value.substr(0, 1) + "**" + value.substr(-1, 1);
    },
  },
};
</script>
<style lang="scss" scoped>
.activityImg {
  position: relative;
}
//  .notStart_btn{
//   background: url("https://img10.360buyimg.com/imgzone/jfs/t1/38417/8/1503/17025/5cbd71feE246473f7/9c1a52b7b709679d.png") no-repeat;
//   background-size: 100%;
//   width:2.32rem;
//   height:2.34rem;
//   margin:.8rem auto 0;
// }
/*动态class*/
.marginTop1 {
  margin-top: 0rem !important;
}
.marginTop2 {
  margin-top: 0.4rem !important;
}
</style>
