const config = require('../../config/config.js')
const Http = require('../../utils/http.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: ''
  },
  //进到记录页面初始化显示的数据
  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 &&res.data.list.list.length &&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')
              dataList.push(file)
            })
            
          }
          
          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 &&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')
              dataList.push(file)
            })
          }
          
          this.setData({
            list: dataList,
            hide: res.data.list.list == 0 ? true : false
          })
          resolve(res)
        })
      })
    }
  },
  onLoad(e) {
    let {
      page,
      getFullYear,
      getMonth,
      getDate,
      dateTime
    } = this.data
    let nowTime = getFullYear + '-' + getMonth + '-' + getDate
    this.setData({
      dateTime: nowTime,
      nowDateTime: nowTime
    })
    this.getList(e.type, page, nowTime)
    if (e.type == '0') {  // 交易流水
      this.setData({
        flag: true,
        text: '未核销',
        defaultId: e.type,
        name: '购买用户',
        timeName: '购买时间'
      })
      tt.setNavigationBarTitle({
        title: '交易记录',
      })
    } 
    else {             // 核销
      this.setData({
        flag: false,
        text: '已核销',
        defaultId: e.type,
        name: '核销员',
        timeName: '核销时间'
      })
      tt.setNavigationBarTitle({
        title: '核销记录',
      })
    }
  },
  onReachBottom() {
    let {
      page,
      defaultId,
      dateTime,
    } = this.data
    var pageInd = '';
    tt.showLoading({
      title: '玩命加载中',
    })
    this.setData({
      page: page = ++page
    })

    this.getList(defaultId, page, dateTime).then(res => {
      if (res.code == 200) {
        tt.hideLoading();
      }
    })
  },
  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) {
        // 退券退款申请成功
        tt.showToast({
          title: res.message,
          image:'../../static/images/success.png'
        })
        setTimeout(() => {
          tt.switchTab({
            url: '/pages/main/main'
          })
        }, 1000)
      } else {
        tt.showModal({
          title: '错误',
          content: res.message,
          showCancel:false
        })
      }
    }, (err) =>{
      console.log("err", err)
      tt.showModal({
        title: '错误',
        content: err.message,
        showCancel: false
      })
    })
  },
  //往回传用户选择的日期渲染不同的数据
  bindDateChange(e) {
    let {
      page,
      defaultId
    } = this.data
    this.setData({
      dateTime: e.detail.value,
      dataList: [],
      list:[]
    })
    page = 1
    this.setData({
      page: page
    })
    this.getList(defaultId, page, e.detail.value)
  }
})