<template>
  <div class="notStarted">
    <div class="title">{{info.title}}</div>
    <p class="tit">直播倒计时</p>
    <div class="time">
      <timeDown v-if="startTime" :startTime="startTime"></timeDown>
    </div>
    <div class="remindBtn" @click="isLogin">开播提醒</div>
  </div>
</template>

<script type="text/ecmascript-6">
import live from "@/api/live";
import timeDown from "@/components/livedModel/timeDown";
export default {
  props: ["info"],
  name: "",
  data() {
    return {
      liveId: 0,
      startTime: null
    };
  },
  components: {
    timeDown
  },
  computed: {},
  created() {},
  onLoad(options) {
    
    if(options.params){
      let params = JSON.parse(options.params);
      this.liveId = params.liveId;  
    }else if(options.scene){
      var scene = decodeURIComponent(options.scene); //参数二维码传递过来的参数
      console.log(scene)
      let sceneInfo = scene.split(',')
      console.log('sceneInfo',sceneInfo)
      this.liveId = sceneInfo[0].split('=')[1]
    }
    this.startTime = this.info.startTime;
    console.log(this.info)
  },
  mounted() {},
  methods: {
    // 开播提醒
    remind() {

      let _this = this;

      wx.getSetting({
        withSubscriptions: true,
        success (res) {
          console.log(res.subscriptionsSetting,'subscriptionsSetting')

          if(!res.subscriptionsSetting.mainSwitch){

            wx.showModal({
                content: '检测到您没打开订阅消息的权限,是否去设置打开?',
                confirmText : '确认',
                confirmColor : '#07c160',
                success : (res2) => {
                    if (res2.confirm) {
                        wx.openSetting({
                          withSubscriptions : true,
                          success (res1) {
                            console.log(res1.authSetting,'authSetting')
                          }
                        })
                    }
                }
            })

            
          }else{
            try {
              const subscribeMessageObj = _this.$store.state.subscribeMessageObj;
              if (
                subscribeMessageObj &&
                Object.keys(subscribeMessageObj).length > 0
              ) {
                // TODO 开播订阅
                const tmplIds = [];
                for (var key in subscribeMessageObj) {
                  // 改成开播key
                  if (key == "start_live") {
                    tmplIds.push(subscribeMessageObj[key]);
                  }
                }
                if (tmplIds.length > 0) {
                  wx.requestSubscribeMessage({
                    tmplIds: tmplIds,
                    success:(res)=> {
                      console.log(res,res[tmplIds[0]],'---------------------58')
                      if(res[tmplIds[0]]=="accept"){
                        wx.showToast({
                          title: "您已成功订阅~",
                          icon: "success"
                        });
                        let query = {
                          liveBroadcastId: Number(_this.liveId),
                          openId: wx.getStorageSync("openid"),
                          subMessageTemplateId:tmplIds[0],
                        };
                        live.addLiveNoticeByCustomer(query).then(res => {});
                      }
                      console.log("message success response: ", res);
                    },
                    fail(res) {
                      console.log("message fail response: ", res);

                      wx.showToast({
                        title: '订阅失败,是否授权或者网络错误',
                        icon: "none"
                      });
                    }
                  });
                }
              }else{
                wx.showToast({
                  title: '订阅失败,是否授权或者网络错误',
                  icon: "none"
                });
                console.log('开播失败')
              }
            } catch (err) {
              wx.showToast({
                title: '订阅失败,是否授权或者网络错误',
                icon: "none"
              });
              console.error("subscribeMessage-err", err);
            }
          }
        }
      })
      
      
    },
    // 是否登录
    isLogin() {
      let _this = this;
      wx.getStorage({
        key: "sessionid",
        success(res) {
          _this.remind();
        },
        fail(res) {
          console.log("未登录------150");
          // 跳转登录页
          let backUrl = "/lived/main";
          let query = {
            liveId: _this.liveId,
            fromType: "mini"
          };
          let url = `/pages/login/main?back=${backUrl}&params=${JSON.stringify(
            query
          )}`;
          wx.navigateTo({
            url: url
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.notStarted {
  position: absolute;
  width: 70vw;
  top: 35%;
  left: 15vw;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 2;
  .title{
    font-size: 18px;
    font-weight: bold;
    color: #fff;
    margin-bottom: 20px;
  }
  .tit {
    font-size: 16px;
    color: #fff;
  }
  .time {
    margin-top: 16px;
  }
  .remindBtn {
    margin-top: 16px;
    height: 20px;
    line-height: 20px;
    text-align: center;
    border-radius: 6px;
    color: #fff;
    font-size: 18px;
    padding: 14px 40px;
    background: linear-gradient(to right, #ff877d, #fb566d);
  }
}
</style>