const app = getApp();
Page({
  data: {
    images: {
      background: '/static_file/background.png',
      water: '/static_file/water.png'
    },
    userInfo: {
      name: app.globalData.currentAccountInfo.username,
      id:  app.globalData.currentAccountInfo.usernumber,
      address: app.globalData.currentAccountInfo.address
    },
    showKeyboard: false,
    selectedAmount: null,
    customAmount: '',
    inputFocus: false,
    address: '',
    billList: [],
    // 是否可以开票
    isInvoic:false,
  },

  onLoad: function(options) {
    // 确保图片资源正确加载
    this.setData({
      images: {
        background: '/static_file/background.png',
        water: '/static_file/water.png'
      },
      isInvoic: app.globalData.waterCompanys.filter(data=>data.id==app.globalData.currentAccountInfo.dsKey)[0].swCompanyInfo.supportonlineinvoice=="1"
    });
    // 获取账单列表
    this.getBillList();
  },

  getBillList:function(){
    const _this = this;
    wx.showLoading({
      title: '获取中...',
      mask: true,
    });
      // debugger;
      wx.request({
        url: app.globalData.interfaceUrls.billList,
        method: 'POST',
        data: {
          accountNum: app.globalData.currentAccountInfo.usernumber,
          year: new Date().getFullYear()
        },
        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;
          // debugger;
          _this.setData({
            billList:apiReturnData.data,
          })
        },
        fail(error) {
          wx.hideLoading()
          utils.simleInfo('登录失败，请稍后再试')
        }
      });
  },
  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  },

  // 点击开发票按钮
  openInvoice: function(e) {
    const index = e.currentTarget.dataset.index;
    const bill = this.data.billList[index];

    wx.showToast({
      title: '开发票功能开发中',
      icon: 'none'
    });
  },

  // 切换到首页
  goToHome: function() {
    wx.switchTab({
      url: '/pages/homepage/homepage',
    });
  },

  // 切换到我的页面
  goToMine: function() {
    wx.switchTab({
      url: '/pages/mine/mine',
    });
  },

  navigateToHome() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },

  navigateToMine() {
    wx.switchTab({
      url: '/pages/mine/mine'
    })
  },

  // 跳转到账单详情页面
  goToBillDetail: function(e) {
    const bill = e.currentTarget.dataset.bill;
    wx.navigateTo({
      url: '/pages/zhangdanxiangqing/zdxiangqing?billInfo=' + JSON.stringify(bill)
    });
  },

  // 获取发票
  getInvoice: function(e) {
    wx.showToast({
      title: '暂不支持!',
      icon: 'none'
    });
    return;
    // 阻止事件冒泡，避免触发goToBillDetail
    e.stopPropagation();
    const bill = e.currentTarget.dataset.bill;
    wx.navigateTo({
      url: '/pages/invoice/invoice?billInfo=' + JSON.stringify(bill)
    });
  }
})
