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

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 10,
    pages: '',
    finish: false,
    keywords: '',
  },

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

  //列表
  getList(type) {
    let params = {
      currentUserMobile: utils.getInfo().mobile,
      reqChannel: 5,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    let keywords = utils.trimAll(this.data.keywords)
    if (keywords !== '') {
      params.name = keywords
    }
    utils.axios({
      method: 'get',
      url: '/auth/comm/user/findChildUsers',
      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')
  },

  //点击选中
  select(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.list
    for (let i = 0; i < list.length; i++) {
      if (i !== index) {
        list[i].selected = false
      }
    }
    list[index].selected = !list[index].selected
    this.setData({
      list
    })
  },

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

  //新增
  add() {
    wx.navigateTo({
      url: '/pages/ispPeople/add?type=0'
    })
  },

  //编辑(type=0是新增，=1是编辑)
  edit(e) {
    let mobile = e.currentTarget.dataset.mobile
    wx.navigateTo({
      url: `/pages/ispPeople/add?type=1&&mobile=${mobile}`
    })
  },

  //删除
  showDelete() {
    let delList = 0
    for (let i = 0; i < this.data.list.length; i++) {
      if (this.data.list[i].selected === true) {
        delList++
      }
    }
    if (delList <= 0) {
      utils.toast('请选择要删除的条目')
      return
    }
    wx.showModal({
      content: '确定要删除选中的人员方信息吗？',
      confirmColor: '#007dff',
      success: res => {
        if (res.confirm) {
          this.del()
        }
      }
    })
  },
  del() {
    let list = this.data.list
    let params = {
      currentUserMobile: utils.getInfo().mobile,
      reqChannel: 5,
      mobile: ''
    }
    for (let i = 0; i < list.length; i++) {
      if (list[i].selected === true) {
        params.mobile = list[i].mobile
      }
    }
    utils.axios({
      method: 'post',
      url: '/auth/isp/user/delete',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '删除中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        utils.toast('人员信息删除成功')
        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 () {

  }
})