const app = getApp();
let self = null;

Page({
  data: {
		tabArr: ['全部','参团中','已团中','未团中'],
		currentTabIndex: 0,
		
		isLogin: false,
		list: null,
		isLoading: false, // 检测是否已经发送请求,防止重复发送请求
		noMore: false, // 检测是否有更多数据,true为没有更多数据,false为还有数据
		pageNum: 1, // 当前页数
		showReward: false,
		prizesArr: [],
		
		typeArr: [{
			key: 'point',
			name: '积分',
		},{
			key: 'grow',
			name: '成长值',
		},{
			key: 'point',
			name: '优惠券',
		},{
			key: 'prestore_name',
			name: '项目服务',
		},{
			key: 'prestore_money',
			name: '预存金额',
		}],
  },
	
  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,
				};
				
				// 默认获取'全部'
				this.getData(true, '/api/weshop/prom/luckyOrder/page', data)
				.then(()=>{
					this.setData({
						currentData: data,
					});
				});
				
			};
		};
  },
	
	
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		this.scrollToLower('/api/weshop/prom/luckyOrder/page', 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选项卡
	clickTab(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: {
				delete currentData.team_status;
				break;
			};
			case 1: {
				currentData.team_status = 0;
				break;
			};
			case 2: {
				currentData.team_status = 2;
				break;
			};
			case 3: {
				currentData.team_status = 1;
				break;
			};
		};
		
		// 请求
		this.getData(true, '/api/weshop/prom/luckyOrder/page', currentData);
		
	},
	
	
	/**
	 * 请求数据
	 */
	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);
		};
	},
	
	
	//跳转
	go: function (e) {
	  var url = e.currentTarget.dataset.url;
	  getApp().goto(url);
	},
	
	//查看奖励
	showReward: function(e) {
		this.setData({
			currentItemIndex: e.target.dataset.index,
			currentItemAid: e.target.dataset.aid,
		});
		this.getPrizes(this.data.currentItemIndex);
	},
	
	//关闭奖励
	closeReward: function() {
		this.setData({
			showReward: false,
		});
	},
	
	
	// 获取优惠券奖励和服务项目奖励
	getPrizes: function(currentItemIndex) {
		let currentItem = this.data.list.pageData[currentItemIndex];
		let type = currentItem.type;
		
		// 分割type字符串
		let typeArr = this.splitStringToArr(type);
		let prizesArr = [];
		let promiseArr = [];
		typeArr.forEach(function(item) {
			
			let obj = {};
			obj.type = item;
			
			let promise = new Promise((resolve, reject) => {
				switch(item) {
					case 1: { //积分
						obj.name = '积分';
						obj.num = 'point'; 
						prizesArr.push(obj);
						resolve();
						break;
					};
					case 2: { //成长值
						obj.name = '成长值';
						obj.num = 'grow';
						prizesArr.push(obj);
						resolve();
						break;
					};
					case 3: { //优惠券
						app.request.promiseGet('/api/weshop/prom/luckyCoupon/page', {
							data: {
								store_id: app.globalData.setting.stoid,
								aid: self.data.currentItemAid,
							}
						}).then(res => {
							if(res.data.code == 0) {
								obj.couponList = res.data.data.pageData;
								prizesArr.push(obj);
								resolve();
							};
						});
						break;
					};
					case 4: { //服务项目
						app.request.promiseGet('/api/weshop/prom/luckyService/page', {
							data: {
								store_id: app.globalData.setting.stoid,
								aid: self.data.currentItemAid,
							}
						}).then(res => {
							if(res.data.code == 0) {
								obj.serviceList = res.data.data.pageData;
								prizesArr.push(obj);
								resolve();
							};
						});
						break;
					};
					case 5: { //预存金额
						obj.name = 'prestore_name';
						obj.num = 'prestore_money';
						prizesArr.push(obj);
						resolve();
						break;
					};
				};
			});
			
			promiseArr.push(promise);
			
			
			Promise.all(promiseArr).then(()=> {
				self.setData({
					prizesArr,
					showReward: true,
				});
			});
						
		});
	},
	
	// 将type字符串分割成数组
	splitStringToArr: function(typeStr) {
		var index;
		var newTypeArr = [];
		var typeStrArr = typeStr.split(',');
		typeStrArr.forEach(function(item) {
			index = parseInt(item);
			newTypeArr.push(index);
		});
		return newTypeArr;
	},
	
})

