// monitor/pages/materialList/materialList.js
var Api = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyWords: '',
    listData: [],
    pageNum:1,
		pageSize:10,
  },

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

  /** 搜索 **/
  keyWordsSearch(res) { 
    this.setData({
      keyWords: res.detail, //搜索内容 
    })   
    this.getList()
  },
  // 列表
  getList: function () {
    var that = this
    var data = {
      current: this.data.pageNum,
      size: this.data.pageSize,
      engineerId: wx.getStorageSync('userId'),
      smallConditon: this.data.keyWords
    }
    wx.showLoading({
      title: '加载中...',
    })   
    Api.req('wl/materialEngineer/engineerStockList', data, 'get').then(res => {
      wx.stopPullDownRefresh()
      wx.hideLoading()
      if(res.code== 200){
        res.data.records.forEach(element => {
          element.isSelect = false
        });
        this.data.loadEnd = res.data.records.length < this.data.pageSize
        if (this.data.pageNum == 1) {
          this.data.listData = res.data.records
        } else {
          this.data.listData = this.data.listData.concat(res.data.records)
        }
        if (!this.data.loadEnd) {
          this.data.pageNum++;
        }
        this.setData({
          listData: this.data.listData,
          loadEnd: this.data.loadEnd,
        })
      }else{
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    })
  },

  //选择
  bindchange(e) {
    this.setData({
      [`listData[${e.currentTarget.dataset.index}].isSelect`]: e.currentTarget.dataset.is ? false : true,
    })
  },


  //提交
  submitForm(e) {
    var dataList = [];
    this.data.listData.forEach((item)=>{
      if(item.isSelect){
        dataList.push({
          materialId: item.materialId, //物料ID
          materialName: item.name, //物料名称
          materialCode: item.code, //物料代码
          materialSpec: item.power, //物料规格
          materialType: item.model,  //	物料型号
          materialStock: item.stock, //库存
          userAmount: 1, //数量
        })
      }
    })
    if (dataList.length <= 0) {
      wx.showToast({
        title: "请选择一条数据",
        icon: 'none'
      })
      return
    }else{
      var page = getCurrentPages();
      let prevPage = page[page.length - 2];
      prevPage.setData({
      　　// 要设置的值
        jgList: dataList
      })
      setTimeout(function() {
        prevPage.addJgList();
        wx.navigateBack({})
      }, 500)
    }
  },

  /** 下拉刷新 **/
  onPullDownRefresh(e) {
    this.setData({
      pageNum: 1,
      pageSize: 10,
      loadEnd: false,
    })
    this.getList()     
  },
  /** 页面触底上拉刷新 **/
  onReachBottom(e) {
    if (!this.data.loadEnd) {       
      this.getList()
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 500
      })
    }
  },
})