let util = require('../../../utils/util.js');
let dateTimePicker = require('../../../utils/dateTimePicker.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: util.imgurl,
    dateStr: '',
    money: '',
    remark: '',
    typeid: 0,
    typeonlyid: 0,
    typename: '',
    isfirst: true,
    startYear: 1900,
    endYear: 2999
  }, 
  getdata() {
    util.query('expend/wxapi/findbyid', {
      id: this.data.id
    }, function(res) {
      if (res.code === 10000) {
        that.setData({
          olddata: res.data,
          money: res.data.money,
          remark: res.data.remark,
          expendType: {
            id: res.data.expendType.id,
            onlyID: res.data.expendType.onlyID
          },
          typename: res.data.expendType.name,
          typeid: res.data.expendType.id,
          typeonlyid: res.data.expendType.onlyID,
          dateStr: res.data.dateStr
        })
        var date = res.data.dateStr.split(' ')[0].split('-')
        var time = res.data.dateStr.split(' ')[1].split(':')
        that.dateUpdate(0, parseInt(date[0]) - that.data.startYear)
        that.dateUpdate(1, parseInt(date[1]) - 1)
        that.dateUpdate(2, parseInt(date[2]) - 1)
        that.dateUpdate(3, parseInt(time[0]))
        that.dateUpdate(4, parseInt(time[1]))
        that.dateUpdate(5, parseInt(time[2]))
      } else {

      }
    })
  },
  save() {
    if (!this.data.money) {
      util.showToast('请输入金额')
      return
    }
    if (!this.data.typeid || this.data.typeid === 0) {
      util.showToast('请选择类型')
      return
    }
    var data = {

      dateStr: this.data.dateTimeArray[0][this.data.dateTime[0]] + '-' + this.data.dateTimeArray[1][this.data.dateTime[1]] + '-' + this.data.dateTimeArray[2][this.data.dateTime[2]] + ' ' + this.data.dateTimeArray[3][this.data.dateTime[3]] + ':' + this.data.dateTimeArray[4][this.data.dateTime[4]] + ':' + this.data.dateTimeArray[5][this.data.dateTime[5]],
      money: this.data.money,
      remark: this.data.remark,
      expendType: {
        id: this.data.typeid,
        onlyID: this.data.typeonlyid
      }
    }
    var url = 'expend/wxapi/create'
    if (this.data.id) {
      url = 'expend/wxapi/update'
      this.data.olddata.dateStr = this.data.dateTimeArray[0][this.data.dateTime[0]] + '-' + this.data.dateTimeArray[1][this.data.dateTime[1]] + '-' + this.data.dateTimeArray[2][this.data.dateTime[2]] + ' ' + this.data.dateTimeArray[3][this.data.dateTime[3]] + ':' + this.data.dateTimeArray[4][this.data.dateTime[4]] + ':' + this.data.dateTimeArray[5][this.data.dateTime[5]]
      this.data.olddata.money = this.data.money
      this.data.olddata.remark = this.data.remark
      this.data.olddata.expendType = {
        id: this.data.typeid,
        onlyID: this.data.typeonlyid
      }
      data = this.data.olddata
    }
    util.query(url, data, function(res) {
      if (res.code === 10000) {
        util.showToast('保存成功')
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      } else {
        util.showToast('保存失败')
      }
    }, 'POST')
  },
  setmoney(e) {
    this.setData({
      money: util.PointNum(e.detail.value)
    })
  },
  settype(e) {
    if (e.currentTarget.dataset.id != -1) {
      this.setData({
        typeid: e.currentTarget.dataset.id,
        typeonlyid: e.currentTarget.dataset.onlyid,
        typename: e.currentTarget.dataset.name
      })
    } else {
      wx.navigateTo({
        url: '/pages/stock/payrecordtype/payrecordtype',
      })
    }
  },
  setdate(e) {
    this.setData({
      dateStr: e.detail.value
    })
  },
  setremark(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  changeDateTime(e) {
    this.setData({
      dateTime: e.detail.value
    });
  },
  changeDateTimeColumn(e) {
    this.dateUpdate(e.detail.column, e.detail.value)
  },
  dateUpdate(column, value) {
    var arr = this.data.dateTime,
      dateArr = this.data.dateTimeArray;

    arr[column] = value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  del(e) {
    wx.showModal({
      title: '确认',
      content: '是否确定删除支出',
      success(res) {
        if (res.confirm) {

          util.query("expend/wxapi/delete", {
            id: parseInt(that.data.id)
          }, function(res) {
            if (res.code == 10000) {
              util.showToast('删除成功')
              setTimeout(function() {
                wx.navigateBack({
                  delta: 1
                })
              }, 2000)
            } else {
              util.showToast(res.message)
            }
          }, "POST")
        } else if (res.cancel) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    this.getTypes()
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
    });
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getdata()
    } else {

    }
    var ui = wx.getStorageSync('userinfo')
    if (ui) {
      this.setData({
        createUser: ui.wechatName
      })
    }







    // this.setData({
    //   dateStr: util.formatTime(new Date()).split(' ')[0]
    // })

  },
  getTypes() {
    util.query('expendtype/wxapi/list', {
      pageNo: 1,
      pageSize: 999
    }, function(res) {
      if (res.code === 10000) {
        var types = res.data
        types.push({
          id: -1,
          name: '设置',
          icon: ''
        })
        var tt = []
        for (var i = 0; i < Math.ceil(types.length / 4); i++) {
          tt[i] = []
          for (var j = i * 4; j < (1 + i) * 4; j++) {
            if (types[j]) {
              tt[i].push(types[j])
            } else {
              tt[i].push({
                no: 1
              })
            }
          }

        }
        that.setData({
          types: tt
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!this.data.isfirst) {
      that.getTypes()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      isfirst: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})