<template>
  <div class="root" :style="{backgroundColor:comp.props.bgColor.val }">
    <!-- 背景图片 -->
    <img class="img-bg" :src="comp.props.bgImg.val" />

    <!-- 企业logo -->
    <img class="img-elogo" :src="logo" />

    <!-- 活动攻略 -->
    <img class="img-rule" :src="comp.props.ruleBtnImg.val" @click="onClickRule()" />

    <!-- 领取按钮 -->
    <img class="img-btn" :src="comp.props.lotteryBtnImg.val" @click="onClickLottery()" />
  </div>
</template>

<script>
import { Toast } from "mint-ui";
import apiHelper from "@/assets/js/apiHelper.js";
// import { userInfo } from 'os';
import { Indicator } from "mint-ui";
import eventBus from "@/assets/js/eventBus.js";
export default {
  name: "Comp",
  props: ["comp", "cfg", "selectedPage"],
  data() {
    return {
      bgStyle: {},
      env: "",
      weChatInfo: this.sup8.context.wxUser,
      isReg: false, //是否有登陆流程，是否需要注册
      logo: ""
    };
  },
  created: function() {
    var that = this;
    that.mergeCompProps(that.comp);

    switch (that.sup8.env) {
      case that.sup8._def.env.design:
        that.env = "设计器模式";
        break;
      case that.sup8._def.env.preview:
        that.env = "预览模式";
        break;
      case that.sup8._def.env.run:
        that.env = "运行模式";
        sessionStorage.removeItem("prizeInfo");
        // this.checkIsRegister();
        if (this.sup8.context.geo) {
          this.checkIsRegister();
        } else {
          eventBus.subscribe("gpsDone", data => {
            this.checkIsRegister();
          });
        }
        if (!that.sup8.enoInfo) {
          this.getEnterprise();
        } else {
          this.logo = that.sup8.enoInfo.logoUrl;
        }
        break;
      default:
        break;
    }

    this.bgStyle["background-color"] = this.comp.props.bgImg.val;
  },

  mounted: function() {},
  methods: {
    // 判断用户是否注册过
    checkIsRegister() {
      let that = this;
      let userInfo = {
        MainId: "",
        UserId: "",
        Location: "",
        Address: "",
        ActivityId: "",
        EnterpriseNo: "",
        Mobile: "",
        MsgCode: "",
        DigitCode: "",
        IsRegister: false,
        WeChat: {}
      };
      if (that.sup8.context.activityRegister == "3-2") {
        // 模板有注册页面
        userInfo.IsRegister = true;
      }
      const geo = !!this.sup8.context.geo
        ? this.sup8.context.geo.addressComponent
        : "";
      userInfo.Location = that.sup8.context.channel;
      userInfo.Address =
        geo != ""
          ? `${geo.country}${geo.province}${geo.city}${geo.district}${geo.street}${geo.street_number}`
          : "";
      userInfo.WeChat = that.weChatInfo;
      userInfo.DigitCode = that.sup8.codeInfo.Code;
      userInfo.ActivityId = !!that.sup8.h5Template
        ? that.sup8.h5Template.ActivityId
        : "";
      userInfo.EnterpriseNo = that.sup8.context.eno;
      Indicator.open();
      apiHelper.checkIsRegister(JSON.stringify(userInfo), function(res) {
        Indicator.close();
        if (res.flag) {
          // that.isReg = false;
          // that.sup8.context.userId = res.data;
          // IsNeedRegister：true：需要注册，false:不需要注册
          if (res.data.IsNeedRegister) {
            that.isReg = true;
          } else {
            that.isReg = false;
            that.sup8.context.userId = res.data.UserId;
          }
        } else {
          that.isReg = true;
          Toast("网络开小差了，请稍后再试");
          // if(that.sup8.context.activityRegister == "3-2"){
          //     // 模板有注册页面
          //     that.isReg = true;
          // }
        }
      });
    },
    // 获取用户logo信息
    getEnterprise() {
      let that = this;
      apiHelper.getEnterprise(that.sup8.context.eno, function(res) {
        if (res.flag) {
          that.logo = !!res.data.logoUrl ? res.data.logoUrl : "";
          that.sup8.enoInfo = res.data;
        }
      });
    },
    onClickRule() {
      let that = this;
      switch (that.sup8.env) {
        case that.sup8._def.env.design:
          break;
        case that.sup8._def.env.preview:
          this.$parent.changeSelectedPage("3-5");
          break;
        case that.sup8._def.env.run:
          this.$parent.changeSelectedPage("3-5");
          break;
        default:
          break;
      }
    },
    onClickLottery() {
      let that = this;
      switch (that.sup8.env) {
        case that.sup8._def.env.design:
          break;
        case that.sup8._def.env.preview:
          this.$parent.changeSelectedPage("3-3");
          break;
        case that.sup8._def.env.run:
          if (!this.sup8.context.wxUser) {
            Toast("参与活动需要微信授权，请重新扫码授权参与");
            return;
          }
          if (this.isReg) {
            this.$parent.changeSelectedPage("3-2");
          } else {
            this.participationActivity();
          }
          break;
        default:
          break;
      }
    },
    participationActivity() {
      let that = this;
      let userInfo = {
        MainId: "",
        UserId: "",
        Location: "",
        Address: "",
        ActivityId: "",
        EnterpriseNo: "",
        Mobile: "",
        MsgCode: "",
        DigitCode: "",
        IsRegister: false,
        WeChat: {},
        WebBrowser: navigator.userAgent,
		TargetPeople:this.sup8.context.targetpeople,
		ActivityType:this.sup8.context.businesstype
      };
      if (that.sup8.context.activityRegister == "3-2") {
        // 模板有注册页面
        userInfo.IsRegister = true;
      }
      const geo = !!this.sup8.context.geo
        ? this.sup8.context.geo.addressComponent
        : "";
      userInfo.Location = that.sup8.context.channel;
      userInfo.Address =
        geo != ""
          ? `${geo.country}${geo.province}${geo.city}${geo.district}${geo.street}${geo.street_number}`
          : "";
      userInfo.WeChat = that.weChatInfo;
      userInfo.DigitCode = that.sup8.codeInfo.Code;
      userInfo.ActivityId = that.sup8.context.activityid;
      userInfo.EnterpriseNo = that.sup8.context.eno;
      userInfo.UserId = that.sup8.context.userId;
      Indicator.open();
      // alert("开始调用");
      apiHelper.participationActivity(
        JSON.stringify(userInfo),
        function(res) {
          Indicator.close();
          // alert("调用结束");
          if (res.flag) {
            if (!!res.data.WiningRecords && res.data.WiningRecords.length > 0) {
              sessionStorage.setItem(
                "prizeInfo",
                JSON.stringify(res.data.WiningRecords)
              );
              that.$parent.changeSelectedPage("3-3");
            } else {
              sessionStorage.setItem("prizeInfo", JSON.stringify(res.msg));
              that.$parent.changeSelectedPage("3-4");
            }
          } else {
            // 0:正常，1：活动规则，2：奖项规则，9：系统错误
            if (res.errorCode == 1 || res.errorCode == 9) {
              Toast(decodeURIComponent(res.msg));
            } else {
              sessionStorage.setItem("prizeInfo", JSON.stringify(res.msg));
              that.$parent.changeSelectedPage("3-4");
            }
          }
        },
        function(res) {
          Indicator.close();
          Toast("网络开小差了，请稍后再试");
        }
      );
    }
  }
};
</script>

<style scoped>
.root {
  height: 100vh;
  overflow: hidden;
  background-color: #f9383a;
}
.img-bg {
  width: 100% !important;
  overflow: hidden;
  z-index: 1;
}

.img-elogo {
  position: absolute;
  width: 20%;
  top: 0;
  left: 6%;
}

.img-rule {
  position: absolute;
  width: 30%;
  top: 5%;
  right: 0%;
}

.img-btn {
  position: absolute;
  width: 50%;

  bottom: 8%;
  left: 25%;
}

/* 设计器 */
.env-design .root {
  height: 538px !important;
}
</style>
