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

import {
  imgServerUrl
} from '../../../config/config.js'
let selected = ''
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,
    clockCount: 0,
    defaultDate: ''
  },
  toList() {
    wx.navigateTo({
      url: './list?date=' + this.data.defaultDate,
    })
  },
  getList(month_type) {
    monthTotalSettlement({ month_type }).then(data => {
      this.setData({
        wsPunchClockList: data.data.wsPunchClockList
      })
    })
  },
  getCurrent() {
    currentSettlement().then(data => {
      const date = new Date(data.data.currentSettlement.date.replace(/'-'/g, '/'))
      // const month = Number(data.data.currentSettlement.date.split('-')[1])
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      this.selectComponent('.calendar').init(date)
      this.setData({
        currentSettlement: data.data.currentSettlement,
        text: month,
        mouth: month,
        year,
        defaultDate: data.data.currentSettlement.date.replace(/'-'/g, '/')
      })

      const search_date = [year, month, day].map(val => {
        const number = val.toString()
        return number[1] ? number : '0' + number
      }).join('-')
      this.getDay(search_date)
    })
  },
  next() {
    if (this.data.text !== this.data.mouth) {
      this.selectComponent('.calendar').next()
      this.getList(1)
      this.setData({
        text: this.data.mouth,
        next: `${imgServerUrl}/images/clock/clock-arrow-righr-disabled.png`,
        last: `${imgServerUrl}/images/clock/clock-arrow-left.png`,
      })
    }
  },
  last() {
    if (this.data.text === this.data.mouth) {
      this.selectComponent('.calendar').last()
      this.getList(2)
      if (this.data.mouth - 1 <= 0) {
        this.setData({
          text: 12,
        })
      } else {
        this.setData({
          text: this.data.mouth - 1
        })
      }
      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,
        clockCount: data.data.clockCount,
        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 () {
    this.getList(1)
    this.getCurrent()
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})