const config = require('../../../config/config.js')
const Http = require('../../../utils/http.js')
const app = getApp();

Page({
  data: {
    recentlyList: [],
    list: [],
    Switch: true,
    todayTimer: '',
    tradeAmt: '',
    money: '',
    flag: true,
    pageNum: 1,
    val: '',
    disabled: false
  },

  recently(formType) {
    Http.getRequest(config.api.tradeDailyList, app.globalData.token, '获取近日解单', {
      pageNum: this.data.pageNum,
      pageSize: 10,
    }, (res) => {
      this.setData({
        pages: res.data.pages,
        recentlyList: formType == 'bottom' ? this.data.recentlyList.concat(res.data.list) : res.data.list
      })
    })
  },
  search() {
    this.setData({
      flag: true
    })
  },
  subList(e) {
    this.setData({
      disabled: true
    })
    wx.showLoading({
      title: '提交中',
    })
    let reg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
    let numbers = e.detail.value.numbers ? e.detail.value.numbers : "";
    if (reg.test(numbers) && numbers <= 1000000) {
      Http.postRequest(config.api.reportDailyVolume, app.globalData.token, '解单中', {
        id: this.data.id,
        reportDate: this.data.todayTimer,
        tradeAmt: numbers * 100,
        dateType: 1,
        tradeCount: e.detail.value.tradeCount ? e.detail.value.tradeCount : 0,
        proof: this.data.receiptUrl ? this.data.receiptUrl : null,
      }, (res) => {
        this.setData({
          disabled: false
        })
        wx.hideLoading();
        if (res.code == 200) {
          wx.redirectTo({
            url: `/pages/main/solution/success/success?money=${numbers * 100}`,
          })
        } else {
          // wx.redirectTo({
          //   url: '/pages/main/solution/fail/fail',
          // })
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 5000
          })
          return
        }
      })
    } else {
      wx.hideLoading();
      if (numbers > 1000000) {
        wx.showToast({
          title: '您输入的解单金额不能大于100万元',
          icon: "none"
        })
      } else {
        wx.showToast({
          title: '金额限制为最多两位小数',
          icon: "none"
        })
      }
      this.setData({
        disabled: false
      })
    }
  },
  uploadImg() {
    let that = this;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: config.api.imgUpload,
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'token': app.globalData.token
          },
          success(res) {
            const data = res.data
            that.setData({
              receiptUrl: JSON.parse(res.data).data.url
            })
          }
        })
      }
    })
  },
  onLoad() {
    this.getList()
  },
  //编辑
  edit(e) {
    let id = e.currentTarget.dataset.id;
    let reportDate = e.currentTarget.dataset.reportdate
    wx.navigateTo({
      url: `/pages/main/solution/detail/index?id=${id}&reportDate=${reportDate}`,
    })
  },
  //获取今日解单
  getList() {
    Http.getRequest(config.api.getVolume, app.globalData.token, '获取解单', {}, (res) => {
      if (res.code == 200) {
        //提交的情况
        this.setData({
          todayTimer: res.data.reportDate,
          tradeAmt: res.data.tradeAmt ? res.data.tradeAmt : false,
          proof: res.data.proof ? res.data.proof : false,
          tradeCount: res.data.tradeCount || res.data.tradeCount == 0 ? res.data.tradeCount : 0,
          Switch: false
        })
      } else {
        let myDate = new Date();
        var year = myDate.getFullYear();
        var month = myDate.getMonth() + 1;
        let day = myDate.getDate();
        let zero = month >= 10 ? '' : 0;
        let zero1 = day >= 10 ? '' : 0;
        this.setData({
          todayTimer: year + '-' + zero + month + '-' + zero1 + day
        })
      }
    })
  },
  //补单
  supplement() {
    wx.navigateTo({
      url: '/pages/main/solution/detail/index?flag=supplement',
    })
  },
  onShow: function (options) {
    let myDate = new Date();
    myDate.getFullYear();
    myDate.getMonth();
    myDate.getDate();
    let date = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate();
    this.setData({
      date: date,
      pageNum: 1
    })
    this.recently();
  },
  onReachBottom() {
    let {
      pageNum,
      pages,
      dateTime,
    } = this.data
    this.setData({
      pageNum: pageNum = ++pageNum
    })
    console.log(pages, pageNum)
    if (pages >= pageNum) {
      wx.showLoading({
        title: '玩命加载中',
      })
      this.recently('bottom')
    } else {
      this.setData({
        maxIf: true,
        pageNum: pageNum = --pageNum
      })
    }
  },
})