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

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

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

  //搜索
  getList(type) {
    let params = {
      mobile: utils.getInfo().mobile,
      reqChannel: 5,
      entTaxId:wx.getStorageSync('entTaxId'),
    
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    if (utils.trimAll(this.data.keywords) !== '') {
      params.productName = utils.trimAll(this.data.keywords)
    }
    utils.axios({
      method: 'get',
      url: '/sys/product/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
          if(result){
            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')
  },

  //获取用户输入的内容
  getKeywords(e) {
    let value = e.detail.value
    this.setData({
      keywords: value
    })
  },

  //点击某个项目
  select(e) {
    let list = this.data.list
    let index = e.currentTarget.dataset.index
    //上一页是第几个元素点进来的
    let prevIndex = this.data.index
    utils.funPrev(prev => {
      list[index].noRatePrice = ''
      list[index].noRateTotal = ''
      list[index].totalMoney = ''
      list[index].totalRate = ''
      list[index].quantity = ''
      list[index].spreadFlag = false
      list[index].taxRateArr = [list[index].taxRate]
      list[index].taxRateArrIndex = 0
      prev.getTaxRate(prevIndex,list[index].taxationCateCode,list[index].taxRate)
      prev.setData({
        [`proList[${prevIndex}]`]: list[index]
      })
      prev.calcMoney()
      wx.navigateBack()
    })
  },

  //新增商品
  toGoodsAdd() {
    wx.navigateTo({
      url: '/pages/goodsAdd/goodsAdd?type=0'
    })
  },

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