//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    waterInfo: {},
    onQuery: true,
    items: [{
        id: 'realtimewater',
        label: '用水量',
        icon: '/static/img/icon-water.png',
        url: '?period=7'
      }, {
        id: 'waterprice',
        label: '水价查询',
        icon: '/static/img/icon_water_price.png'
      }, {
        id: 'payment',
        label: '缴费',
        icon: '/static/img/icon-payment.png'
      }, {
        id: 'wateranalysis',
        label: '用水趋势',
        icon: '/static/img/icon_water_analysis.png'
      }, {
        id: 'waterabnormal',
        label: '异常预警',
        icon: '/static/img/icon_water_abnormal.png'
      }, {
        id: 'customermanage',
        label: '户号管理',
        icon: '/static/img/icon_customer_manage.png'
      }
    ]
  },
  getPhoneNumber(e) {
    if (e.detail.encryptedData) {
      const {
        iv,
        encryptedData
      } = e.detail;
      const {
        param,
        item = {}
      } = e.currentTarget.dataset;
      wx.login({
        success: res => {
          const {
            code
          } = res;
          app.$http.postForm('/user/bindingMobile', {
            code,
            encryptedData,
            iv
          }).then(({
            status,
            data = {}
          }) => {
            if (status === 0) {
              if (this.data.userInfo.deviceId) {
                wx.navigateTo({
                  url: `/pages/${item.id || param}/${item.id || param}${item.url ? item.url : ''}`
                })
              } else {
                wx.navigateTo({
                  url: "/pages/customermanage/customermanage"
                })
              }
            }
          })
        }
      })
    }
  },
  gotopage(e) {
    const {
      param,
      item = {}
    } = e.currentTarget.dataset;
    if (!this.data.userInfo.userNumber && param != 'customermanage' && item.id != 'customermanage') {
      wx.showToast({
        title: '请先绑定您的户号',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.navigateTo({
      url: `/pages/${item.id || param}/${item.id || param}${item.url ? item.url : ''}`
    })
  },
  getAccountInfo() {
    this.setData({
      onQuery: true
    })
    //app.showLoading()
    app.$util.selfUserInfo(app.globalData, true).then((data) => {
      if (data) {
        this.setData({
          userInfo: data,
          waterInfo: data.waterStages
        })
      }else {
        this.setData({
          userInfo: {},
          waterInfo: {}
        })
      }
      this.setData({
        onQuery: false
      })
      //app.hideLoading();
    }).catch(() => {
      //app.hideLoading();
      this.setData({
        onQuery: false
      })
    })
  },
  onShow() {
    this.getAccountInfo();
  },
  toLogin(msg) {
    this.popup && this.popup.setData({
      type: 'error',
      title: '提示',
      content: msg,
      showCancel: false
    });
    this.popup.show();
  },
  cancelEvent: function() {
    this.popup.close();
  },
  confirmEvent: function() {
    this.getAccountInfo(true);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    //获得popup组件
    this.popup = this.selectComponent("#popup");
  },
})