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

Page({
  data: {
    Switch: true,
    todayTimer: '',
    tradeAmt: '',
    money: '',
    flag: false,
    val: '',
    disabled: false,
    dateMark: 'day',
    dateType: 1
  },
  search() {
    this.setData({
      flag: true
    })
  },
  // 选择时间
  bindDateChange(e) {
    let that = this;
    that.setData({
      date: e.detail.value,
      reportDateX: e.detail.value
    })
    const timeArr = this.data.reportDateX.split('-')
    // 按日解单
    if (this.data.dateMark == 'day' && timeArr.length == 3) {
      const reportDate = timeArr[0] + "-" + timeArr[1] + "-" + timeArr[2]
      this.setData({
        reportDate: reportDate,
        dateType: 1
      })
      // 按月解单
    } else if (this.data.dateMark == 'month') {
      const reportDate = timeArr[0] + "-" + timeArr[1]
      this.setData({
        reportDate: reportDate,
        dateType: 2
      })
    } else if (this.data.dateMark == 'day' && timeArr.length == 2) {
      this.setData({
        reportDate: '',
        dateType: 1
      })
    }
    that.getList(e.detail.value)
  },
  //获取今日解单
  getList(reportDate) {
    Http.getRequest(config.api.getVolume, app.globalData.token, '获取解单', {
      reportDate: reportDate
    }, (res) => {
      if (res.code == 200) {
        //提交的情况
        this.setData({
          Switch: false,
          flag: true
        })
      } else {
        this.setData({
          Switch: true,
          flag: true
        })
      }
    })
  },
  subList(e) {
    this.setData({
      disabled: true
    })
    let reg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
    let tradeAmt = e.detail.value.tradeAmt ? e.detail.value.tradeAmt : "";

    if (reg.test(tradeAmt) && tradeAmt <= 1000000) {
      let flag = this.data.flag;
      let parmers;
      let url
      if (flag) {
        url = config.api.reportDailyVolume
        parmers = {
          reportDate: this.data.reportDate,
          tradeAmt: tradeAmt * 100,
          dateType: this.data.dateType,
          tradeCount: e.detail.value.tradeCount ? parseInt(e.detail.value.tradeCount) : null,
          proof: this.data.receiptUrl ? this.data.receiptUrl : null,
        }
        if (!parmers.reportDate) {
          wx.showToast({
            title: '请选择时间',
            icon: "none"
          })
          this.setData({
            disabled: false
          })
          return;
        }
      } else {
        url = config.api.updateDailyVolume
        parmers = {
          id: this.data.id,
          tradeAmt: tradeAmt * 100,
          tradeCount: e.detail.value.tradeCount ? parseInt(e.detail.value.tradeCount) : null,
          reportDate: this.data.reportDate,
          dateType: this.data.dateType,
          proof: this.data.receiptUrl ? this.data.receiptUrl : null,
        }
      }

      Http.postRequest(url, app.globalData.token, '解单中', parmers, (res) => {
        this.setData({
          disabled: false
        })
        if (res.code == 200) {
          wx.redirectTo({
            url: `/pages/main/solution/success/success?money=${tradeAmt * 100}`,
          })
        } else {
          // wx.redirectTo({
          //   url: '/pages/main/solution/fail/fail',
          // })
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 5000
          })
          return
        }
      })
    } else {
      this.setData({
        disabled: false
      })
      if (tradeAmt > 1000000) {
        wx.showToast({
          title: '您输入的解单金额不能大于100万元',
          icon: "none"
        })
      } else {
        wx.showToast({
          title: '金额限制为最多两位小数',
          icon: "none"
        })
      }
    }
  },

  changeDateType(e) {
    console.log(e.currentTarget.dataset.id);
    this.setData({
      dateMark: e.currentTarget.dataset.id
    })
    if (!this.data.reportDateX) return
    const timeArr = this.data.reportDateX.split('-')
    console.log(timeArr, 'timeArr');
    // 按日解单
    if (this.data.dateMark == 'day' && timeArr.length == 3) {
      const reportDate = timeArr[0] + "-" + timeArr[1] + "-" + timeArr[2]
      this.setData({
        reportDate: reportDate,
        dateType: 1
      })
      // 按月解单
    } else if (this.data.dateMark == 'month') {
      const reportDate = timeArr[0] + "-" + timeArr[1]
      this.setData({
        reportDate: reportDate,
        dateType: 2
      })
    } else if (this.data.dateMark == 'day' && timeArr.length == 2) {
      this.setData({
        reportDate: '',
        dateType: 1
      })
    }
    console.log(this.data.reportDate, 'reportDate');
  },

  onShow() {
    let myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    let day = myDate.getDate();
    let zero = month >= 10 ? '' : 0;
    this.setData({
      nowDateTime: year + '-' + zero + month + '-' + day
    })
  },
  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) {
            if (res.statusCode == 200) {
              that.setData({
                receiptUrl: JSON.parse(res.data).data.url
              })
            } else if (res.statusCode == 413) {
              wx.showToast({
                title: '上传的图片太大，请重新上传',
                icon: "none"
              })
            } else {
              wx.showToast({
                title: '请上传正确的图片',
                icon: "none"
              })
            }
          },
          fail(error) {
            console.log(error);
          }
        })
      }
    })
  },
  onLoad(options) {
    let that = this;
    console.log(options, 'options');
    if (options && options.reportDate) {
      that.setData({
        reportDate: options.reportDate,
        reportDateX: options.reportDate
      })
    }
    if (options && options.id) {
      that.setData({
        id: options.id
      })
      that.wxMerchantTradeDailyfindById(options.id);
    }
    if (options && options.flag) {
      that.setData({
        flag: options.flag
      })
      wx.setNavigationBarTitle({
        title: '补单'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '编辑'
      })
    }
  },
  // //解单详情
  wxMerchantTradeDailyfindById(id) {
    Http.getRequest(config.api.wxMerchantTradeDailyfindById, app.globalData.token, '解单中', {
      id: id
    }, (res) => {
      if (res && res.data) {
        this.setData({
          detail: res.data,
          tradeAmt: res.data.tradeAmt,
          tradeCount: res.data.tradeCount,
        })
      }
    })
  }
})