// pages/waterabnormal/waterabnormal.js
const app = getApp();
let moment = require('../../utils/moment.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    monthValue: moment().format('YYYY-MM'),
    monthTxt: moment().format('YYYY年MM月'),
    monthStart: '1990-01',
    monthEnd: moment().format('YYYY-MM'),
    weeks: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
    curDay: moment().format('YYYYMMDD'),
    selectedDay: moment().format('YYYYMMDD'),
    dateArr: [],
    abnormalList: [],
  },
  toSettings() {
    wx.navigateTo({
      url: '/pages/waterabnormal/settings/settings',
    })
  },
  dayTap(e) {
    const {
      dateStr
    } = e.currentTarget.dataset.info;
    if (dateStr) {
      this.setData({
        selectedDay: dateStr
      })
      this.getList();
    };
  },
  bindDateChange(e) {
    const {
      value
    } = e.detail;
    this.setData({
      monthTxt: moment(value).format('YYYY年MM月'),
    })
    const arr = value.split('-'),
      year = arr[0],
      month = arr[1];
    //获取本月 
    const endDate = moment(year, "YYYY").month(month - 1, 'MM').endOf('month').format("YYYYMMDD");
    const now = this.data.selectedDay.substr(6);
    const end = endDate.substr(6);
    if (now > end) {
      this.setData({
        selectedDay: endDate
      })
    } else {
      this.setData({
        selectedDay: `${year}${month}${now}`
      })
    }
    this.getDaysArray(year, month);
  },
  getDaysArray(year, month) {
    let dateArr = []; //需要遍历的日历数组数据
    let arrLen = 0; //dateArr的数组长度
    let startWeek = moment(`${year}/${month}/01`, 'YYYY/MM/DD').day();; //目标月1号对应的星期
    let dayNums = moment(`${year}/${month}`, 'YYYY/MM').daysInMonth(); //获取目标月有多少天
    let obj = {};
    arrLen = startWeek + dayNums;
    for (let i = 0; i < arrLen; i++) {
      if (i >= startWeek) {
        const dateNo = (i - startWeek + 1).toString().padStart(2, '0');
        const dateStr = `${year}${month}${dateNo}`;
        obj = {
          dateStr,
          dateNo
        }
      } else {
        obj = {};
      }
      dateArr[i] = obj;
    }
    this.setData({
      dateArr: dateArr
    })
    this.getDateArr();
  },
  getList() {
    app.$http.get('/warningMessage/getPage', {
      date: this.data.selectedDay
    }).then(({
      status,
      data = []
    }) => {
      if (status == 0) {
        data.forEach(v => {
          v.time = moment(v.dateCreate).format('HH:mm:ss')
        })
        this.setData({
          abnormalList: data
        })
      }
    })
  },
  getDateArr() {
    app.showLoading();
    app.$http.get('/warningMessage/getDateStatus', {
      date: moment(this.data.monthTxt, 'YYYY年MM月').format('YYYYMM')
    }).then(({
      status,
      data = []
    }) => {
      if (status == 0) {
        this.data.dateArr.forEach(v => {
          const info = data.find(d => d.days == v.dateStr);
          if (info) {
            v.isAbnormal = info.feedbackStatus == 0;
            v.isHandled = info.feedbackStatus != 0;
          }
        })
        this.setData({
          dateArr: this.data.dateArr
        })
        this.getList();
      }
      app.hideLoading();
    }).catch(() => {
      app.hideLoading();
    })
  },
  touchstart: function (e) {
    const {
      feedbackStatus
    } = e.currentTarget.dataset.item;
    if (feedbackStatus) return
    //开始触摸时 重置所有删除
    let data = app.$touch._touchstart(e, this.data.abnormalList) //将修改过的list setData
    this.setData({
      abnormalList: data
    })
  },

  //滑动事件处理
  touchmove: function (e) {
    const {
      feedbackStatus
    } = e.currentTarget.dataset.item;
    if (feedbackStatus) return
    let data = app.$touch._touchmove(e, this.data.abnormalList, 'id') //将修改过的list setData
    this.setData({
      abnormalList: data
    })
  },
  toFeedback(e) {
    const {id} = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/waterabnormal/feedback/feedback?id=${id}`,
    })
  },
  onShow() {
    const arr = this.data.monthValue.split('-');
    this.getDaysArray(arr[0], arr[1]);
  }
})