const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const format = require('../../utils/util.js')
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    showNocontent: false,
    sum_real_payment1: 0,
    sum_real_payment2: 0,
    sum_real_payment3: 0,
    page: 1,
    tabIndex: 0
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    // 获取当前的时间
    let myDate = new Date();
    let date_value = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate();
    that.setData({
      todayDate: date_value
    })
    that.getList(1);
    that.payMum1();
    // that.payMum2();
    // that.payMum3();
  },
  getList(page) {
    let that = this;
    let postData = {
      pageNum: page,
      pageSize: 10,
      startdate: this.data.todayDate + " 00:00:00",
      enddate: this.data.todayDate + " 23:59:59",
      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: []
          })
        }
        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
          })
        }
      })
  },
  payMum1: function () {
    let that = this;
    let postData = {
      merchantId: app.globalData.merchant.merchant_id,
      startdate: this.data.todayDate + " 00:00:00",
      enddate: this.data.todayDate + " 23:59:59",
      payType: this.data.tabIndex
    };
    Http.get({
      url: config.api.payMum,
      data: postData
    })
      .then(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 () {
    let that = this;
    let postData = {
      merchantId: app.globalData.merchant.merchant_id,
      startdate: this.data.todayDate + " 00:00:00",
      enddate: this.data.todayDate + " 23:59:59",
      payStatus: 1,
      payType: this.data.tabIndex
    };
    Http.get({
      url: config.api.payMum,
      data: postData
    })
      .then(res => {
        if (res.data.length > 0) {
          console.log(res.data[0].sum_real_payment, 222)
          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 () {
    let that = this;
    let postData = {
      merchantId: app.globalData.merchant.merchant_id,
      startdate: this.data.todayDate + " 00:00:00",
      enddate: this.data.todayDate + " 23:59:59",
      payStatus: 2,
      payType: this.data.tabIndex
    };
    Http.get({
      url: config.api.payMum,
      data: postData
    })
      .then(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)
      })
  },
  // 跳转到按日期查询界面
  gotoDate: function () {
    wx.navigateTo({
      url: '/pages/receiveDate/receiveDate',
    })
  },

  /**
* @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
    }
  },
  // 更改tab栏
  changeTabs(e) {
    this.setData({
      list: [],
      tabIndex: e.target.id,
    })
    this.getList(1);
    this.payMum1();
  },
  //加载更多
  onReachBottom: function () {
    let that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    that.getList(that.data.page);
  },
})