// pages/shopOrder/index.js
let myPro = require("../../../utils/wxRequest.js");
let util = require("../../../utils/util.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
		imgUrl: getApp().globalData.imgUrl,
		active: null, // null全部 0未支付 1待发货 2待自提 3已完成
		orderList: [],
		page: 1,
		size: 10,
		finished: false,
		loading: false // 全局loading
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		var that = this
		if(options.status != null){
			that.setData({
				active: parseInt(options.status),
			});
		};
		that.getOrderList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
		let that = this;
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
		// console.log('上拉触底事件')
		let that = this;
		if(!that.data.finished){
			that.getOrderList()
		};
    },
	// 获取商家订单列表
	getOrderList(){
		let that = this
		let params = {
			page: that.data.page,
			size: that.data.size
		};
		if(that.data.active != -1){
			params.status = that.data.active
		};
		myPro.wxRequest("store/order/list","GET",params).then(res=>{
			let tempList = res.result;
			let list = that.data.orderList;
			// 无数据
			if(tempList.length == 0){
				wx.showToast({
					title: "暂无更多数据",
					icon: "none"
				});
				that.setData({
					finished: true
				});
			}else{
				// 有数据
				for(let i in tempList){
					// 订单状态
					if(tempList[i].order_status == 0){
						tempList[i].statusTitle = '已关闭'
					}else if(tempList[i].order_status == 2){
						tempList[i].statusTitle = '已完成'
					}else if(tempList[i].order_status == 1){
						// 正常订单
						if(tempList[i].pay_status == 0){
							tempList[i].statusTitle = '待支付'
						}else if(tempList[i].pay_status == 1){
							if(tempList[i].ship_status == 0){
								// fare_type 0邮寄 1自提
								if(tempList[i].fare_type == 0){
									tempList[i].statusTitle = '待发货'
								}else if(tempList[i].fare_type == 1){
									tempList[i].statusTitle = '待自提'
								}
							}
						}else if(tempList[i].pay_status == 2){
							// fare_type 0邮寄 1自提
							if(tempList[i].fare_type == 0){
								tempList[i].statusTitle = '已发货'
							}else if(tempList[i].fare_type == 1){
								tempList[i].statusTitle = '已自提'
							}
						}
					}
					// 订单的商品总数
					let order_goods = tempList[i].order_good
					let nums = 0
					for(let j in order_goods){
						nums = nums + parseInt(order_goods[j].nums)
					}
					tempList[i].goods_total_nums = nums // 商品总数,也应该接口返回(现在没有,就先自己总)
				}
				list = list.concat(tempList);
				// 计算总价 (不计算了,直接拿pay_price,这里还有优惠券)
				// for(let i in list){
				// 	list[i].totalPrice = parseFloat(list[i].price) + parseFloat(list[i].delivery_fee);
				// 	list[i].totalMemberPrice = parseFloat(list[i].member_price) + parseFloat(list[i].delivery_fee)
				// };
				that.setData({
					orderList: list,
					page: that.data.page + 1
				});
			}
		}).catch(err=>{
			// that.setData({
			// 	loading: false
			// });
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		});
	},
	// 切换列表
	onClickTab(event){
		// console.log('event=====',event);
		let that = this;
		that.setData({
			active: event.detail.name,
			page: 1,
			orderList: []
		})
		that.getOrderList();
	},
	// 处理订单(发货)
	toHandle(event){
		let that = this;
		// console.log(event)
		let order = event.currentTarget.dataset.order;
		let params = {
			order_id: order.id
		};
		myPro.wxRequest("store/order/ship","POST",params).then(res=>{
			wx.showToast({
				title: res.msg,
				icon: "none"
			});
			that.setData({
				page: 1,
				orderList: [],
				finished: false
			});
			that.getOrderList();
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		});
	},
	// 处理订单(取消)
	cancleOrder(event){
		let that = this;
		let order = event.currentTarget.dataset.order;
		let params = {
			order_id: order.id
		};
		myPro.wxRequest("store/tx/cancelOrder","POST",params).then(res=>{
			wx.showToast({
				title: res.msg,
				icon: "none"
			});
			that.setData({
				page: 1,
				orderList: [],
				finished: false
			});
			that.getOrderList();
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		});
	}
})