const app = getApp();
let self = null;
// 防抖函数用定时器
let timer;

Page({
  data: {
		tabArr: ['待核销','已核销'],
		currentTabIndex: 0,
		
		isLogin: false,
		list: {
			pageData: [],
		},
		isLoading: false, // 检测是否已经发送请求,防止重复发送请求
		noMore: false, // 检测是否有更多数据,true为没有更多数据,false为还有数据
		pageNum: 1, // 当前页数
  },
	
  onLoad: function () {
		self = this;
		app.isLogin().then(function(data) {
			self.setData({
				userInfo: data,
			});
		});
  },
	
  onShow: function() {
		if(app.globalData.userInfo) {
			if(!this.data.isLogin) {
				this.setData({
					userInfo: app.globalData.userInfo,
					imghost: app.globalData.setting.imghost,
					isLogin: true,
				});
			};
			
			let data = {
				store_id: app.globalData.setting.stoid,
				user_id: this.data.userInfo.user_id,
				IsPos: 0,
			};
			
			// 默认获取'待核销'
			this.getData(true, '/api/weshop/serviceCard/pageWriteoff', data)
			.then(()=>{
				this.setData({
					currentData: data,
				});
			});
		};
  },
	
	
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		this.scrollToLower('/api/weshop/serviceCard/pageWriteoff', this.data.currentData);
	},
	
	
	// 图片异常时加载默认图片
	// bind_bnerr: function (e) {
	//   var _errImg = e.target.dataset.errorimg;
	//   var val = e.target.dataset.val;
	//   if(val!=undefined && val!=null && val!='null' ){
	//     var _errObj = {};
	//     _errObj[_errImg] = "/public/images/default_goods_image_240.gif";
	//     this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
	//   }
	// },
	
	

	// 点击tab选项卡
	handleTab(e) {
		this.setData({
			currentTabIndex: e.target.dataset.index,
			noMore: false,
			pageNum: 1,
			list: null,
		});
		
		let currentTabIndex = parseInt(this.data.currentTabIndex);
		let currentData = this.data.currentData;
		
		delete currentData.page;
		
		switch(currentTabIndex) {
			case 0: {
				currentData.IsPos = 0;
				break;
			};
			case 1: {
				currentData.IsPos = 1;
				break;
			};
		};
		
		// 请求
		this.getData(true, '/api/weshop/serviceCard/pageWriteoff', currentData);
		
	},
	
	clickTab(e) {
		// 判断会员状态
		let user_info = getApp().globalData.userInfo;
		if (user_info == null || user_info.mobile == undefined || user_info.mobile == '' || user_info.mobile == null) {
			wx.navigateTo({
					url: '/packageE/pages/togoin/togoin',
			})
			return false;
		};
		this.debounce(this.handleTab.bind(this, e), 200)();
	},
	
	
	/**
	 * 请求数据
	 */
	getData: function(isInit, url, data) {
		let p = app.request.promiseGet(url, {
			data: data,
			isShowLoading: true,
		})
		.then(function(res) {
			if(res.data.code == 0) {
				
				self.setData({
					isLoading: false
				});
				
				if(isInit) {// 第一次加载
					self.setData({
						list: res.data.data
					});
				} else {
					self.setData({
						'list.pageData': self.data.list.pageData.concat(res.data.data.pageData)
					});
				};
				
				if((res.data.data.pageData.length == 0) || (res.data.data.pageSize * res.data.data.page >= res.data.data.total)) {
					self.setData({
						noMore: true
					});
				};
				
			} else {
				self.setData({
					'list.pageData': []
				});
			};
			
		});
	
		return p;
	},
	
	
	/**
	 * 上拉加载
	 */
	scrollToLower(url, requestData) {
		// 数据总量
		let total = this.data.list.total;
		// 单页最大数据量
		let pageSize = this.data.list.pageSize;
		// 如果数据总量不为0且小于或等于单页最大数据量，说明数据已全部加载，显示‘没有更多了’
		if((total != 0)&&(total <= pageSize)) {
			this.setData({
				noMore: true
			});
		};
		
		if(!this.data.isLoading && !this.data.noMore) {
			this.setData({
				isLoading: true,
				pageNum: this.data.pageNum + 1
			});
			requestData.page = this.data.pageNum;
			this.getData(false, url, requestData);
		};
	},
	

	
	viewDetails(e) {
		let formlistid = e.currentTarget.dataset.formlistid;
		let title = e.currentTarget.dataset.title;
		let date = e.currentTarget.dataset.date; //有效截止日期
		let use_state = this.data.currentTabIndex; //0待核销 1已核销
		let url = `/packageB/pages/boxes-details/boxes-details?formlistid=${formlistid}&title=${title}&usestate=${use_state}&date=${date}`;

		app.goto(url);
	},
	
	
	// 函数防抖
	debounce(func, wait) { 
		return () => {
			clearTimeout(timer);
			timer = setTimeout(func, wait);
		};
	},
	
	
})

