import common from '../../utils/util';
import main from '../../utils/main';

const app = getApp();

Page({
  data: {
    scrollTop: 0,
    showReturnBtn: false, // 控制返回顶部按钮的显示/隐藏
  },
  onLoad: function (options) {
    let that = this;
    //debugger;
    let arr=[];
    for(let i=0;i<24;i++){
      let id=i.toString();
      if (i<10)
        id="0"+id;
      let obj={};
      obj.ID=id;
      
      arr.push(obj);
    }
   
    that.setData({
      Containnerheight: main.getWindowHeight(),
      List:arr,
    });
    main.checkGenerating();
    if (options.ID){
      setTimeout(function(){
        that.handleImageTap({currentTarget:{dataset:{id:options.ID}}});
      },1000);
    }
  },
  // 处理图片点击事件，实现锚点功能
  handleImageTap: function(e) {
    const id = e.currentTarget.dataset.id;
    let targetId="",offsetY=0;
    switch(id) {
      case "02":
        targetId = "img07";
        offsetY=40;
        break;
      case "03":
        targetId = "img09";
        offsetY=210;
        break;
      case "04":
        targetId = "img12";
        offsetY=50;
        break;
      case "05":
        targetId = "img17";
        offsetY=220;
        break;
      case "06":
        targetId = "img21";
        offsetY=-80;
        break;
      default:
        targetId = "";
        offsetY=0;
        break;
    }
    
    // 使用微信小程序的API滚动到目标元素
    wx.createSelectorQuery()
      .select('#' + targetId)
      .boundingClientRect(function(rect){
        if(rect){
          wx.pageScrollTo({
            scrollTop: rect.top+offsetY,
            duration: 300
          });
        }
      })
      .exec();
  },
  returnTop:function(){
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    });
  },
  // 监听页面滚动
  onPageScroll: function(e) {
    // 获取当前的scrollTop
    const scrollTop = e.scrollTop;
    
    // 更新scrollTop数据
    this.setData({
      scrollTop: scrollTop
    });
    
    // 当滚动距离超过一屏（这里以屏幕高度的一半为例）时显示返回顶部按钮
    // 获取系统信息来确定屏幕高度
    const screenHeight = wx.getSystemInfoSync().windowHeight;
    const threshold = screenHeight / 2; // 设置阈值为屏幕高度的一半
    
    // 根据scrollTop决定是否显示返回按钮
    if (scrollTop > threshold && !this.data.showReturnBtn) {
      this.setData({
        showReturnBtn: true
      });
    } else if (scrollTop <= threshold && this.data.showReturnBtn) {
      this.setData({
        showReturnBtn: false
      });
    }
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})