const util = require('../../utils/util.js');
const app = getApp();
Component({
  // properties: {
  //   isShow: {
  //     type: Boolean,
  //     observer: function (newVal, oldVal) {
  //       this.setData({ isShow: newVal });
  //     }
  //   }
  // },
  data:{
    imgUrl:util.config.imgUrl,
    tipImg:'',
    popupData:{},
    popType:1 //1--安全弹窗 2--分享家停用弹窗
  },
  ready(){
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.src = 'http://video.bbztx.com/video/122d3cd2-18994ee8e9b-0000-0000-000-1e90f.mp3';
  },
  lifetimes:{
    moved(){
      this.innerAudioContext.stop();
    }
  },
  detached() {
    this.innerAudioContext.stop();
  },
  methods: {
    tipsClose(e){
      let data = wx.getStorageSync("safeTips") || {},value='';
      if (e&&e.currentTarget&&e.currentTarget.dataset.value) {
        value=e.currentTarget.dataset.value
      }else{
        value='';
      }
      if (value=='popup') {
        util.ajax({
          func:'v2/sharer/confirm/tips',
        }),function (res) {
          if (res.code == 0) {
            console.log(res.data)
          } else {
            console.log(res.data)
          }
        }
      }
      data.show = false;
      this.setData({ isShow : false });
      wx.setStorageSync("safeTips", data);
    },
    audioPlay() {
      this.innerAudioContext.stop();
      this.innerAudioContext.play();
      this.tipsClose();
    },
    // 点击图片跳转页面（记录点击次数）
    jumpPage(){
      let data = this.data.popupData,url = '';
      let obj ={
        cid : data.id,
        source : 'applet',
      };
      util.ajax({
        func:'v2/index/advert/popup/click',
        data:obj,
        method:'post',
        load:false
      }),function (res) {
        if (res.code == 0) {
          console.log(res.data)
        } else {
          console.log(res.data)
        }
      }
      //类目类型 0-单个活动 1-活动集 2-精彩回顾 3-活动体系 4-活动日历 5-活动视频 6-炫酷装备 7-单个链接
    switch (data.categoryType){
      case 0:
        url = '/pages/product/activity/index?aid='+data.aid;
      break;
      case 1:
        url = '/pages/public/swiper/list?id=' + data.id + '&title=' + data.title;
      break;
      case 2:
        url = '/pages/public/wonderful/index';
      break;
      case 3:
        url = '/pages/product/activity/system';
      break;
      case 4:
        url = '/pages/public/calendar/calendar';
      break;
      case 5:
        url = '/pages/public/video/video';
      break;
      case 6:
        url = '/pages/product/goods/index';
      break;
      case 7:
        url = '/pages/public/swiper/web?url=' + encodeURIComponent(data.url) + '&title=' + encodeURIComponent(data.title)
      break;
      case 8:
        url = '/pages/turntable/index?id='+data.wheelId
      break;
      case 9:
        url = '/pages/account/growthCourse/index'
      break;
    }
    this.setData({ isShow : false });
    wx.navigateTo({
      url: url
    });
    },
    // 安全弹窗数据
    getSafeTips(popupData){
      let that=this;
      util.getUserInfo().then(res=>{
        if (res.roleType=='sharer'&& res.roleTypeDisable==1 && res.sharerInfo.showTips==1) {
          that.setData({popType:2,isShow:true})
        }else{
          that.setData({popType:1})
          that.safePopup(popupData)
        }
      },(error)=>{
        if (!error) {
          that.setData({popType:1})
          that.safePopup(popupData)
        }
      })
    },
    // 非分享家停用文案内容
    safePopup(popupData){
      if (popupData.data) {
        let TipData = wx.getStorageSync("safeTips");
        if (TipData && ((popupData.data && popupData.data.id != TipData.modalId) || TipData.oldHours)) {
          wx.removeStorageSync('safeTips')
        }
        let data = popupData.data;
        let newDate = util.getNowFormatDate('yyyy-MM-dd'), safeTipData = wx.getStorageSync("safeTips") || {},newTime = new Date().getTime();
        this.setData({ popupData:data});
        if (popupData.showPopup && data.area.indexOf(popupData.entryPage) != -1) {
          if (util.isObjEmpty(safeTipData) || (util.datedifference(newDate, safeTipData.oldDate) > 0 && safeTipData.todayNum <= data.showCountDay)) {
            // this.globalData.openTipsNum = 0;
            safeTipData.oldDate = newDate;
            safeTipData.num = (safeTipData.num ? safeTipData.num : 0) + 1;
            safeTipData.todayNum = 1;
            safeTipData.modalId = data.id;
            safeTipData.oldTime = newTime;
            safeTipData.show = true;
            this.setData({ isShow : safeTipData.show });
          } else if(Math.floor((newTime - safeTipData.oldTime)/1000) >data.interval && safeTipData.todayNum < data.showCountDay && safeTipData.num < data.showCountBrowse){
            // this.globalData.openTipsNum = 0;
            safeTipData.oldDate = newDate;
            safeTipData.num = (safeTipData.num ? safeTipData.num : 0) + 1;
            safeTipData.todayNum = safeTipData.todayNum + 1;
            safeTipData.oldTime = newTime;
            safeTipData.show = true;
            this.setData({ isShow : safeTipData.show });
          }else {
            safeTipData.show = false;
            this.setData({ isShow : safeTipData.show });
          }
        }
        wx.setStorageSync("safeTips", safeTipData);
      } else {
        if (wx.getStorageSync("safeTips").modalId) {
          wx.removeStorageSync('safeTips')
        }
        this.defaultTips();
      }
    },
   defaultTips(){
      let newDate = util.getNowFormatDate('yyyy-MM-dd'), data = wx.getStorageSync("safeTips") || {},newHours = new Date().getHours();
      if (util.isObjEmpty(data) || (util.datedifference(newDate, data.oldDate) > 0 && data.num < 9999)) {
        // this.globalData.openTipsNum = 0;
        data.oldDate = newDate;
        data.num = (data.num ? data.num : 0) + 1;
        data.todayNum = 1;
        data.oldHours = newHours;
        data.show = true;
        this.setData({
          isShow:true
        })
      } else if((newHours - data.oldHours) >2 && data.todayNum < 2 && data.num < 9999){
        // this.globalData.openTipsNum = 0;
        data.oldDate = newDate;
        data.num = (data.num ? data.num : 0) + 1;
        data.todayNum = 2;
        data.oldHours = newHours;
        data.show = true;
        this.setData({
          isShow:true
        })
      }else{
        data.show = false;
        this.setData({
          isShow:false
        })
      }
        
      wx.setStorageSync("safeTips", data);
    },
  }
});