// monitor/pages/myMaterial/myMaterial.js
var Api = require("../../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 1,
    keyWords: '',
    listData: [],
    pageNum:1,
		pageSize:10,    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },
  // 点击切换工单
  swichNav: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.current,
      pageNum:1,
      listData: []
    })
    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
    }
    let url = ''
    if(this.data.currentTab == '1'){
      url = 'wl/materialEngineer/engineerStockList'
    }else{
      url = 'blade-operationOrderMaterial/operationOrderMaterial/getEmpUseFittingList'
    }
    wx.showLoading({
      title: '加载中...',
    })   
    Api.req(url, data, 'get').then(res => {
      wx.stopPullDownRefresh()
      wx.hideLoading()
      if(res.code== 200){
        let resData = res.data.records
        this.data.loadEnd = resData.length < this.data.pageSize
        if (this.data.pageNum == 1) {
          this.data.listData = resData
        } else {
          this.data.listData = this.data.listData.concat(resData)
        }
        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'
        })
      }
    })
  },
  //添加
  bindAdd(){
    wx.navigateTo({
      url: '../addMaterial/addMaterial?title=添加',
    })
  }, 
  //跳转详情
  goDetail(e){  
    var id = e.currentTarget.dataset.id 
    var materialId = e.currentTarget.dataset.mid    
    var title = ''
    if(this.data.currentTab == '1'){
      title = '可用物料'
    }else if(this.data.currentTab == '2'){
      title = '已用物料'
    }
    wx.navigateTo({
      url: '../materialDetail/materialDetail?title='+ title +'&materialId='+ materialId +'&id=' + id,
    })
  },
  

  /** 下拉刷新 **/
  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
      })
    }
  },
})