const app = getApp()
import $http from './../../API/http'
Page({
  data:{
    time: '',
    orderStartTime: '',
    orderEndTime: '',
    orderType: '',
    orderStatus: '',
    sceneType: '',
    statusList: ['SUCCESS', 'REFUND_SUCCESS'],
    recordListData: [],
    currentPage: 1,
    totalCount: '',
    pageNumber: 20,
  },
  onLoad(query) {
    this.resetData()
    dd.setNavigationBar({title: '账单记录'});
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth()+1;
    let day = (new Date(year,month,0).getDate().toString()).length > 1 ? new Date(year,month,0).getDate() : '0' + new Date(year,month,0).getDate()
    this.setData({
      //  刚进入账单记录页面,时间不够两位补0
      time: `${year}-${month.toString().length > 1 ? month : '0' + month}`,
      orderStartTime: `${year}-${month.toString().length > 1 ? month : '0' + month}-01`,
      orderEndTime: `${year}-${month.toString().length > 1 ? month : '0' + month}-${ day }`
    })
  },
  resetData(){
    this.setData({
      time: '',
      orderStartTime: '',
      orderEndTime: '',
      orderType: '',
      orderStatus: '',
      sceneType: '',
      statusList: ['SUCCESS', 'REFUND_SUCCESS'],
      recordListData: [],
      currentPage: 1,
      totalCount: '',
      pageNumber: 20,
    })
  },
  chooseType() {
    const _that = this;
    dd.showActionSheet({
      title: '',
      items: ['全部', '消费', '充值', '退款'],
      cancelButtonText: '取消',
      success: (res) => {
        const btn = res.index === -1 ? '取消' : '第' + res.index + '个';
       if(res.index === -1){
         return
       }else{
          switch (res.index) {
          case 0:
            _that.setData({
              orderType: '',
              sceneType: '',
              statusList: ['SUCCESS', 'REFUND_SUCCESS'],
              currentPage: 1,
              recordListData: []
            })
            break;
          case 1:
            _that.setData({
              orderType: 'CONSUME',
              sceneType: '',
              statusList: 'SUCCESS',
              currentPage: 1,
              recordListData: []
            })
            break;
          case 2:
            _that.setData({
              orderType: 'RECHARGE',
              sceneType: '',
              statusList: ['SUCCESS'],
              currentPage: 1,
              recordListData: []
            })
            break;
          case 3:
            _that.setData({
              orderType: 'CONSUME',
              sceneType: '',
              statusList: 'REFUND_SUCCESS',
              currentPage: 1,
              recordListData: []
            })
        }
        _that.queryListPaymentRecord()
       }
      },
    });
  },
  queryListPaymentRecord(str) {
    const _that = this;
    let data = {
      currentPage: this.data.currentPage,
      pageNumber: this.data.pageNumber,
      //第三方企业修改:原先改成注释内容,后改过来
      userIds: [dd.getStorageSync({ key: 'userId' }).data],
      //  ddUserIds: [dd.getStorageSync({ key: 'userId' }).data],
      orderStartTime: this.data.orderStartTime,
      orderEndTime: this.data.orderEndTime,
      orderType: this.data.orderType,
      sceneType: this.data.sceneType,
      // 第三方企业修改:原先改成orderStatus,后改过来
      statusList: this.data.statusList
    }
    $http.$http('isv/consume/list_payment_record', data, 'GET').then((res) => {
      let dataList = [...res.recordList];
      let recordListDataSource = _that.data.recordListData
      if (dataList.length > 0) {
        dataList.map((item, index) => {
          item.createDate = _that.timeToStr(item.createDate)
          recordListDataSource.push(item)
        })
        let totalCount = Math.ceil(parseInt(res.totalCount)/10)
        _that.setData({
          recordListData: recordListDataSource,
          totalCount: totalCount
        })
        dd.hideLoading();
      } else {
        _that.setData({
          recordListData: [],
          totalCount: 1
        })
        dd.hideLoading();
      }
    }).catch((res) => {
      console.log(res)
    })
  },
  goToDetails(event){
    dd.navigateTo({url:`./../billDetails/billDetails?trxNo=${event.target.dataset.trxNo}`})
  },
  choosesCeneType() {
    const _that = this;
    dd.showActionSheet({
      title: '',
      items: ['全部', '餐补津贴', '交通补助'],
      cancelButtonText: '取消',
      success: (res) => {
        console.log(res)
       if(res.index === -1){
         return
       }else{
          switch (res.index) {
          case 0:
            _that.setData({
              sceneType: '',
              recordListData: []
            })
            break;
          case 1:
            _that.setData({
              sceneType: 'MEAL',
              currentPage: 1,
              recordListData: []
            })
            break;
          case 2:
            _that.setData({
              sceneType: 'TRAVEL',
              currentPage: 1,
              recordListData: []
            })
        }
        _that.queryListPaymentRecord()
       }
      },
    });
  },
  onReady() {
    this.queryListPaymentRecord()
  },
  timeToStr (time) {
    time = time.replace(/\-/g, "/")
    let timeData = new Date(time)
    let year = timeData.getFullYear()
    let month = timeData.getMonth() + 1
    let date = timeData.getDate()
    let hour = timeData.getHours()
    let minute = timeData.getMinutes()
    return `${year}年${month.toString().length > 1 ? month : '0' + month}月${date.toString().length > 1 ? date : '0' + date}日${hour.toString().length > 1 ? hour : '0' + hour}:${minute.toString().length > 1 ? minute : '0' + minute}`
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  chooseTime() {
    const _that = this;
    dd.datePicker({
      format: 'yyyy-MM',
      currentDate: this.data.time,
      success: (res) => {
        let date = res.date.split('-')
        let year = date[0];
        let month = date[1];
        let day = (new Date(year,month,0).getDate().toString()).length > 1 ? new Date(year,month,0).getDate() : '0' + new Date(year,month,0).getDate()
        _that.setData({
          time: res.date,
          orderStartTime: `${year}-${month.toString().length > 1 ? month : '0' + month}-01`,
          orderEndTime: `${year}-${month.toString().length > 1 ? month : '0' + month}-${ day }`,
          currentPage: 1,
          recordListData: []
        })
        _that.queryListPaymentRecord()
      },
    });
  },
  onUnload() {
    // 页面被关闭
    dd.stopPullDownRefresh()
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
    this.setData({
      time: '',
      orderStartTime: '',
      orderEndTime: '',
      orderType: '',
      orderStatus: '',
      sceneType: '',
      statusList: ['SUCCESS', 'REFUND_SUCCESS'],
      recordListData: [],
      currentPage: 1,
      totalCount: '',
      pageNumber: 20,
    })
    this.resetData()
    dd.setNavigationBar({title: '账单记录'});
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth()+1;
    let day = (new Date(year,month,0).getDate().toString()).length > 1 ? new Date(year,month,0).getDate() : '0' + new Date(year,month,0).getDate()
    this.setData({
      //  刚进入账单记录页面,时间不够两位补0
      time: `${year}-${month.toString().length > 1 ? month : '0' + month}`,
      orderStartTime: `${year}-${month.toString().length > 1 ? month : '0' + month}-01`,
      orderEndTime: `${year}-${month.toString().length > 1 ? month : '0' + month}-${ day }`
    })
    this.queryListPaymentRecord()
    dd.stopPullDownRefresh()
  },
  onReachBottom() {
    // dd.alert({
    //   content: '111111',
    //    buttonText: '确定'
    // })
    let currentNum = '';
    if (this.data.currentPage < this.data.totalCount) {
      currentNum = this.data.currentPage + 1
      this.setData({
        currentPage: currentNum
      })
      this.queryListPaymentRecord(currentNum)
    }
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});