// index.js
let myPro = require("../../../../utils/wxRequest.js");
let util = require("../../../../utils/util.js");
Page({
    data: {
		imgUrl: getApp().globalData.imgUrl,
		type: '-1', // 订单状态 空全部(-1) 0未支付 1待发货 2待自提 3已完成
		page: 1,
		size: 10,
		dataList: [],
		finished: false,
		loading: false,
		timer: null // 配送计时器
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
		if(options.type){
			that.setData({
				type: options.type
			});
			console.log('type',that.data.type,typeof that.data.type)
		}
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		// 验证登录
		getApp().verifyLogin(function(loginStatus){
			// console.log('登录状态',loginStatus)
			if(loginStatus){
				that.setData({
					page: 1,
					dataList: [],
					finished: false
				});
				that.getDataList();
			};
		});
    },
    onReady: function () {
        // Do something when page ready.
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        // Do something when pull down.
		let that = this;
		that.setData({
			page: 1,
			dataList: [],
			finished: false,
		});
		that.getDataList();
		//停止当前页面下拉刷新
		wx.stopPullDownRefresh();
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
		let that = this;
		if(!that.data.finished){
			that.getDataList();
		};
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 订单列表
	getDataList(){
		let that = this;
		clearInterval(that.data.timer);
		let params = {
			page: that.data.page,
			size: that.data.size
		};
		if(that.data.type != -1){
			params.status = that.data.type
		};
		myPro.wxRequest("user/order/list","GET",params).then(res=>{
			let list = res.result;
			// 无数据
			if(list.length == 0){
				wx.showToast({
					title: "暂无更多数据",
					icon: "none",
				});
				that.setData({
					finished: true
				});
			}else{
				for(let i in list){
					list[i].goodsNums = 0; // 单笔订单的商品总数
					list[i].thumbArr = []; // 单笔订单的商品图片
					for(let j in list[i].order_good){
						let goods = list[i].order_good[j];
						list[i].goodsNums += parseInt(goods.nums);
						list[i].thumbArr.push(goods.good_thumb);
					};
					list[i].thumbArrLength = list[i].thumbArr.length; // 单笔订单的商品图片数量
					// 多于三张截取
					if(list[i].thumbArr.length > 3){
						list[i].thumbArrCustom = list[i].thumbArr.slice(0,3); 
					}else{
						list[i].thumbArrCustom = list[i].thumbArr;
					};
				};
				if(that.data.page == 1){
					that.setData({
						dataList: list
					});
				}else{
					that.setData({
						dataList: that.data.dataList.concat(list)
					});
				}
				that.setData({
					page: that.data.page + 1
				});
				// 开启定时
				if(that.data.type == 1){
					that.newTimer()
				};
			};
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		})
	},
	// 切换tab
	onChange(event){
		console.log(event.detail.name,typeof event.detail.name)
		let that = this;
		that.setData({
			type: event.detail.name,
			page: 1,
			dataList: [],
			finished: false
		});
		that.getDataList();
	},
	// 去详情
	goDetail(event){
		let that = this;
		let id = event.currentTarget.dataset.id;
		wx.navigateTo({
			url: "/pages/user/order/detail/index?order_id="+id
		});
	},
	// 取消订单
	cancleOrder(event){
		let that = this;
		let item = event.currentTarget.dataset.item;
		// 未支付的订单可以取消
		if(item.order_status == 1 && item.pay_status != 1){
			// 取消订单
			myPro.wxRequest("user/v2/order/cancelorder","POST",{order_id: item.id}).then(res=>{
				wx.showToast({
					title: res.msg,
					icon: "none"
				});
				that.setData({
					page: 1,
					dataList: [],
					finished: false
				});
				that.getDataList();
			}).catch(err=>{
				console.log('报错信息',err);
				wx.showToast({
					title: err,
					icon: "none"
				});
			});
		}else{
			// 已支付的订单要联系商家
			wx.showModal({
				title: "可联系商家取消",
				content: "商家电话"+item.store.phone,
				showCancel: false,
				confirmText: "我知道了",
				confirmColor: "#000000"
			});
		}		
	},
	// 收货
	receiveOrder(event){
		let that = this;
		let order_id = event.currentTarget.dataset.id;
		let params = {
			order_id: order_id
		};
		myPro.wxRequest("user/order/commit","POST",params).then(res=>{
			wx.showToast({
				title: res.msg,
				icon: "none"
			});
			that.setData({
				page: 1,
				dataList: [],
				finished: false
			});
			that.getDataList();
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		});
	},
	// 再来一单
	toGoods(){
		let that = this;
		wx.reLaunch({
			url: '/pages/goods/index'
		})
	},
	// 去支付
	goTopay(event){
		let that = this;
		let id = event.currentTarget.dataset.id;
		wx.navigateTo({
			url: '/pages/pay-order/index?order_id='+id
		})
	},
	// 配送计时
	newTimer(){
		let that = this;
		let list = that.data.dataList;
		clearInterval(that.data.timer);
		that.data.timer = setInterval(function() {
			for(let i in list){
				// 当前订单为外卖单且正在配送,开启倒计时
				if(list[i].fare_type == 0 &&　list[i].order_status == 1 && list[i].ship_status){
					let ship_at = list[i].ship_at;
					if(!list[i].isend){
						let shijian = util.daojishi(ship_at,30);
						list[i].count_time = shijian['timeStr']
						list[i].isend = shijian['isend']
					}
				}
			};
			that.setData({
				dataList: list
			})
		},1000)	
	}
});
