const app = getApp();
Page({
  data: {
    images: {
       logo:'/static_file/logo.png',
        background:'/static_file/background.png',
        card:'/static_file/card.png',
        tzd:'/static_file/backgrountzdd.png',
        phone:'/static_file/phone.png',
        yl:'/static_file/background.yl',
        jcsfcjtzd:'/static_file/jcsfcjtzd.png',
        kapiantubiao:'/static_file/kapiantubiao.png'
    },
    // 用户信息
    userInfo: {
      name: "",
      accountNumber: "",
      address: ""
    },

    // 账单信息
    billInfo: {
      totalAmount: 0,  // 总计应缴
      waterUsage: 0,      // 用水量
      balance: 0       // 账户余额
    },
    // 功能按钮配置
    functionList: [
      {
        icon: '/static_file/zdlb.png',
        text: '账单列表',
        url: '/pages/zhangdanlist/zhangdanlist'
      },
      {
        icon: '/static_file/jfjl.png',
        text: '缴费记录',
        // url: '/pages/feedback/feedback'
      },
      {
        icon: '/static_file/yhbx.png',
        text: '用户报修',
        url: '/pages/baoxiudj/baoxiudj'
      },
      {
        icon: '/static_file/tzgg.png',
        text: '通知公告',
        url: '/pages/tongzhiList/tongzhiList'
      },
      {
        icon: '/static_file/ckys.png',
        text: '户号绑定',
        url: '/pages/huhaoguanli/huhaoguanli'
      },
      {
        icon: '/static_file/yjfk.png',
        text: '投诉建议',
        url: '/pages/tousujianyi/tousujianyi'
      }
    ],
    currentIndex:0,
    noticeList:[],
    // 活动信息
    activity: {
      title: '净水器活动',
      subtitle: '预留入口',
      imageUrl: '/static_file/background.png'
    }
  },

  onLoad() {
    let refresh= app.globalData.refresh;
    if(refresh==1){
      wx.showLoading({
        title: '加载中...',
      });
      if(!app.globalData.userWxInfo.token){
        wx.hideLoading();
        return;
      }
      const _this = this;
      wx.request({
        url: app.globalData.interfaceUrls.homePageInfo+app.globalData.currentAccountInfo.usernumber,
        method: 'GET',
        header: {
          'content-type': 'application/json', // 默认值
          'token':app.globalData.userWxInfo.token,
          'source':"wc",
          '!SAAS_LOGIN_TOKEN_!':app.globalData.currentAccountInfo.dsKey
        },
        success (res) {
          wx.hideLoading();
          let apiReturnData=res.data;
          app.globalData.notices= apiReturnData.data.swNotificationAnnouncementDos;
          let data= apiReturnData.data.swNotificationAnnouncementDos.filter(data=>{
            return data.homepageslider=='1';
          });
      
          _this.setData({
            billInfo:{
              totalAmount:apiReturnData.data.duFees,  // 总计应缴
              waterUsage:apiReturnData.data.waterConsumption,      // 用水量
              balance: apiReturnData.data.accountbalance   // 账户余额
            },
            userInfo:{
              name: apiReturnData.data.username,
              accountNumber: apiReturnData.data.usernumber,
              address: apiReturnData.data.address
            },
            noticeList :data,
            isScrolling: true,
          })
        },
        fail(error) {
          wx.hideLoading()
          utils.simleInfo('登录失败，请稍后再试')
        }
      })
    }
  },
  onShow() {

  },

  // 切换水表
  handleSwitchMeter() {
    wx.navigateTo({
       url: '/pages/huhaoguanli/huhaoguanli'
    })
  },

  // 充值缴费
  handlePayment() {
    wx.navigateTo({
      // url: '/pages/payment/payment'
    })
  },

  // 功能按钮点击事件
  handleFunctionClick(e) {
    const index = e.currentTarget.dataset.index
    const item = this.data.functionList[index]

    if (item && item.url) {
      wx.navigateTo({
        url: item.url,
        fail: function(err) {
          console.error('页面跳转失败:', err)
          wx.showToast({
            title: '功能开发中',
            icon: 'none',
            duration: 2000
          })
        }
      })
    } else {
      wx.showToast({
        title: '功能开发中',
        icon: 'none',
        duration: 2000
      })
    }
  },

  // 查看账单列表
  handleViewBill() {
    wx.navigateTo({
      // url: '/pages/billList/billList'
    })
  },

  // 查看更多活动
  handleMoreActivity() {
    wx.navigateTo({
      // url: '/pages/activityList/activityList'
    })
  },

  // 跳转到立即缴费页面
  goToPayment: function() {
    wx.navigateTo({
      url: '/pages/lijijiaofei/lijijiaofei',
    })
  },

  // 跳转到通知公告列表页面
  goToNoticeList: function() {
    wx.navigateTo({
      url: '/pages/tongzhiList/tongzhiList'
    })
  },

  // 跳转到通知详情页面
  goToNoticeDetail: function() {
    const currentNotice = this.data.noticeList[this.data.currentIndex];
    // 将整个通知对象序列化并编码
    const noticeData = encodeURIComponent(JSON.stringify({
      id: currentNotice.id,
      title: currentNotice.title,
      content: currentNotice.content,
      date: currentNotice.date,
      type: currentNotice.type,
      source: currentNotice.source
    }));

    wx.navigateTo({
      url: `/pages/tzxq/tzxq?noticeData=${noticeData}`
    });
  },

  onReady: function() {
    this.startNoticeScroll();
  },

  startNoticeScroll: function() {
    if(this.data.noticeList.length==0){
      return;
    }
    let data= (this.data.noticeList||[]).filter(data=>{
       return data.homepageslider=='1';
     });
   
    const content = data[this.data.currentIndex].scrollingcontent;
    const title = data[this.data.currentIndex].noticetitle;
    const duration = (title.length + content.length) * 0.3;

    this.setData({
      scrollDuration: duration,
      isScrolling: true,
    });
  },

  onScrollComplete: function() {
    debugger;
    this.setData({
      isScrolling: false
    });

    // 等待1秒后切换到下一条
    setTimeout(() => {
      const nextIndex = (this.data.currentIndex + 1) % this.data.noticeList.length;
      this.setData({
        currentIndex: nextIndex
      }, () => {
        this.startNoticeScroll();
      });
    }, 500);
  }
})
