// pages/index/transaction/transaction.js
const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const app = getApp()
const {formatDate02} = 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
  },
  onLoad(e) {
    let {
      page,
      getFullYear,
      getMonth,
      getDate,
      dateTime
    } = this.data
    let nowTime = getFullYear + '-' + getMonth + '-' + getDate
    this.setData({
      dateTime: nowTime,
      nowDateTime: nowTime
    })
    this.getrecordData()
  },
  onReachBottom: function () {
    var that = this;
    that.data.pageNum++;
    that.setData({
      pageNum: that.data.pageNum,
      loading: true
    });
    that.getrecordData();
  },
  /**
  * 刷新
  */
  onPullDownRefresh: function (e) {
    let that = this;
    that.setData({
      pageNum: 1,
      list: []
    });
    that.getrecordData();
  },
  onShow() {
    this.setData({
      pageNum: 1
    })
    this.getrecordData()
  },
  changeDate(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() {
    let _this = this;
    let postData = {
      userId: wx.getStorageSync('bUserId'),
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum
    }
    Http.get({
      url: config.api.listCUser,
      data: postData
    })
      .then(res => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        let data=[];
        res.data.list.map((item, index) => {
          let items = JSON.parse(item.info)
          let a={};
          a.id = item.id;
          a.name = items.name ? items.name:'';
          a.phone = items.phone ? items.phone:'';
          a.address = items.address ? items.address:'';
          a.nickName = items.nickName?items.nickName:'';
          a.birthdate = items.birthdate?formatDate02(items.birthdate):'';
          a.sex = items.sex==1?'男':'女';
          data.push(a)
        })
        console.log(this.data.list,data, listData, 44444444444)
        let listData = [];
        if (this.data.pageNum == 1) {
          listData = data;
        } else {
          listData = _this.data.list;
        }
        data.map((item, index) => {
          let haveIf = false;
          listData.map((item02, index02) => {
            if (item02.id == item.id) {
              haveIf = true;
            }
          })
          if (!haveIf) {
            listData.push(item)
          }
        })
        console.log(this.data.list, listData,44444444444)
        if (res.data.pages <= _this.data.pageNum) {
          _this.setData({
            list: listData,
            pageNum: res.data.pages - 1,
            pages: res.data.pages,
            content: '已经加载全部数据！'
          })
        } else {
          _this.setData({
            list: listData,
            loading: false,
            content: '小主,我在玩命加载中...'
          })
        }
      })
      .catch(err => {
        wx.hideLoading();
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  refund(e) {

  },
  //往回传用户选择的日期渲染不同的数据
  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
        });
      });
  }
})