const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const format = require('../../utils/util.js')
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    date: "", //默认起始时间  
    date2: "", //默认结束时间 
    list: [],
    typeList: [{
      name: '微信支付',
      value: 1
    }],
    statusList: [{
      name: '交易成功',
      value: 1
    }],
    showNocontent: true,
    todayDate: null,
    amount: "",
    total: "",
    page: 1,
    pageNum: 1,
    type: "",
  },

  bindDateChange(e) {
    let that = this;
    that.setData({
      date: e.detail.value + " 00:00:00",
    })
    console.log(that.data.date)
  },
  bindDateChange2(e) {
    let that = this;
    that.setData({
      date2: e.detail.value + " 23:59:59",
    })
    console.log(that.data.date2)
  },

  onShow: function () {
    let that = this;
    that.setData({
      date: '选择开始日期',
      date2: '选择结束日期',
    });
  },

  onLoad(option) {
    console.log(option, 'option');
    this.setData({
      type: option.type
    })
    if (option.type == "transaction") {
      wx.setNavigationBarTitle({
        title: '核销记录',
      })
    } else if (option.type == "record") {
        wx.setNavigationBarTitle({
          title: '收银记录',
        })
      } else if (option.type == "pos") {
        wx.setNavigationBarTitle({
          title: '交易流水',
        })
      }
  },

  search: function () {
    let that = this;
    let startdate = that.data.date;
    let enddate = that.data.date2;
    console.log(startdate, enddate);
    if (startdate == "选择开始日期" || enddate == "选择结束日期") {
      wx.showToast({
        title: '请选择起止时间！',
        icon: "none"
      })
      return
    }
    that.setData({
      pageNum: 1
    })
    this.getList()
    this.getSum()
  },

  getList() {
    const that = this
    const pageNum = that.data.pageNum
    const type = that.data.type
    const data = {
      pageNum,
      pageSize: 20,
      startDate: that.data.date,
      endDate: that.data.date2,
      phone: type == "pos" ? wx.getStorageSync("linkPhone") : undefined,
      createBegin: type == "pos" ? that.data.date : undefined,
      createEnd: type == "pos" ? that.data.date2 : undefined,
    }
    // const url = type == "transaction"
    //   ? config.api.couponOrderListVerifiedV2
    //   : config.api.micropayListMicroPayV2
      let url = ''
      if (type == "transaction") {
        url = config.api.couponOrderListVerifiedV2
      } else if (type == "record"){
        url = config.api.micropayListMicroPayV2
      } else if (type == "pos") {
        url = config.api.oneMerchantOrderlist
      }
    Http.get({
      url,
      data
    })
      .then(res => {
        wx.hideLoading()
        console.log(res, 'res');
        if (type == "pos") {
            if (res.data.list) {
                that.setData({
                    list: res.data.list,
                })
                that.setData({
                    showNocontent: false
                })
            }
            return
        }
        if (res.data.list && res.data.list.list && res.data.list.list.length > 0) {
          that.setData({
            showNocontent: false
          })
          res.data && res.data.list.list.map(file => {
            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)

            if (type == "record") {
              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
          })
        }
      }).catch(err => {
        console.log(err, 'err');
      })
  },

  getSum() {
    const that = this
    const type = that.data.type
    const url = type == "transaction"
      ? config.api.couponOrderSumVerified
      : config.api.micropaySumMicroPay
    const data = {
      startDate: that.data.date,
      endDate: that.data.date2
    }
    Http.get({
      url,
      data
    })
      .then(res => {
        console.log(res, 'res');
        that.setData({
          amount: res.data.amount / 100,
          total: res.data.total
        })
      }).catch(err => {
        console.log(err, 'err');
      })
  },

  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中',
    })
    let pageNum = this.data.pageNum + 1
    this.setData({
      pageNum
    })
    this.getList()
  },
})