// pages/ucenter/zhanw/zhanw.js
let Util = require("../../../utils/util");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchIndex:-1,
    occupyFeeOrderList: [],
    pagesize:10,
    pagenum:1,
    total:-1,
    loadTotal: 0,
    dataLoading: false,
    finishedLoadTap: 0,
    finishedLoadShowTimes: 0,
    chongdianzhuangmingcheng: false,
    chongdianzhuangbianhao: false,
    chongdianshichang: false,
    chongdiankaishijieshu: false,
    customerServiceFlag: false,
    cancleBtnFlag: false,
    servicetel: getApp().globalData.helpPhoneNum,
  },
  switchBar(e){
    let searchIndex = e.currentTarget.dataset.idx;
    this.setData({
      searchIndex
    })
    this.resetListZero();
    this.getPage();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let userInfo = wx.getStorageSync('userInfo');
    let isLogin = wx.getStorageSync('isLogin');
    let that = this;
    // 页面显示
    if (userInfo && isLogin) {
      this.resetListZero();
      this.getPage();
    }
    
  },
  resetListZero(){
    let occupyFeeOrderList =  []
    let pagesize = 10
    let pagenum = 1
    let total = -1
    let loadTotal =  0
    let dataLoading =  false
    let finishedLoadTap =  0
    let finishedLoadShowTimes =  0
    this.setData({
      occupyFeeOrderList,
      pagesize,
      pagenum,
      total,
      loadTotal,
      dataLoading,
      finishedLoadTap,
      finishedLoadShowTimes,
    })
  },
  getPage(){
    // console.info(this.data.occupyFeeOrderList.length + " MMM " + this.data.total)
    
    if(this.data.occupyFeeOrderList.length == this.data.total){
      this.setData({
        finishedLoadTap:this.data.finishedLoadTap+1
      })
      if(this.data.finishedLoadTap>0 && this.data.finishedLoadShowTimes==0){
        this.setData({
          finishedLoadShowTimes:this.data.finishedLoadShowTimes+1
        })
        wx.showToast({
          title: '全部加载完毕',
          icon: 'success',
          duration: 2000
        })
      }
      return
    }

    if(this.data.dataLoading){
      return
    }
    this.setData({
      dataLoading: true
    })
    wx.showLoading({
      title: '数据加载中....',
    })

  let that = this;

    wx.request({
      url: getApp().globalData.postHeadAgreement +'/restapi/wechat/queryOccupyFeeOrder',
      data: {
        userId: wx.getStorageSync('userInfo').userId,
        searchIndex: that.data.searchIndex<0?null:that.data.searchIndex,
        pagenum: this.data.pagenum++,
        pagesize: 10,
      },
      method: 'POST',
      success(res) {
        console.log("占位费",res);
        if (res.data.result.rows) {
          let {
            rows: occupyFeeOrderList
          } = res.data.result;
          // that.startTimeOrder(charginglogs);
          that.formatCharginglogs(occupyFeeOrderList);
          wx.hideLoading()
          that.setData({
            occupyFeeOrderList: that.data.occupyFeeOrderList.concat(occupyFeeOrderList),
            total: res.data.result.total,
            dataLoading: false
          });
        }
      }
    });
  },


  formatCharginglogs(formatCharginglogs) {
    formatCharginglogs.forEach((item, index) => {
      try {
        console.log("item",item);
          // 时间截取
          item.occupyBeginTime = item.occupyBeginTime.substr(0,16)
          item.occupyEndTime = item.occupyEndTime.substr(0,16)
          // 状态翻译
          if(item.status==3){
            item.occupyStatusCN = '未支付'
          }else if(item.status==4){
            item.occupyStatusCN = '已支付'
          }else if(item.status==6){
            item.occupyStatusCN = '已取消'
          }else if(item.status==7){
            item.occupyStatusCN = '已退款'
          };
          // 判断标记为  chongdianzhuangmingcheng chongdianzhuangbianhao chongdianshichang chongdiankaishijieshu
          // let chongdianzhuangmingcheng =  false
          // let chongdianzhuangbianhao =  false
          // let chongdianshichang =  false
          // let chongdiankaishijieshu =  false
          // let chongdianfeiyong =  false
          let customerServiceFlag = false;
          let cancleBtnFlag= false;
          let height= 'h3';

          if(item.status==3){
            customerServiceFlag = true;
          }
          if(item.status==3 || item.status == 4){
            cancleBtnFlag = true;
          }
          // item.chongdianzhuangmingcheng = chongdianzhuangmingcheng;
          // item.chongdianzhuangbianhao = chongdianzhuangbianhao;
          // item.chongdianshichang = chongdianshichang;
          // item.chongdiankaishijieshu = chongdiankaishijieshu;
          // item.chongdianfeiyong = chongdianfeiyong;
          item.customerServiceFlag = customerServiceFlag;
          item.cancleBtnFlag = cancleBtnFlag;
          item.height = height;
        //console.log(item.chargPile);
      } catch (err) {
        //在这里处理错误
      }
      try {
        let chargePile = item.pileLog;
        console.info(chargePile)
        let startDate = Util.parseDate(chargePile.chargstarttime);
        let endDate = Util.parseDate(chargePile.chargendtime)
        let totalTime = Math.round((endDate.getTime() - startDate.getTime()) / 60000);
        let totalTimeHour = Math.floor(totalTime / 60);
        let totalTimeMinute = totalTime % 60;
        let startTime = startDate.getFullYear() + "年" + Util.formatNumber(startDate.getMonth() + 1) + '月' + Util.formatNumber(startDate.getDate()) + '日  ' + Util.formatNumber(startDate.getHours()) + ':' + Util.formatNumber(startDate.getMinutes()) + ':' + Util.formatNumber(startDate.getSeconds());
        let endTime = endDate.getFullYear() + "年" + Util.formatNumber(endDate.getMonth() + 1) + '月' + Util.formatNumber(endDate.getDate()) + '日  ' + Util.formatNumber(endDate.getHours()) + ':' + Util.formatNumber(endDate.getMinutes()) + ':' + Util.formatNumber(endDate.getSeconds());
        totalTime = Util.formatNumber(totalTimeHour) + "时" + Util.formatNumber(totalTimeMinute) + "分";
        chargePile.startTime = startTime;
        chargePile.endTime = endTime;
        chargePile.totalTime = totalTime;
        chargePile.chargallmoney = chargePile.chargallmoney.toFixed(2);
        chargePile.chargservice = chargePile.chargservice.toFixed(2);
        chargePile.chargmoney = chargePile.chargmoney.toFixed(2);
        //console.log(chargePile.chargPile);
      } catch (err) {
        //在这里处理错误
      }


    });

  },
  startTimeOrder(charginglogs) {
    if (charginglogs && 0 != charginglogs.length) {
      charginglogs.sort(function(ma, mb) {
        return mb.chargstarttime.localeCompare(ma.chargstarttime);
      });
    }
  },
  customerServiceBtn(){
    wx.makePhoneCall({
      phoneNumber: this.data.servicetel
    })
  },
  cancleBtn(e){
    let  that = this
    let reservId = e.currentTarget.dataset.idx;
    let userInfo = wx.getStorageSync('userInfo');
    let isLogin = wx.getStorageSync('isLogin');
    wx.showModal({
      title: '提示',
      confirmColor: '#00AADD',
      content: '若对当前占位费订单有疑问，请拨打客服电话4009608068，工作时间：08:00-18:00',
      complete: (res) => {
        // 可以在这里添加回调逻辑
        if (res.confirm) {
          console.log('用户点击了确认按钮');
        }else{
          console.log('用户点击了取消按钮');
        }
      }
    });


  },
  cancelM:function(e){
    this.setData({
       hiddenmodalput: true,
    })
 },

 confirmM: function (e) {
    console.log("姓名：" + this.data.name + "  电话：" + this.data.phoneNum);
 },

 iName: function (e) {
    this.setData({
       name:e.detail.value
    })
 },
 iPhoneNum: function (e) {
    this.setData({
       phoneNum: e.detail.value
    })
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  bindScrollTolowerEvent: function(){
    this.getPage();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})