// packageI/cps/cpsExtension.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: '',
    testcolor: '#ffb32f',
    active: 0,
    isFixed: false,
    child:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.tab) {
      this.setData({
        tab: options.tab,
      });
    }
    if (options.mid) {
      app._setMid(options.mid);
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      console.log(scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split('&');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          console.log('59', chil_arr);
          if (chil_arr[0] == 'mid') {
            app._setMid(chil_arr[1]);
          } else if (chil_arr[0] == 'tab') {
            this.setData({
              tab: chil_arr[1],
            });
          } else if (chil_arr[0] == 'child') {
            this.setData({
              child: true,
            });
          }
        }
      }
    }
    if(options.child == 'child'){
      console.log('111111111');
      // 子平台插件
      this.setData({
        child:true
      });
    }
    this.getData();
    let oris = wx.getSystemInfoSync().platform;
    let that = this;
    setTimeout(() => {
      let height = wx.getMenuButtonBoundingClientRect().height;
      console.log(wx.getMenuButtonBoundingClientRect(), );
      that.setData({
        viewHeight: oris == 'ios' ? height + 20 : height
      });
    }, 400);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    console.log(`/packageI/cps/cpsExtension?tab=${this.data.active}&mid=${mid}`);
    let url;
    if(this.data.child){
      url=`/packageI/cps/cpsExtension?tab=${this.data.info[this.data.active].activity_id}&mid=${mid}&child=child`;
    }else{
      url=`/packageI/cps/cpsExtension?tab=${this.data.info[this.data.active].activity_id}&mid=${mid}`;
    }
    return {
      path: url
    };
  },
  ascrollChange(e) {

  },
  toLink(e) {
    console.log(e);
    let id = e.currentTarget.dataset.web_url.mini_app_id;
    let paths = e.currentTarget.dataset.web_url.mini_path;
    wx.navigateToMiniProgram({
      appId: id,
      path: paths,
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    });
  },
  runGoPageYx(e) {
    let id = e.currentTarget.dataset.id;
    let info = this.data.info;
    for (let i = 0; i < info.length; i++) {
      if (info[i].another_id == id) {
        // this.$refs.tabsNav.scrollTo(i);
        this.setData({
          active: i
        });
      }
    }
  },
  goPageYx(ind) {
    let info = this.data.info;
    for (let i = 0; i < info.length; i++) {
      if (info[i].another_id == ind) {
        // this.$refs.tabsNav.scrollTo(i);
        this.setData({
          active: i
        });
      }
    }
  },
  actChange(e) {
    console.log(e);
    let key = e.detail.index;
    this.setData({
      active: key
    });
    console.log(this.data.info[key].activity_id);
    this.getPage(this.data.info[key].activity_id);
  },
  closePopup() {
    this.setData({
      show: false
    });
  },
  imgPreview(e) {
    console.log(e);
    let link = e.currentTarget.dataset.img;
    wx.previewImage({
      current: link,
      urls: [link]
    });
  },
  getData() {
    let that = this;
    let url = "";
    if(this.data.child){
      url = "plugin.cps-sub-platform.frontend.plat-activity.getPlatList";
    }else{
      url = "plugin.aggregation-cps.api.plat-activity.getPlatList";
    }
    let urlStr = app.getNetAddresss(url);
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            info: res.data
          });
          let ind = res.data[that.data.active].activity_id;
          if(that.data.tab == ind){
            console.log('这个tab是第一个');
            // 如果是第一个导航就直接请求
            this.getPage(ind);
          }else{
            // 不是第一个就循环找到第几个，然后在 导航事件 里面请求
            for (let i = 0; i < res.data.length; i++) {
              if (that.data.tab == res.data[i].activity_id) {
                that.goPageYx(res.data[i].another_id);
              }
            }
          }
          setTimeout(() => {
            // 获取元素高度
            let query = wx.createSelectorQuery();
            query.select('.navtab').boundingClientRect(rect => {
              console.log(rect);
              let clientHeight = rect.height;
              let clientWidth = rect.width;
              let ratio = 750 / clientWidth;
              let height = clientHeight * ratio;
              console.log(height);
              that.setData({
                paddingHeight:height
              });
            }).exec();
          }, 300);
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
    });
  },
  getPage(type) {
    wx.showLoading({
      title: '加载中',
    });
    let url = "";
    if(this.data.child){
      url = "plugin.cps-sub-platform.frontend.plat-activity.getPlatDetail";
    }else{
      url = "plugin.aggregation-cps.api.plat-activity.getPlatDetail";
    }
    let urlStr = app.getNetAddresss(url);
    app._postNetWork({
      url: urlStr,
      data: {
        activity_id: type
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            datas: res.data
          });
          let arr = [];
          let topimg;
          for (let i in this.data.datas.page_setting.tab_image) {
            if (i == res.data.another_id) {
              // banner图
              topimg = `https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/cps/${this.data.datas.page_setting.tab_image[i]}`;
            }
            arr.push(i);
          }
          this.setData({
            buttonArr: arr,
            topImg: topimg
          });
          console.log(this.data.datas);
          wx.hideLoading({
            success: (res) => {},
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
          if (res.data.bind_mobile) {
            this.setData({
              show1: true
            });
          }
          wx.hideLoading({
            success: (res) => {},
          });
        }
      },
    });
  },
  getShare() {
    wx.showLoading({
      title: '海报加载中',
    });
    let url = "";
    if(this.data.child){
      url = "plugin.cps-sub-platform.frontend.plat-activity.getPlatPoster";
    }else{
      url = "plugin.aggregation-cps.api.plat-activity.getPlatPoster";
    }
    let urlStr = app.getNetAddresss(url);
    app._postNetWork({
      url: urlStr,
      data: {
        activity_id: this.data.info[this.data.active].activity_id,
        poster_type: 2
      },
      success: (resdata) => {
        var res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          this.setData({
            show: true,
            shareImg: res.data.url
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
    });
  },
  tabrshowbtn(e) {
    let bol = e.detail;
    if (bol) {
      this.setData({
        kFootshow: false
      });
    }
  }
});