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

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 10,
    pages: '',
    finish: false,
    keywords: '',
    placeholder: '搜索商品',
    allSelected: false, //是否全选
    tabIndex: 0,
  },

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

  //列表
  getList(type) {
    let params = {
      // reqChannel: 5,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    let keywords = utils.trimAll(this.data.keywords)
    let url = ''
    if (this.data.tabIndex === 0) {
      url = '/sys/product/findPage'
      if (keywords !== '') {
        params.productName = keywords
      }
      params.mobile = utils.getInfo().mobile
      params.entTaxId=wx.getStorageSync('entTaxId')
    }
    if (this.data.tabIndex === 1) {
      url = '/sys/customer/findPage'
      if (keywords !== '') {
        params.customerName = keywords
      }
      params.mobile = utils.getInfo().mobile
      params.entTaxId=wx.getStorageSync('entTaxId')
    }
    if (this.data.tabIndex === 2) {
      url = '/auth/ent/user/findEntUserRoleInfo'
      if (keywords !== '') {
        params.entUserMobile = keywords
      }
      params.entTaxId=wx.getStorageSync('entTaxId')
      params.mobile = utils.getInfo().mobile
    }
    utils.axios({
      method: 'get',
      url: url,
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      success: 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
          })
        }
        this.isAllselected()
      }
    })
  },
  toFresh() {
    this.setData({
      pageNum: 1,
      pages: '',
      finish: false
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    this.getList('toFresh')
  },

  //点击选中
  select(e) {
    let selected = e.currentTarget.dataset.selected
    let index = e.currentTarget.dataset.index
    let str = `list[${index}].selected`
    if (this.data.tabIndex !== 2) {
      this.setData({
        [str]: !selected
      })
      this.isAllselected()
    } else {
      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
      })
    }
  },
  //点击全选
  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
    })
  },
  //判断是否全选
  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
      })
    }
  },

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

  //切换
  switchTab(e) {
    let index = e.currentTarget.dataset.index
    if (index === this.data.tabIndex) {
      return
    }
    this.setData({
      tabIndex: index
    })
    if (index === 0) {
      this.setData({
        placeholder: '搜索商品'
      })
    }
    if (index === 1) {
      this.setData({
        placeholder: '搜索购方'
      })
    }
    if (index === 2) {
      this.setData({
        placeholder: '搜索员工'
      })
    }
    this.toFresh()
  },

  //新增
  add() {
    let tabIndex = this.data.tabIndex
    if (tabIndex === 0) {
      wx.navigateTo({
        url: '/pages/goodsAdd/goodsAdd?type=0'
      })
    }
    if (tabIndex === 1) {
      wx.navigateTo({
        url: '/pages/buyerAdd/buyerAdd?type=0'
      })
    }
    if (tabIndex === 2) {
      wx.navigateTo({
        url: '/pages/empAdd/empAdd?type=0'
      })
    }
  },

  //编辑(type=0是新增，=1是编辑)
  edit(e) {
    let index = Number(e.currentTarget.dataset.index)
    let list = this.data.list
    if (this.data.tabIndex === 0) {
      let configPro = list[index]
      wx.setStorageSync('configPro', configPro)
      wx.navigateTo({
        url: '/pages/goodsAdd/goodsAdd?type=1'
      })
    }
    if (this.data.tabIndex === 1) {
      let configBuyer = list[index]
      wx.setStorageSync('configBuyer', configBuyer)
      wx.navigateTo({
        url: '/pages/buyerAdd/buyerAdd?type=1'
      })
    }
    if (this.data.tabIndex === 2) {
      // let userMobile = e.currentTarget.dataset.userMobile
      let configUser = list[index]
      wx.setStorageSync('configUser', configUser)
      wx.navigateTo({
        url: `/pages/empAdd/empAdd?type=1`
      })
    }
  },

  //删除
  showDelete() {
    let tabIndex = this.data.tabIndex
    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
    }
    let text = ''
    if (tabIndex === 0) {
      text = '确定要删除选中的商品信息吗？'
    }
    if (tabIndex === 1) {
      text = '确定要删除选中的购方信息吗？'
    }
    if (tabIndex === 2) {
      text = '确定要删除选中的人员方信息吗？'
    }
    wx.showModal({
      content: text,
      confirmColor: '#007dff',
      success: res => {
        if (res.confirm) {
          this.del()
        }
      }
    })
  },
  del() {
    let tabIndex = this.data.tabIndex
    let list = this.data.list
    if (tabIndex === 0) {
      let params = {
        mobile: utils.getInfo().mobile,
        reqChannel: 5,
        delList: []
      }
      for (let i = 0; i < list.length; i++) {
        if (list[i].selected === true) {
          params.delList.push({
            productId: list[i].productId,
            entTaxId:list[i].entTaxId
          })
        }
      }
      utils.axios({
        method: 'post',
        url: '/sys/product/delete',
        data: params,
        sendBefore() {
          wx.showLoading({
            title: '删除中...',
            mask: true
          })
        },
        complete() {
          wx.hideLoading()
        },
        success: res => {
          utils.toast('商品信息删除成功')
          this.toFresh()
        }
      })
    }
    if (tabIndex === 1) {
      let params = {
        mobile: utils.getInfo().mobile,
        reqChannel: 5,
        delList: []
      }
      for (let i = 0; i < list.length; i++) {
        if (list[i].selected === true) {
          params.delList.push({
            customerId: list[i].customerId,
            customerCode: list[i].customerCode,
            entTaxId: list[i].entTaxId
          })
        }
      }
      utils.axios({
        method: 'post',
        url: '/sys/customer/delete',
        data: params,
        sendBefore() {
          wx.showLoading({
            title: '删除中...',
            mask: true
          })
        },
        complete() {
          wx.hideLoading()
        },
        success: res => {
          utils.toast('购方信息删除成功')
          this.toFresh()
        }
      })
    }
    if (tabIndex === 2) {
      let params = {
        entUserMobile:'',
        entTaxId:wx.getStorageSync('entTaxId'),
        reqChannel: 5,
        mobile: utils.getInfo().mobile
      }
      for (let i = 0; i < list.length; i++) {
        if (list[i].selected === true) {
          params.entUserMobile = list[i].userMobile
        }
      }
      utils.axios({
        method: 'post',
        url: '/auth/ent/user/delOnEnt',
        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 () {
    return {
      title: '诺信云',
      path: '/pages/index/index'
    }
  }
})