// pages/ticketRed/ticketRed.js
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 10,
    pages: '',
    finish: false,
    invoiceNumber: '', //购方名称
    hongchongFlag: 2, //是否可红冲
    allSelected: false, //是否全选
    tabIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },

  //列表
  getList(type) {
    let params = {
      mobile: utils.getInfo().mobile,
      reqChannel: 5,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    let invoiceNumber = utils.trimAll(this.data.invoiceNumber)
    if (invoiceNumber !== '') {
      params.invoiceNumber = invoiceNumber
    }
    let url = ''
    if (this.data.tabIndex === 0) {
      url = '/sys/invoice/findPage'
      params.hongchongFlag = this.data.hongchongFlag
    }
    if (this.data.tabIndex === 1) {
      url = '/sys/hongchongInfo/findPage'
    }
    utils.axios({
      method: 'get',
      url: url,
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        console.log('红冲信息');
        console.log(res);
        let result = res.data
        if (this.data.tabIndex === 0) {
          for (let i = 0; i < result.records.length; i++) {
            result.records[i].selected = false
          }
        }
        if (type === 'toFresh') {
          this.setData({
            list: result.records,
            pages: result.pages
          })
        } else {
          let list = this.data.list
          list = list.concat(result.records)
          this.setData({
            list,
            pages: result.pages
          })
        }
        if (result.total <= this.data.pageSize) {
          this.setData({
            finish: true
          })
        }
        if (this.data.tabIndex === 0) {
          this.isAllselected()
        }
      }
    })
  },
  toFresh() {
    this.setData({
      pageNum: 1,
      pages: '',
      finish: false
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    this.getList('toFresh')
  },

  //新增红冲
  toAddred(e) {
    let index = e.currentTarget.dataset.index
    let item = this.data.list[index]
    wx.setStorageSync('ticketRedData', item)
    wx.navigateTo({
      url: '/pages/ticketRed/detail'
    })
  },

  //开票详情
  toDetail(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.list
    let item = list[index]
    wx.setStorageSync('ticketDetail', item)
    wx.navigateTo({
      url: '/pages/ticketRed/redDetail'
    })
  },

  //刷新
  refresh(e) {
    let index = e.currentTarget.dataset.index
    let invoiceReqFlowNo = e.currentTarget.dataset.invoiceno
    let params = {
      invoiceReqFlowNo,
      mobile: utils.getInfo().mobile,
      reqChannel: 5
    }
    utils.axios({
      method: 'post',
      url: '/sys/hongchongInfo/refreshResult',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '刷新中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        utils.toast('已刷新')
        let result = res.data
        let str = `list[${index}].status`
        this.setData({
          [str]: result.status
        })
      }
    })
  },

  //点击重开
  showRetry(e) {
    wx.showModal({
      content: '确定重开该发票吗？',
      confirmColor: '#007dff',
      success: res => {
        if (res.confirm) {
          this.retry(e)
        }
      }
    })
  },
  retry(e) {
    let index = e.currentTarget.dataset.index
    let invoiceReqFlowNo = e.currentTarget.dataset.invoiceno
    let params = {
      invoiceReqFlowNo,
      mobile: utils.getInfo().mobile,
      reqChannel: 5
    }
    utils.axios({
      method: 'post',
      url: '/sys/hongchongInfo/retry',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '推送中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        utils.toast('已推送')
        let result = res.data
        let str = `list[${index}].status`
        this.setData({
          [str]: result.status
        })
      }
    })
  },

  //点击选中
  select(e) {
    let selected = e.currentTarget.dataset.selected
    let index = e.currentTarget.dataset.index
    let str = `list[${index}].selected`
    this.setData({
      [str]: !selected
    })
    this.isAllselected()
  },

  //点击全选
  selectedAll() {
    let allSelected = !this.data.allSelected
    let list = this.data.list
    this.setData({
      allSelected
    })
    if (allSelected) {
      for (let i = 0; i < list.length; i++) {
        list[i].selected = true
      }
    } else {
      for (let i = 0; i < list.length; i++) {
        list[i].selected = false
      }
    }
    this.setData({
      list
    })
  },

  //搜素
  search(e) {
    let invoiceNumber = e.detail.value
    this.setData({
      invoiceNumber
    })
    this.toFresh()
  },

  //切换
  switchTab(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      tabIndex: index
    })
    this.toFresh()
  },

  //判断是否全选
  isAllselected() {
    let list = this.data.list
    let sList = 0
    for (let i = 0; i < list.length; i++) {
      if (list[i].selected) {
        sList++
      }
    }
    if (list.length === sList) {
      this.setData({
        allSelected: true
      })
    } else {
      this.setData({
        allSelected: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.toFresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNum = this.data.pageNum
    let pages = this.data.pages
    pageNum++
    if (pageNum <= pages) {
      this.setData({
        pageNum
      })
      this.getList()
    } else {
      this.setData({
        finish: true
      })
      return
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '诺信云',
      path: '/pages/index/index'
    }
  }
})