import {
  getPayroll,
  myPayRollList
} from '../../../../services/index.js'
var app = getApp()

Page({
  data: {
    realName: '',
    idNum: '',
    targetTime: '2019-01',
    detailFlag: 1,
    multiArray: [],
    data: '',
    show: true,
    list: [],
  },
  onLoad: function () {
    wx.getStorage({
      key: 'hpMember',
      success: (res) => {
        this.setData({
          realName: res.data.realName,
          idNum: res.data.idCardNumber,
        })
      },
    })

    // console.log(options)
    this.fetchData()
  },
  todetail(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: './detail?id=' + id,
    })
  },

  // 页面数据加载
  fetchData() {
    // var curDate = new Date();
    // this.setData({
    //   realName: this.options.realName,
    //   idNum: this.options.idNum,
    // })
    myPayRollList({
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
    }).then(data => {
      if (data.errcode === 0) {
        this.setData({
          list: data.data.hpPayrollList
        })
      }
    })
  },

  // 工资条查询
  toGetPayroll() {
    this.setData({
      detailFlag: 2,
      show: false,
    })
    // var targetTime = this.data.multiArray[0] + '-' + formatNumber(this.data.multiArray[1]);
    let paramsObj = {
      month: this.data.targetTime,
      user_id: app.globalData.userId,
      user_token: app.globalData.userToken,
      member_id: app.globalData.memberId
    }
    Object.assign(paramsObj);
    getPayroll(paramsObj).then(data => {
      if (data.data.hpPayroll != null) {
        var payDetailArr = [];
        data.data.hpPayroll.payrollDetail = JSON.parse(data.data.hpPayroll.payrollDetail);
        for (var p in data.data.hpPayroll.payrollDetail) {
          payDetailArr.push({
            'key': data.data.hpPayroll.payrollDetail[p].info0,
            'value': data.data.hpPayroll.payrollDetail[p].info1
          })
        }
        this.setData({
          payDetailArr: payDetailArr,
          data: data.data.hpPayroll,
          detailFlag: 3
        })
      } else {
        return;
      }
    })
  },
  // 时间选择器值变化
  bindMultiPickerChange(e) {
    var targetDate = new Date(e.detail.value + '-01 00:00:00');
    this.setData({
      multiArray: [targetDate.getFullYear(), targetDate.getMonth() + 1],
      targetTime: e.detail.value
    })
    this.toGetPayroll()
  },
  onPullDownRefresh: function () {
    this.setData({
      list: []
    }, _ => {
      this.fetchData()
    })
  },
})