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

Page({
  data: {
    list: [],
    getFullYear: new Date().getFullYear(),
    getMonth: new Date().getMonth() + 1,
    getDate: new Date().getDate(),
    dateTime: '',
    flag: true,
    defaultId: 0,
    text: '',
    page: 1,
    dataList: [],
    nowDateTime: '',
    hide: false,
    name: '',
    timeName: '',
    todayDate: "",
    pageNum: 1,
    amount: "",
    total: "",
    type: ""
  },
  //进到记录页面初始化显示的数据
  getList(statetId, page, dateTime) {
    let {
      dataList,
      show,
      isShow,
      listInfo
    } = this.data
    if (statetId == 0) {
      //今日交易额记录
      return new Promise((resolve, reject) => {
        Http.getRequest(config.api.couponOrderListUnVerified, app.globalData.token, '加载中', {
          date: dateTime,
          pageNum: page,
          pageSize: 8,
        }, (res) => {
          console.log(res)
          if (res.data.list.list && res.data.list.list.length > 0) {
            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)
              dataList.push(file)
            })
          } else {
            dataList = []
          }


          this.setData({
            list: dataList,
            hide: res.data.list.list == 0 ? true : false
          })

          console.log(this.data.list)
          resolve(res)
        })
      })
    } else {
      //今日核销记录
      return new Promise((resolve, reject) => {
        Http.getRequest(config.api.couponOrderListVerified, app.globalData.token, '加载中', {
          date: dateTime,
          pageNum: page,
          pageSize: 10,
        }, (res) => {
          console.log(res)
          if (res.data.list && res.data.list.list.length > 0) {
            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)
              dataList.push(file)
            })
          } else {
            dataList = []
          }

          this.setData({
            list: dataList,
            hide: res.data.list.list == 0 ? true : false
          })
          resolve(res)
        })
      })
    }
  },

  getListV2() {
    const that = this
    const pageNum = that.data.pageNum
    const data = {
      pageNum,
      pageSize: 20,
      startDate: that.data.todayDate + " 00:00:00",
      endDate: that.data.todayDate + " 23:59:59"
      // startDate: " 00:00:00",
      // endDate: " 23:59:59"
    }
    HttpBasics.get({
      url: config.api.couponOrderListVerifiedV2,
      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.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 (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"
    }
    HttpBasics.get({
      url: config.api.couponOrderSumVerified,
      data
    })
      .then(res => {
        console.log(res, 'res');
        that.setData({
          amount: res.data.amount / 100,
          total: res.data.total
        })
      }).catch(err => {
        console.log(err, 'err');
      })
  },

  onLoad(e) {
    let {
      page,
      getFullYear,
      getMonth,
      getDate,
      dateTime
    } = this.data
    let nowTime = getFullYear + '-' + getMonth + '-' + getDate
    this.setData({
      dateTime: nowTime,
      nowDateTime: nowTime,
      type: e.type
    })
    if (e.type == '0') { // 交易流水
      this.setData({
        flag: true,
        text: '未核销',
        defaultId: e.type,
        name: '购买用户',
        timeName: '购买时间'
      })
      wx.setNavigationBarTitle({
        title: '交易记录',
      })
      this.getList(e.type, page, nowTime)
    } else { // 核销
      this.setData({
        flag: false,
        text: '已核销',
        defaultId: e.type,
        name: '核销员',
        timeName: '核销时间'
      })
      wx.setNavigationBarTitle({
        title: '核销记录',
      })
      let myDate = new Date();
      let date_value = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate();
      this.setData({
        todayDate: date_value
      })

      this.getListV2()
      this.getSum()
    }

  },

  onShow() {

  },

  onReachBottom() {
    if (e.type == '0') {
      let {
        page,
        defaultId,
        dateTime,
      } = this.data
      page += 1
      this.getList(defaultId, page, dateTime)
    } else {
      wx.showLoading({
        title: '玩命加载中',
      })
      let pageNum = this.data.pageNum + 1
      this.setData({
        pageNum
      })
      this.getListV2()
      this.getSum()
    }
  },
  refund(e) {
    console.log(e.currentTarget.dataset.id) //从这里拿到当前的id
    Http.postRequest(config.api.refundOrderCreate, app.globalData.token, '加载中', {
      couponOrderId: e.currentTarget.dataset.id
    }, (res) => {
      if (res.code == 200) {
        // 退券退款申请成功
        wx.showToast({
          title: res.message,
          image: './../../../static/images/success.png'
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/main/main'
          })
        }, 1000)
      } else {
        wx.showModal({
          title: '错误',
          content: res.message,
          showCancel: false
        })
      }
    }, (err) => {
      console.log("err", err)
      wx.showModal({
        title: '错误',
        content: err.message,
        showCancel: false
      })
    })
  },
  //往回传用户选择的日期渲染不同的数据
  bindDateChange(e) {
    let {
      page,
      defaultId
    } = this.data
    this.setData({
      dateTime: e.detail.value,
      dataList: []
    })
    page = 1
    this.setData({
      page: page
    })
    this.getList(defaultId, page, e.detail.value)
  },

  findByDate() {
    wx.navigateTo({
      url: '/pages/receiveDateX/receiveDate?type=transaction',
    })
  }
})