// pages/past_record/past_record.js
import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime, formateNumber, formatDate,throttle } from '../../utils/util.js'
import { HomeworkList, FindOwnServiceList, FindSaleServiceList, CalculateValidInterval, FindConflictServiceList, AddCorrectService, DelLibChild } from '../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData } = getApp()
const { baseImgUrl, thumbnail } = globalData
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl,
    thumbnail,
    pageNo: 1,
    pageSize: 5,
    startTime: formatDate(new Date()),
    isAll: false,
    isLoaded:false,
    hiddenModel:false,
    dateIndex:0,
    number:1,
    conflictModel:false,
    childInfo: {
      // "childName": "zhsfsf",
      // "imgUrl": "",
      // "school": "华景小学",
      // "grade": "三年级",
      // "serviceBeginTime": "2019-09-03",
      // "serviceEndTime": "2019-09=12",
      // derver:'5421562',
      // derverType:0
    },
    list: [
      //   {
      //   "date": "2019-09-03",
      //   "suggestion": "sfsfsfsf",
      //   "homeworkpictures": [
      //     { "fileKey": "/2019/09/03/1.png" },
      //   ]
      // },
      // {
      //   "date": "2019-09-03",
      //   "suggestion": "sfsfsfsf",
      //   "homeworkpictures": [
      //     { "fileKey": "/2019/09/03/1.png" },
      //   ]
      // },
      // {
      //   "date": "2019-09-03",
      //   "suggestion": "sfsfsfsf",
      //   "homeworkpictures": [
      //     { "fileKey": "/2019/09/03/1.png" },
      //   ]
      // },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.libId = wx.getStorageSync('libId')||options.libId;
    this.setData(options)
    console.log(options)
    this.getData()
    this.sureSaleServiceList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const obj = { navigateTo, redirectTo, navigateBack, viewImage }
    for (const i in obj) {
      this[i] = obj[i]
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ pageNo: 1 })
    this.getData(res => {
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { isAll } = this.data
    if (isAll) {
      return
    }
    this.getData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (app.sharePageDefaultCtl) {
      return app.sharePageDefaultCtl()
    }
  },
  /**
   * 底部导航跳转
   */
  redirectCtl: app.redirectCtl,

  /**
   * 路由跳转
   */
  navigateCtl: app.navigateCtl,
  /**
   * 提示框隐藏
   */
  allShowModel: function () {
    this.setData({ hiddenModel: false, conflictModel:false})
  },
  /**
   * 提示框弹出
   */
  childShowModel:throttle( function () {
    this.setData({ hiddenModel: true })
  },1000), 
 
  /**
 * 选择时间
 */
  bindDateChange: function (e) {
    console.log(e)
    const { value } = e.detail
    const beginDate = value.split("-").join(".")
    this.setData({ beginDate })
    this.sureValidInterval()
  },
  /**
  * 查询已拥有服务
  */
  sureOwnServiceList: function (cb) {
    const continuousFn = { fn: this.sureOwnServiceList, param: { ...arguments } }
    const { vipId } = this.data;
    FindOwnServiceList({ data: { vipId }, continuousFn }).then(res => {
      const { list}=res.data
      this.setData({ serveList: list})
      if (isFn(cb)) cb()
    }).catch(res => {
      if (isFn(cb)) cb()
    })
  },
  /**
  * 查询可购买服务信息
  */
  sureSaleServiceList: function (cb) {
    const continuousFn = { fn: this.sureSaleServiceList, param: { ...arguments } }
    const { libId, dateIndex, vipId } = this.data;
    FindSaleServiceList({ data: { libId, vipId}, continuousFn }).then(res => {
      const { list } = res.data
      const tempList = (res => {
        for (let i in res) {
          res[i]['number'] = 1
        }
        return res
      })(list || [])
      var temp = {};
      temp[`priceType`] = list[dateIndex].priceType;
      temp[`beginDate`] = list[dateIndex].beginDate;
      temp[`endDate`] = list[dateIndex].endDate;
      this.setData(temp);
      this.setData({ payList:list })
      if (isFn(cb)) cb()
    }).catch(res => {
      if (isFn(cb)) cb()
    })
  },
  // 选择卡类型
  selectCardType: function (e) {
    const { payList, priceType } = this.data;
    const { index, type, price } = e.currentTarget.dataset;
    this.setData({ priceType: type, dateIndex: index })
    var temp = {};
    for (var i in payList) {
      temp[`payList[${i}].status`] = ''
    }
    temp[`number`] = payList[index].number;
    temp[`beginDate`] = payList[index].beginDate;
    temp[`endDate`] = payList[index].endDate;
    temp[`payList[${index}].status`] = 'active';
    this.setData(temp);
  },
  // 加月卡数量
  plusNum:throttle( function () {
    const { payList, number, dateIndex } = this.data;
    var temp = {}
    let numbers = payList[dateIndex].number;
    if (numbers < 12) {
      numbers = numbers + 1;
    } else {
      numbers = numbers;
    }
    this.setData({ number: numbers })
    temp[`payList[${dateIndex}].number`] = numbers;
    this.setData(temp)
    
    this.sureValidInterval()
  },500),
  // 减月卡数量
  arrowNum:throttle( function () {
    const { payList, number, dateIndex } = this.data;
    var temp = {}
    let numbers = payList[dateIndex].number;
    if (numbers > 1) {
      numbers = numbers - 1;
    } else {
      numbers = 1
    }
    this.setData({ number: numbers })
    temp[`payList[${dateIndex}].number`] = numbers;
    this.setData(temp)
    this.sureValidInterval()
  },500),
  /**
  * 计算服务有效时间
  */
  sureValidInterval:throttle( function (cb) {
    const continuousFn = { fn: this.sureValidInterval, param: { ...arguments } }
    const { number, priceType, beginDate, payList, dateIndex, vipId } = this.data;
    CalculateValidInterval({ data: { num: number, priceType, beginDate, vipId }, continuousFn }).then(res => {
      const { beginDate, endDate } = res.data
      console.log(beginDate, endDate)
      var temp = {}
      temp[`payList[${dateIndex}].beginDate`] = beginDate;
      temp[`payList[${dateIndex}].endDate`] = endDate;
      this.setData(temp)
      this.setData({ beginDate, endDate })
      if (isFn(cb)) cb()
    }).catch(res => {
      if (isFn(cb)) cb()
    })
  },1000),
  /**
 * 查询有冲突的服务
 */
  sureServiceList: function (cb) {
    const continuousFn = { fn: this.sureServiceList, param: { ...arguments } }
    const { vipId, beginDate, endDate } = this.data;
    FindConflictServiceList({ data: { vipId, beginDate, endDate }, continuousFn }).then(res => {
      const { list } = res.data
      if (list.length > 0) {
        this.setData({ conflictModel: true, conflictList: list })
      } else {
        this.setData({ hiddenModel: false })
        this.sureAddCorrectService()
      }
      if (isFn(cb)) cb()
    }).catch(res => {
      if (isFn(cb)) cb()
    })
  },
  /**
  * 添加批改服务
  */
  sureAddCorrectService: function (cb) {
    const continuousFn = { fn: this.sureAddCorrectService, param: { ...arguments } }
    const { vipId, libId, number, priceType, beginDate, endDate } = this.data;
    console.log(vipId, libId, number, priceType, beginDate, endDate)
    AddCorrectService({ data: { vipId, libId, number, priceType, beginDate, endDate }, continuousFn }).then(res => {
      wx.showToast({
        title: '服务添加成功',
        icon: 'none',
        duration:1500
      })
      setTimeout(function(){
        wx.navigateBack({})
      },1500)
      if (isFn(cb)) cb()
    }).catch(res => {
      if (isFn(cb)) cb()
    })
  },
  /**
  * 从校区移除小孩
  */
  sureDelLibChild: function (cb) {
    const continuousFn = { fn: this.sureDelLibChild, param: { ...arguments } }
    const { vipId, libId } = this.data;
    DelLibChild({ data: { vipId, libId }, continuousFn }).then(res => {
      wx.showToast({
        title: '操作成功',
        icon: 'none'
      })
      this.allShowModel()
      wx.navigateBack({})
      if (isFn(cb)) cb()
    }).catch(res => {
      if (isFn(cb)) cb()
    })
  },
  /**
   * 获取数据
   */
  getData: function (cb) {
    const continuousFn = { fn: this.getData, param: { ...arguments } }
    const { vipId, pageNo, pageSize, list} = this.data
    // console.log(new Date())
    HomeworkList({ data: { vipId, pageNo, pageSize}, continuousFn }).then(res => {
      // const {list} = res.data;
      const { derver } = res.data;
      const listTemp = (res => {
        for (let i in res) {
          res[i].times = res[i].date.slice(0,10)
          
        }
        return res
      })(res.data.list)
      let arr = []
      if (pageNo == 1) {
        arr = [...listTemp]
      } else {
        arr = [...list, ...listTemp]
      }
      this.setData({
        derver,
        list: arr,
        pageNo: listTemp.length == pageSize ? pageNo + 1 : pageNo,
        isAll: listTemp.length < pageSize,
        isLoaded: true
      })
      if (isFn(cb)) cb()
    }).catch(res => {
      this.setData({ isLoaded: true })
      if (isFn(cb)) cb()
    })
  },
})