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

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    tabList: [{
      name: '全部',
      siteStatus: '0,1,2,3,4,5,7,8,9',
      num: 0
    }, {
      name: '待审核',
      siteStatus: '0,1,2',
      num: 0
    }, {
      name: '待出库',
      siteStatus: '8',
      num: 0
    }, {
      name: '待领取',
      siteStatus: '9',
      num: 0
    }, {
      name: '已完成',
      siteStatus: '5',
      num: 0
    }],
    listData: [],
    params:{		
			startDate:'',
      endDate:'',
			pageNum:1,
			pageSize:10
    },    
    //时间查询    
		isPickerShow: false,
		pickerConfig: {
		  endDate: true,
		}
  },

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

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      [`params.pageNum`]:1,
      listData: []
    })
    this.getList()
    this.getNum()
  },
  // 点击切换工单
  swichNav: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.current,
      [`params.pageNum`]:1,
      listData: []
    })
    this.getList()
    this.getNum()
  }, 
  // 列表
  getList: function () {
    var that = this
    var data = {
      current: this.data.params.pageNum,
      size: this.data.params.pageSize,
      createTimeSta:this.data.params.startDate,
      createTimeEnd:this.data.params.endDate,
      status: this.data.tabList[this.data.currentTab].siteStatus,
    }
    wx.showLoading({
      title: '加载中...',
    })   
    Api.req('blade-platformFittingApply/platformFittingApply/page', data, 'get').then(res => {
      wx.stopPullDownRefresh()
      wx.hideLoading()
      if(res.code== 200){
        this.data.loadEnd = res.data.records.length < this.data.params.pageSize
        if (this.data.params.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.params.pageNum++;
        }
        this.setData({
          listData: this.data.listData,
          loadEnd: this.data.loadEnd,
        })
      }else{
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    })
  },
  getNum: function () {
    var that = this;
    var data = {}    
    Api.req('blade-platformFittingApply/platformFittingApply/findHomeFittingCount', data, 'get')
      .then(res => {
        if(res.code == 200){
          that.setData({
            [`tabList[0].num`]: res.data.allCount, //全部
            [`tabList[1].num`]: res.data.dshCount, //待审核
            [`tabList[2].num`]: res.data.dckCount, //待出库
            [`tabList[3].num`]: res.data.dlqCount, //	待领取 
            [`tabList[4].num`]: res.data.endCount, //	已完成 
          })
        }
      })

  },
  //添加
  bindAdd(){
    wx.navigateTo({
      url: '../addMaterial/addMaterial?title=添加',
    })
  },
  //编辑
  editList(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "../addMaterial/addMaterial?id="+ id +"&title=编辑",
    })
  },

  //跳转详情
  goDetail(e){   
    var id = e.currentTarget.dataset.id    
    wx.navigateTo({
      url: "../addMaterial/addMaterial?id="+ id +"&title=详情",
    })
  },
  //领取
  getReceive(e){
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '是否确认领取该物料？',
      complete: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中...',
          })   
          Api.req('blade-platformFittingApply/platformFittingApply/empReceiveMate', {
            id: id
          }, 'get').then(res => {
              wx.hideLoading()
              if(res.code == 200){
                wx.showToast({
                  title: '操作成功',
                })
                setTimeout(()=>{
                  this.setData({
                    ['params.pageNum']: 1, 
                    listData: [],
                  });
                  that.getNum()
                  that.getList();
                },1500)
              }              
            })
        }
      }
    })
  },
  //删除物料
  delList(e){
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '是否确认删除该物料？',
      complete: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中...',
          })   
          Api.req('blade-operationOrderMaterial/operationOrderMaterial/deleteApplyFitting', {
            id: id
          }, 'get').then(res => {
              wx.hideLoading()
              if(res.code == 200){
                wx.showToast({
                  title: '删除成功',
                })
                setTimeout(()=>{
                  this.setData({
                    ['params.pageNum']: 1, 
                    listData: [],
                  });
                  that.getNum();
                  that.getList();
                },500)
              }              
            })
        }
      }
    })
  },
  //隐藏时间选择
  pickerHide: function() {
		this.setData({
		  isPickerShow: false
		});
  },
  //设置时间
  setPickerTime: function(val) {
		let data = val.detail, params = this.data.params;
		params.pageNum = 1;
		params.startDate = data.startTime.substring(0,data.startTime.indexOf(' '));
		params.endDate = data.endTime.substring(0,data.endTime.indexOf(' '));
		this.setData({
      params:params,
      applyTime: params.startDate + '~' + params.endDate
    });	
    this.getList()
  },
  /** 点击申请时间 **/
	pickerShow: function() {
		let now = new Date(),
			year = now.getFullYear(),
			month = now.getMonth() + 1,
			day = now.getDate(),
			initStartTime = year+ '-' + month + '-' + day;
		let pickerConfig = this.data.pickerConfig, params = this.data.params;
		pickerConfig.initStartTime = params.startDate != '' ?  params.startDate : initStartTime;
		pickerConfig.initEndTime = params.endDate != '' ? params.endDate : initStartTime;
		this.setData({
		  pickerConfig:pickerConfig,
		  isPickerShow: true
		});
	},

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