const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const format = require('../../utils/util.js')
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    date: "2018-12-22", //默认起始时间  
    date2: "2018-12-22", //默认结束时间 
    list: [],
    showNocontent: false,
    todayDate: null,
    page: 1,
    length: 0,
    showNocontent: true,
    sum_real_payment1: 0,
    sum_real_payment2: 0,
    sum_real_payment3: 0,
    tabIndex: 0
  },

  // 更改tab栏
  changeTabs(e) {
    this.setData({
      list: [],
      tabIndex: e.target.id,
    })
    let that = this;
    let startdate = that.data.date;
    let enddate = that.data.date2;
    if (startdate == '选择开始日期' || enddate == '选择结束日期') {
      wx.showToast({
        title: '请输入开始和结束时间',
        icon: "none"
      })
    } else {
      that.getList(startdate, enddate, 1);
      that.payMum1(startdate, enddate);
    }
  },

  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: '选择结束日期',
    });
  },
  search: function () {
    let that = this;
    let startdate = that.data.date;
    let enddate = that.data.date2;
    if (startdate == '选择开始日期' || enddate == '选择结束日期') {
      wx.showToast({
        title: '请输入开始和结束时间',
        icon: "none"
      })
    } else {
      that.getList(startdate, enddate, 1);
      that.payMum1(startdate, enddate);
      // that.payMum2(startdate, enddate);
      // that.payMum3(startdate, enddate);
    }
  },
  payMum1: function (startdate, enddate) {
    let that = this;
    let postData = {
      merchantId: app.globalData.merchant.merchant_id,
      startdate: startdate,
      enddate: enddate,
      payType: this.data.tabIndex
    };
    Http.get({
      url: config.api.payMum,
      data: postData
    })
      .then(res => {
        console.log(res);
        if (res.data.length > 0) {
          that.setData({
            sum_real_payment1: Number(res.data[0].sum_deduction_amount ? res.data[0].sum_deduction_amount : 0)
          })
        } else {
          that.setData({
            sum_real_payment1: Number(res.data.sum_deduction_amount ? res.data.sum_deduction_amount : 0)
          })
        }
      })
      .catch(err => {
        console.log(err)
      })
  },
  payMum2: function (startdate, enddate) {
    let that = this;
    let postData = {
      merchantId: app.globalData.merchant.merchant_id,
      startdate: startdate,
      enddate: enddate,
      payStatus: 1,
      payType: this.data.tabIndex
    };
    Http.get({
      url: config.api.payMum,
      data: postData
    })
      .then(res => {
        console.log(res);
        if (res.data.length > 0) {
          that.setData({
            sum_real_payment2: Number(res.data[0].sum_deduction_amount ? res.data[0].sum_deduction_amount : 0)
          })
        } else {
          that.setData({
            sum_real_payment2: Number(res.data.sum_deduction_amount ? res.data.sum_deduction_amount : 0)
          })
        }
      })
      .catch(err => {
        console.log(err)
      })
  },
  payMum3: function (startdate, enddate) {
    let that = this;
    let postData = {
      merchantId: app.globalData.merchant.merchant_id,
      startdate: startdate,
      enddate: enddate,
      payStatus: 2,
      payType: this.data.tabIndex
    };
    Http.get({
      url: config.api.payMum,
      data: postData
    })
      .then(res => {
        console.log(res);
        if (res.data.length > 0) {
          that.setData({
            sum_real_payment3: Number(res.data[0].sum_deduction_amount ? res.data[0].sum_deduction_amount : 0)
          })
        } else {
          that.setData({
            sum_real_payment3: Number(res.data.sum_deduction_amount ? res.data.sum_deduction_amount : 0)
          })
        }
      })
      .catch(err => {
        console.log(err)
      })
  },
  getList: function (startdate, enddate, page) {
    let that = this;
    let postData = {
      pageNum: page,
      pageSize: 10,
      startdate: startdate,
      enddate: enddate,
      payType: this.data.tabIndex
    }
    Http.get({
      url: config.api.cardPayList,
      data: postData
    })
      .then(res => {
        res.data.list.forEach(item => {
          item.cardId = item.cardId.slice(0, 4) + `******` + item.cardId.slice(14)
          item.createDate = that.timestampToTime(item.createDate, 'YYYY-MM-DD hh:mm:ss')
        })
        if (page == 1) {
          that.setData({
            list: [],
            page: 1
          })
        }
        var tmpArr = that.data.list;
        tmpArr.push.apply(tmpArr, res.data.list);
        if (res.code == 200 && tmpArr.length > 0) {
          tmpArr.map(file => {
            file.updateDate = format.formatTime(file.updateDate, 'yyyy-MM-dddd hh:mm:ss')
            console.log(file.updateDate)
          })
          that.setData({
            length: res.data.total,
            list: tmpArr
          })
        } else {
          that.setData({
            length: 0,
            showNocontent: true
          })
        }
      })
  },

  /**
  * @description 根据时间戳获取时间
  * @param {*} timestamp 必传，number类型，时间戳数据（10位及以下，10位至13位）;若不传，则返回：“无时间戳”
  * @param {*} format  选传，string类型，提供以下时间格式：YYYY-MM-DD hh:mm:ss、YYYY/MM/DD hh:mm:ss、YYYY.MM.DD hh:mm:ss、YYYY MM DD hh:mm:ss、YYYY年MM月DD日 hh:mm:ss、YYYY-MM-DD、YYYY/MM/DD、YYYY.MM.DD、YYYY MM DD、YYYY年MM月DD日；若不传，则默认为：YYYY-MM-DD
  * @returns 根据要求的时间格式
  * @version V 1.0, Created by YWQ, 2022.10.20
  */
  timestampToTime(timestamp, format) {
    //时间戳为10位需*1000，时间戳为13位不需乘1000
    const length = timestamp.length
    if (length <= 10) {
      var date = new Date(timestamp * 1000)
    } else {
      var date = new Date(timestamp)
    }
    let Y = String(date.getFullYear())
    let M = String(date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
    let D = String(date.getDate() + 1 < 10 ? '0' + (date.getDate()) : date.getDate())
    let h = String(date.getHours() + 1 < 10 ? '0' + (date.getHours()) : date.getHours())
    let m = String(date.getMinutes() + 1 < 10 ? '0' + (date.getMinutes()) : date.getMinutes())
    let s = String(date.getSeconds() + 1 < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
    // return Y + M + D + h + m + s
    if (format == "YYYY-MM-DD hh:mm:ss") {
      return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY/MM/DD hh:mm:ss") {
      return Y + "/" + M + "/" + D + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY.MM.DD hh:mm:ss") {
      return Y + "." + M + "." + D + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY MM DD hh:mm:ss") {
      return Y + " " + M + " " + D + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY年MM月DD日 hh:mm:ss") {
      return Y + "年" + M + "月" + D + "日" + " " + h + ":" + m + ":" + s
    } else if (format == "YYYY-MM-DD") {
      return Y + "-" + M + "-" + D
    } else if (format == "YYYY/MM/DD") {
      return Y + "/" + M + "/" + D
    } else if (format == "YYYY.MM.DD") {
      return Y + "." + M + "." + D
    } else if (format == "YYYY MM DD") {
      return Y + " " + M + " " + D
    } else if (format == "YYYY年MM月DD日") {
      return Y + "年" + M + "月" + D + "日"
    } else {
      return Y + "-" + M + "-" + D
    }
  },

  onReachBottom: function () {
    let that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    that.getList(that.data.date, that.data.date2, that.data.page);
  },
})