// pages/index/transaction/transaction.js
const config = require('../../../config/config.js')
const Http = require('../../../utils/HttpBasics.js')
const app = getApp()
const format = require('../../../utils/util.js')

Page({
  data: {
    maxIf: false,
    list: [],
    typeList: [{
      name: '微信支付',
      value: 1
    }],
    statusList: [{
      name: '交易成功',
      value: 1
    }],
    getFullYear: new Date().getFullYear(),
    getMonth: new Date().getMonth() + 1,
    getDate: new Date().getDate(),
    dateTime: '',
    flag: true,
    text: '',
    dataList: [],
    nowDateTime: '',
    hide: false,
    name: '',
    timeName: '',
    pageNum: 1,
    pageSize: 5,
    pages: 0,
    amount: "",
    list: "",
  },
  //进到记录页面初始化显示的数据
  getList(statetId, page, dateTime) {
    let {
      dataList,
      show,
      isShow,
      listInfo
    } = this.data
    console.log("page", page);
    if (statetId == 0) {

    } else {

    }
  },
  onLoad(e) {
    let {
      page,
      getFullYear,
      getMonth,
      getDate,
      dateTime
    } = this.data
    let nowTime = getFullYear + '-' + getMonth + '-' + getDate
    this.setData({
      dateTime: nowTime,
      nowDateTime: nowTime
    })
  },
  onReachBottom() {
    // let {
    //   pageNum,
    //   pages,
    //   dateTime,
    // } = this.data
    // this.setData({
    //   pageNum: pageNum = ++pageNum
    // })
    // console.log(pages, pageNum)
    // if (pages >= pageNum) {
    //   wx.showLoading({
    //     title: '玩命加载中',
    //   })
    //   this.getrecordData('bottom')
    // } else {
    //   this.setData({
    //     maxIf: true,
    //     pageNum: pageNum = --pageNum
    //   })
    // }
    wx.showLoading({
      title: '玩命加载中',
    })
    let pageNum = this.data.pageNum + 1
    this.setData({
      pageNum
    })
    this.getListV2()
    this.getSum()
  },

  getListV2() {
    const that = this
    const pageNum = that.data.pageNum
    const data = {
      pageNum,
      pageSize: 10,
      startDate: that.data.todayDate + " 00:00:00",
      endDate: that.data.todayDate + " 23:59:59"
      // startDate: " 00:00:00",
      // endDate: " 23:59:59"
    }
    Http.get({
      url: config.api.micropayListMicroPayV2,
      data
    })
      .then(res => {
        console.log(res, 'res');
        if (res.data.list && res.data.list.list.length > 0) {
          res.data && res.data.list.list.map(file => {
            file.payment = file.payment / 100
            file.expiredTime = format.formatTime(file.expiredTime, 'yyyy-MM-dddd hh:mm:ss')
            file.createDate = format.formatTime(file.createDate, 'yyyy-MM-dddd hh:mm:ss')
            file.updateDate = format.formatTime(file.updateDate, 'yyyy-MM-dddd hh:mm:ss')
            file.ids = file.id.slice(0, 4) + `******` + file.id.slice(14)
            that.data.typeList.map((item01) => {
              if (file.type == item01.value) {
                file.type = item01.name
              }
            })
            that.data.statusList.map((item01) => {
              if (file.orderStatus == item01.value) {
                file.orderStatus = item01.name
              }
            })
          })
        }
        if (pageNum == 1) {
          that.setData({
            list: res.data.list.list,
            // amount: res.data.amount / 100
          })
        } else if (pageNum > 1) {
          const list = that.data.list
          // let amount = that.data.amount + res.data.amount
          res.data.list.list.forEach(item => {
            list.push(item)
          })
          that.setData({
            list,
            // amount
          })
        }

        wx.hideLoading()
      }).catch(err => {

      })
  },

  getSum() {
    const that = this
    const data = {
      startDate: that.data.todayDate + " 00:00:00",
      endDate: that.data.todayDate + " 23:59:59"
    }
    Http.get({
      url: config.api.micropaySumMicroPay,
      data
    })
      .then(res => {
        console.log(res, 'res');
        that.setData({
          amount: res.data.amount / 100,
          total: res.data.total
        })
      }).catch(err => {
        console.log(err, 'err');
      })
  },

  onShow() {
    let myDate = new Date();
    let date_value = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate();
    this.setData({
      todayDate: date_value,
      pageNum: 1
    })
    // this.getrecordData()
    this.getListV2()
    this.getSum()
  },
  changeDate(time) {
    time = parseInt(time)
    return (new Date(time).getFullYear() + '-' + (Number(new Date(time).getMonth()) + 1) + '-' + new Date(time).getDate() + '  ' + (new Date(time).getHours() < 10 ? '0' + new Date(time).getHours() : new Date(time).getHours()) + ':' + (new Date(time).getMinutes() < 10 ? '0' + new Date(time).getMinutes() : new Date(time).getMinutes()) + ':' + (new Date(time).getSeconds() < 10 ? '0' + new Date(time).getSeconds() : new Date(time).getSeconds()))
  },
  // 刷卡支付交易流水分页列表接口
  getrecordData(formType) {
    let _this = this;
    let postData = {
      date: this.data.dateTime,
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum
    }
    Http.get({
      url: config.api.micropayListMicroPay,
      data: postData
    })
      .then(res => {
        wx.hideLoading();
        res.data.list.list.map((item, index) => {
          item.createDate = _this.changeDate(item.createDate)
          item.payment = item.payment / 100
          _this.data.typeList.map((item01, index01) => {
            if (item.type == item01.value) {
              item.type = item01.name
            }
          })
        })
        _this.setData({
          pages: res.data.list.pages,
          list: formType == 'bottom' ? _this.data.list.concat(res.data.list.list) : res.data.list.list
        })
        console.log(_this.data.list, "lsit")
      })
      .catch(err => {
        wx.hideLoading();
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  //往回传用户选择的日期渲染不同的数据
  bindDateChange(e) {
    this.setData({
      dateTime: e.detail.value,
      pageNum: 1
    })
    this.getrecordData()
  },
  refundPament(e) {
    let _this = this;
    Http.post({
      url: config.api.microPayRefund,
      data: {
        orderId: e.currentTarget.id
      }
    })
      .then(res => {
        // console.log(666)
        wx.showToast({
          title: '退款成功！',
          icon: 'success',
          duration: 2000,
          mask: false
        });
        _this.getrecordData()
      })
      .catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  findByDate() {
    wx.navigateTo({
      url: '/pages/receiveDateX/receiveDate?type=record',
    })
  }
})