// pages/clock/clock-tongji/index.js
import {
  monthTotalSettlement,
  currentSettlement,
  daySettlement
} from '../../../services/index.js'

import {
  imgServerUrl
} from '../../../config/config.js'
Page({

  /**
    * 页面的初始数据
    */
  data: {
    wsPunchClockList: [],
    currentSettlement: {},
    imgServerUrl,
    next: `${imgServerUrl}/images/clock/clock-arrow-righr-disabled.png`,
    last: `${imgServerUrl}/images/clock/clock-arrow-left.png`,
    text: '',
    currentDuration: 0,
    punckClockList: [],
    nextMoney: 0
  },
  toList() {
    wx.navigateTo({
      url: './list',
    })
  },
  getList(month_type) {
    monthTotalSettlement({ month_type }).then(data => {
      this.setData({
        wsPunchClockList: data.data.wsPunchClockList
      })
    })
  },
  getCurrent() {
    currentSettlement().then(data => {
      this.setData({
        currentSettlement: data.data.currentSettlement
      })
    })
  },
  next() {
    if (this.data.text < this.data.mouth) {
      this.selectComponent('.calendar').next()
      this.getList(1)
      this.setData({
        text: new Date().getMonth() + 1,
        next: `${imgServerUrl}/images/clock/clock-arrow-righr-disabled.png`,
        last: `${imgServerUrl}/images/clock/clock-arrow-left.png`,
      }, _ => {
        let { year, text } = this.data
        const search_date = [year, text, 1].map(val => {
          const number = val.toString()
          return number[1] ? number : '0' + number
        }).join('-')
        this.getDay(search_date)
      })
    }
  },
  last() {
    if (this.data.text === this.data.mouth) {
      this.selectComponent('.calendar').last()
      this.getList(2)
      if (new Date().getMonth() - 1 < 0) {
        this.setData({
          text: 12
        }, _ => {
          let { year, month } = this.data
          const search_date = [year, month, 1].map(val => {
            const number = val.toString()
            return number[1] ? number : '0' + number
          }).join('-')
          this.getDay(search_date)

        })
      } else {
        this.setData({
          text: new Date().getMonth()
        }, _ => {
          let { year, text } = this.data
          const search_date = [year, text, 1].map(val => {
            const number = val.toString()
            return number[1] ? number : '0' + number
          }).join('-')
          this.getDay(search_date)
        })
      }
      this.setData({
        next: `${imgServerUrl}/images/clock/clock-arrow-righr.png`,
        last: `${imgServerUrl}/images/clock/clock-arrow-left-disabled.png`,
      })
    }
  },
  getDay(search_date) {
    daySettlement({ search_date }).then(data => {
      this.setData({
        punckClockList: data.data.punckClockList,
        nextMoney: data.data.nextMoney,
        currentDuration: data.data.currentDuration,
      })
    })
  },
  day(e) {
    let { year, month, date } = e.detail
    const search_date = [year, month, date].map(val => {
      const number = val.toString()
      return number[1] ? number : '0' + number
    }).join('-')
    this.getDay(search_date)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const date = new Date()
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    const search_date = [year, month, day].map(val => {
      const number = val.toString()
      return number[1] ? number : '0' + number
    }).join('-')
    this.getList(1)
    this.getCurrent()
    this.getDay(search_date)
    this.setData({
      text: month,
      mouth: month,
      year
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  }
})