// index.js
let myPro = require("../../../../utils/wxRequest.js");
let util = require("../../../../utils/util.js");
Page({
    data: {
		imgUrl: getApp().globalData.imgUrl,
		loading: false,
		orderInfo: null,
		timer: null, // 配送倒计时
		count_time: ''
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
		that.setData({
			order_id: options.order_id
		});
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		// 获取订单信息
		that.getOrderDetail();
    },
    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.
		
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 获取订单信息
	getOrderDetail(){
		let that = this;
		let params = {
			order_id: that.data.order_id	
		};
		myPro.wxRequest("user/v2/order/detail","GET",params).then(res=>{
			that.setData({
				orderInfo: res.result
			});
			// console.log('订单状态',that.data.orderInfo.order_status)
			let orderInfo = res.result;
			// 当前订单为外卖单且正在配送,开启倒计时
			if(orderInfo.fare_type == 0 && orderInfo.order_status == 1 &&　orderInfo.ship_status == 1){
				clearInterval(that.data.timer);
				that.data.timer = setInterval(function(){
					let ship_at = orderInfo.ship_at;
					let shijian = util.daojishi(ship_at,30);
					if(shijian['isend']){
						clearInterval(that.data.timer);
					}else{
						that.setData({
							count_time: shijian['timeStr']
						});
					}
				},1000)
			};
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		})
	},
	// 再来一单
	toGoods(){
		let that = this;
		wx.reLaunch({
			url: '/pages/goods/index'
		})
	},
	// 取消订单
	cancleOrder(){
		let that = this;
		let store = that.data.orderInfo.store;
		let orderInfo = that.data.orderInfo;
		// 未支付的订单可以取消
		if(orderInfo.order_status == 1 && orderInfo.pay_status != 1){
			// 取消订单
			myPro.wxRequest("user/v2/order/cancelorder","POST",{order_id: orderInfo.id}).then(res=>{
				wx.showToast({
					title: res.msg,
					icon: "none"
				});
				that.getOrderDetail();
			}).catch(err=>{
				console.log('报错信息',err);
				wx.showToast({
					title: err,
					icon: "none"
				});
			});
		}else{
			wx.showModal({
				title: "可联系商家取消",
				content: "商家电话"+store.phone,
				showCancel: false,
				confirmText: "我知道了",
				confirmColor: "#000000"
			});
		}
	},
	// 收货
	receiveOrder(){
		let that = this;
		let params = {
			order_id: that.data.order_id
		};
		myPro.wxRequest("user/order/commit","POST",params).then(res=>{
			wx.showToast({
				title: res.msg,
				icon: "none"
			});
			that.getOrderDetail();
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		});
	},
	// 去支付
	goTopay(){
		let that = this;
		wx.navigateTo({
			url: '/pages/pay-order/index?order_id='+that.data.order_id
		})
	},
	// 拨打店铺电话
	onMakePhone(){
		let that = this;
		wx.makePhoneCall({
			phoneNumber: that.data.orderInfo.store.phone
		});
	},
});
