// components/sg-date-picker/index.js
import http from '../../utils/http'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    date: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    multiArray: [],
    multiIndex: []
  },

  lifetimes: {
    attached: function() {
      var that = this
      http({
        url: 'data/getYearsAndMonths',
        data: {},
        success: function(res) {
          if(res.code == 0) {
            that.setData({
              multiArray: res.data.array,
              multiIndex: res.data.index
            })
            that.updateDate()
          }
        }
      })
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    updateDate: function() {
      var multiArray = this.data.multiArray
      var multiIndex = this.data.multiIndex
      if(multiArray[0][multiIndex[0]].id == '') {
        this.triggerEvent('update', {
          date: ''
        })
      } else {
        var date = multiArray[0][multiIndex[0]].id 
        var month = multiArray[1][multiIndex[1]].id
        month = month <= 9 ? '0' + month : month
        this.triggerEvent('update', {
          date: date + '-' + month + '-' + '01'
        })
      }
    },
    bindColumnChange: function(e) {
      var multiIndex = this.data.multiIndex
      multiIndex[e.detail.column] = e.detail.value
      this.setData({
        multiIndex: multiIndex
      })
      this.updateDate()
    }
  }
})
