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

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 10,
    pages: '',
    finish: false,
    typeList: ['用户管理', '客户管理', '商品管理'], //类型
    typeIndex: '', //类型下标
    startTime: '', //开始时间
    endTime: '', //结束时间
    operaterName: '', //操作人员姓名
  },

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

  //列表
  getList(type) {
    let params = {
      mobile: utils.getInfo().mobile,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    let operaterName = utils.trimAll(this.data.operaterName)
    if (operaterName !== '') {
      params.operaterName = operaterName
    }
    if (this.data.startTime !== '') {
      params.createBeginTime = this.data.startTime + ' 00:00:00'
    }
    if (this.data.endTime !== '') {
      params.createEndTime = this.data.endTime + ' 23:59:59'
    }
    if (this.data.typeIndex !== '') {
      params.opType = this.data.typeList[this.data.typeIndex]
    }
    utils.axios({
      method: 'get',
      url: '/auth/log/findPage',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        let result = res.data
        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
          })
        }
      }
    })
  },
  toFresh() {
    this.setData({
      pageNum: 1,
      pages: '',
      finish: false
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    this.getList('toFresh')
  },

  //搜素
  search(e) {
    let operaterName = e.detail.value
    this.setData({
      operaterName
    })
    this.toFresh()
  },
  //类型
  setType(e) {
    let typeIndex = Number(e.detail.value)
    this.setData({
      typeIndex
    })
    this.toFresh()
  },
  clearType() {
    this.setData({
      typeIndex: ''
    })
    this.toFresh()
  },
  //设置开始结束时间
  setStart(e) {
    let startTime = e.detail.value
    this.setData({
      startTime
    })
    this.toFresh()
  },
  setEnd(e) {
    let endTime = e.detail.value
    this.setData({
      endTime
    })
    this.toFresh()
  },
  clearStart() {
    this.setData({
      startTime: ''
    })
    this.toFresh()
  },
  clearEnd() {
    this.setData({
      endTime: ''
    })
    this.toFresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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'
    }
  }
})