<template>
	<view :style="colorStyle">
		<view class="my-order">
			<view class="header-group">
				<view class="tabs">
					<u-tabs  :is-scroll="true" :list="navigationList" :current="tabIndex" @change="statusClick()" lineColor="#B22338"
						:inactiveStyle="{ color: '#999999' }" :activeStyle="{ color: '#B22338', fontWeight: 'bold' }"
						keyName="title"></u-tabs>
				</view>
			</view>
				<swiper  :style="{ height: height }" :current="tabIndex" @change="swiperChange">
					<swiper-item v-for="(tab, i) in navigationList" :key="i">
						<mescroll-item ref="MescrollItem" :i="i" :index="tabIndex" :tabs="navigationList" :height="height">
						</mescroll-item>
					</swiper-item>
				</swiper>
		</view>
		<view class="publish">
			<button @click="publishGroupon">发布团购</button>
		</view>
		<!-- #ifndef MP -->
		<home></home>
		<!-- #endif -->
		<payment :payMode="payMode" :pay_close="pay_close" @onChangeFun="onChangeFun" :order_id="pay_order_id"
			:totalPrice="totalPrice"></payment>
	</view>
</template>

<script>
import {
	getUserOrderPage,
	postUserOrderCancel,
	postUserOrderRefund
} from '@/api/home.js';
import {
	getOrderList,
	orderData,
	orderCancel,
	orderDel,
	orderPay
} from '@/api/order.js';
import {
	getUserInfo
} from '@/api/user.js';
import {
	openOrderSubscribe
} from '@/utils/SubscribeMessage.js';
import home from '@/components/home';
import payment from '@/components/payment';
import {
	toLogin
} from '@/libs/login.js';
import {
	mapGetters
} from 'vuex';
// #ifdef MP
import authorize from '@/components/Authorize';
// #endif
import emptyPage from '@/components/emptyPage.vue';
import MescrollItem from "./module/mescrollUni-item.vue";
import colors from '@/mixins/color.js';
export default {
	components: {
		MescrollItem,
		payment,
		home,
		emptyPage,
		// #ifdef MP
		authorize
		// #endif
	},
	mixins: [colors],
	data () {
		return {
			height:'600px',
			tabIndex:0,
			navigationList: [{
				title: '全部',
				id: 0
			}
			,
			{
				title: '申请中',
				id: 1
			},
			{
				title: '发布中',
				id: 2
			},
			{
				title: '已拒绝',
				id: 3
			},
			{
				title: '已结束',
				id: 4
			},
		],
			loading: false, //是否加载中
			loadend: false, //是否加载完毕
			loadTitle: this.$t(`加载更多`), //提示语
			orderList: [], //订单数组
			orderData: {}, //订单详细统计
			orderStatus: 0, //订单状态
			page: 1,
			limit: 20,
			payMode: [{
				name: this.$t(`微信支付`),
				icon: 'icon-weixinzhifu',
				value: 'weixin',
				title: this.$t(`使用微信快捷支付`),
				payStatus: true
			},
				// {
				// 	name: this.$t(`支付宝支付`),
				// 	icon: 'icon-zhifubao',
				// 	value: 'alipay',
				// 	title: this.$t(`使用支付宝支付`),
				// 	payStatus: true
				// },
				// {
				// 	name: this.$t(`余额支付`),
				// 	icon: 'icon-yuezhifu',
				// 	value: 'yue',
				// 	title: this.$t(`可用余额`),
				// 	number: 0,
				// 	payStatus: true
				// }, {
				// 	"name": this.$t(`好友代付`),
				// 	"icon": "icon-haoyoudaizhifu",
				// 	value: 'friend',
				// 	title: this.$t(`找微信好友支付`),
				// 	payStatus: 1,
				// }
			],
			pay_close: false,
			pay_order_id: '',
			pay_orderId: '',
			totalPrice: '0',
			isAuto: false, //没有授权的不会自动授权
			isShowAuth: false //是否隐藏授权
		};
	},
	computed: mapGetters(['isLogin']),
	onShow () {
		if (this.isLogin) {
			this.page = 1;
			this.orderList = []
			this.loadend = false;
			this.onLoadFun();
			this.getOrderList();
		} else {
			toLogin();
		}
		let sysInfo = uni.getSystemInfoSync()

		this.height= sysInfo.windowHeight -(sysInfo.screenWidth/750) * (200) +'px';
		console.log(this.height)



	},
	methods: {
		publishGroupon() {
			uni.navigateTo({
				url: `/pages/groupbuying/publish_group/index`
			})
		},

		swiperChange(e) {
			this.tabIndex = e.detail.current
		},

		onLoadFun () {
			this.getOrderData();
			this.getUserInfo();
		},
		// 授权关闭
		authColse: function (e) {
			this.isShowAuth = e;
		},
		/**
		 * 事件回调
		 *
		 */
		onChangeFun: function (e) {
			let opt = e;
			let action = opt.action || null;
			let value = opt.value != undefined ? opt.value : null;
			action && this[action] && this[action](value);
		},
		/**
		 * 获取用户信息
		 *
		 */
		getUserInfo: function () {
			let that = this;
			getUserInfo().then(res => {
				that.payMode[2].number = res.data.now_money;
				that.$set(that, 'payMode', that.payMode);
			});
		},
		/**
		 * 关闭支付组件
		 *
		 */
		payClose: function () {
			this.pay_close = false;
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function (options) {
			if (options.status) this.orderStatus = options.status;
		},
		/**
		 * 获取订单统计数据
		 *
		 */
		getOrderData: function () {
			let that = this;
			// orderData().then(res => {
			// 	that.$set(that, 'orderData', res.data);
			// 	that.payMode.map(item => {
			// 		if (item.value == 'weixin') {
			// 			item.payStatus = res.data.pay_weixin_open ? true : false;
			// 		}
			// 		if (item.value == 'alipay') {
			// 			item.payStatus = res.data.ali_pay_status ? true : false;
			// 		}
			// 		if (item.value == 'yue') {
			// 			item.payStatus = res.data.yue_pay_status == 1 ? true : false;
			// 		}
			// 		if (item.value == 'friend') {
			// 			item.payStatus = res.data.friend_pay_status == 1 ? true : false;
			// 		}
			// 	});
			// 	//#ifdef MP
			// 	this.payMode[1].payStatus = false;
			// 	//#endif
			// });
		},
		/**
		 * 取消订单
		 *
		 */
		cancelOrder: function (index, order_id) {
			let that = this;
			if (!order_id)
				return that.$util.Tips({
					title: that.$t(`缺少订单号无法取消订单`)
				});
			uni.showModal({
				title: that.$t(`取消订单`),
				content: that.$t(`确定取消该订单`),
				success: function (res) {
					if (res.confirm) {
						postUserOrderCancel({ id: order_id })
							.then(res => {
								return that.$util.Tips({
									title: res.msg,
									icon: 'success'
								},
									function () {
										that.orderList.splice(index, 1);
										that.$set(that, 'orderList', that.orderList);
										// that.$set(that.orderData, 'unpaid_count', that.orderData.unpaid_count - 1);
										// that.getOrderList()
									}
								);
							})
							.catch(err => {
								return that.$util.Tips({
									title: err
								});
							});
					} else if (res.cancel) {
						return that.$util.Tips({
							title: that.$t(`已取消`)
						});
					}
				}
			});

		},
		refundOrder: function (index, order_id) {
			let that = this;
			if (!order_id)
				return that.$util.Tips({
					title: that.$t(`缺少订单号无法申请退款订单`)
				});
			uni.showModal({
				title: that.$t(`申请退款`),
				content: that.$t(`确定申请退款该订单`),
				success: function (res) {
					if (res.confirm) {
						postUserOrderRefund({ id: order_id })
							.then(res => {
								return that.$util.Tips({
									title: res.msg,
									icon: 'success'
								},
									function () {
										that.orderList.splice(index, 1);
										that.$set(that, 'orderList', that.orderList);
										// that.$set(that.orderData, 'unpaid_count', that.orderData.unpaid_count - 1);
										// that.getOrderData();
										// this.getOrderList()
									}
								);
							})
							.catch(err => {
								return that.$util.Tips({
									title: err
								});
							});
					} else if (res.cancel) {
						return that.$util.Tips({
							title: that.$t(`已取消`)
						});
					}
				}
			});

		},
		/**
		 * 打开支付组件
		 *
		 */
		goPay: function (item, order_no) {
			this.$set(this, 'pay_close', true);
			this.$set(this, 'pay_order_id', order_no);
			this.$set(this, 'pay_orderId', item.id);
			this.$set(this, 'totalPrice', item.totalFee);
		},
		/**
		 * 支付成功回调
		 *
		 */
		pay_complete: function () {
			this.loadend = false;
			this.page = 1;
			this.$set(this, 'orderList', []);
			this.pay_close = false;
			uni.navigateTo({
				url: '/pages/goods/order_pay_status/index?order_id=' + this.pay_orderId +
					'&msg=' + this.$t(`支付成功`) + '&type=3&totalPrice=' + this.totalPrice
			})
			this.pay_order_id = '';
			this.pay_orderId = '';
			this.getOrderData();
			this.getOrderList();
		},
		/**
		 * 支付失败回调
		 *
		 */
		pay_fail: function () {
			this.pay_close = false;
			this.pay_order_id = '';
		},

		/**
		 * 切换类型
		 */
		statusClick: function (status) {
			console.log(status)
			console.log(status.index)
			this.tabIndex = status.index;
			if (status == this.orderStatus) return;
			this.orderStatus = status;
			this.loadend = false;
			this.page = 1;
			this.$set(this, 'orderList', []);
			this.getOrderList();
		},
		/**
		 * 获取订单列表
		 */
		getOrderList: function () {
			// let that = this;
			// if (that.loadend) return;
			// if (that.loading) return;
			// that.loading = true;
			// that.loadTitle = that.$t(`加载更多`);
			// getUserOrderPage({
			// 	userId: this.$store.state.app.uid,
			// 	current: that.page,
			// 	size: that.limit,
			// 	payFlag: this.orderStatus
			// })
			// 	.then(res => {
			// 		let list = res.data || [];
			// 		let loadend = list.length < that.limit;
			// 		that.orderList = that.$util.SplitArray(list, that.orderList);
			// 		that.$set(that, 'orderList', that.orderList);
			// 		that.loadend = loadend;
			// 		that.loading = false;
			// 		that.loadTitle = loadend ? that.$t(`没有更多内容啦~`) : that.$t(`加载更多`);
			// 		that.page = that.page + 1;
			// 	})
			// 	.catch(err => {
			// 		that.loading = false;
			// 		that.loadTitle = that.$t(`加载更多`);
			// 	});
		},

		/**
		 * 删除订单
		 */
		delOrder: function (order_id, index) {
			let that = this;
			uni.showModal({
				title: that.$t(`删除订单`),
				content: that.$t(`确定删除该订单`),
				success: function (res) {
					if (res.confirm) {
						orderDel(order_id)
							.then(res => {
								that.orderList.splice(index, 1);
								that.$set(that, 'orderList', that.orderList);
								that.$set(that.orderData, 'unpaid_count', that.orderData
									.unpaid_count - 1);
								that.getOrderData();
								return that.$util.Tips({
									title: that.$t(`删除成功`),
									icon: 'success'
								});
							})
							.catch(err => {
								return that.$util.Tips({
									title: err
								});
							});
					} else if (res.cancel) {
						return that.$util.Tips({
							title: that.$t(`已取消`)
						});
					}
				}
			});

		}
	},
	onReachBottom: function () {
		this.getOrderList();
	}
};
</script>

<style scoped lang="scss">
.my-order .nav {
	background-color: #F9F9F9;
	width: 100%;
	// height: 140rpx;
}

.my-order .nav .item {
	text-align: center;
	font-size: 26rpx;
	color: #282828;
	padding: 27rpx 0;
	border-bottom: 5rpx solid transparent;
}

.my-order .nav .item.on {
	/* #ifdef H5 || MP */
	font-weight: bold;
	/* #endif */
	/* #ifdef APP-PLUS */
	color: #000;
	/* #endif */
	border-color: var(--view-theme);
}

.my-order .nav .item .num {
	margin-top: 18rpx;
}

.list {
	width: 100%;
	background: white;
	margin: 14rpx auto 0 auto;
	padding-bottom: 30rpx;
}

.noCart {
	margin-top: 171rpx;
	padding-top: 0.1rpx;
}

.noCart .pictrue {
	width: 414rpx;
	height: 336rpx;
	margin: 78rpx auto 56rpx auto;
}

.noCart .pictrue image {
	width: 100%;
	height: 100%;
}

.my-order .list .item .item-info .text .money .return {
	// color: var(--view-priceColor);
	margin-top: 10rpx;
	font-size: 24rpx;
}


.myclient_list {
	padding: 30rpx;
	background: #FFFFFF;

	.myclient_list_name {
		margin-bottom: 15rpx;
		min-height: 44rpx;
		font-size: 28rpx;
		font-weight: 500;
		color: #111111;
		line-height: 44rpx;
		display: flex;

		.nv {
			flex-grow: 1;
		}

		.status {
			width: 300rpx;
			text-align: right;
			margin-left: 30rpx;
			color: #75BE00;
		}
	}

	.myclient_list_content {
		.subtitle {
			height: 44rpx;
			margin: 20rpx 0;
			font-size: 28rpx;
			font-weight: 400;
			color: #666666;
			line-height: 44rpx;
		}

		image {
			width: 24rpx;
			height: 24rpx;
			margin-right: 20rpx;
		}

		.phone {
			margin-bottom: 15rpx;
			height: 44rpx;
			font-size: 24rpx;
			font-weight: 400;
			color: #999999;
			line-height: 44rpx;
		}

		.time {
			height: 44rpx;
			font-size: 24rpx;
			font-weight: 400;
			color: #999999;
			line-height: 44rpx;
		}
	}
}

.all-products-body {
	background: white;

	.all-products-item {
		display: flex;
		margin-bottom: 20rpx;

		image {
			width: 128rpx;
			height: 128rpx;
			margin-right: 20rpx;
			border: 2rpx solid #FFFFFF;
		}

		.all-products-item-content {
			flex-grow: 1;
			display: flex;
			flex-direction: column;


			.all-products-item-content-t {
				flex: 1;
				font-size: 24rpx;
				font-weight: 400;
				color: #333333;
				line-height: 40rpx;
			}

			.all-products-item-content-b {
				flex: 1;
				font-size: 28rpx;
				font-weight: 500;
				line-height: 80rpx;
				display: flex;
				justify-content: space-between;

				.red {
					display: block;
					font-size: 28rpx;
					font-weight: 500;
					color: #B22338 !important;
					margin-right: 10rpx;
				}

				.line-thr {
					display: block;
					font-size: 28rpx;
					font-weight: 400;
					color: #999999;
					text-decoration: line-through;
				}

				.count {
					font-size: 24rpx;
					font-weight: 400;
					color: #999999;
					margin-right: 30rpx;
				}
			}
		}
	}
}


.a-t {
	padding: 0 30rpx;
	display: flex;
	justify-content: space-between;
	height: 44rpx;
	margin-bottom: 20rpx;

}

.a-m {
	padding: 0 30rpx;

	.am1 {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;

		.am1-1 {
			height: 40rpx;
			font-size: 28rpx;
			font-weight: 400;
			color: #111111;
			line-height: 40rpx;

		}

		.am1-2 {
			height: 36rpx;
			font-size: 20rpx;
			font-weight: 400;
			color: #666666;
			line-height: 36rpx;

			image {
				margin-left: 30rpx;
				vertical-align: middle;
				height: 34rpx;
				width: 34rpx;
			}
		}
	}

	.am2 {
		margin-bottom: 20rpx;
		height: 34rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;
		line-height: 34rpx;
	}


}

.a-b {
	padding: 0 30rpx;
	width: 100%;
	background: #FFF7F8;
	height: 60rpx;
	line-height: 60rpx;
	display: flex;
	justify-content: space-between;

	.a-b1 {
		font-size: 20rpx;
		font-weight: 400;
		color: #666666;

		.red {
			font-size: 20rpx;
			font-weight: 500;
			color: #B22338;
		}
	}

	.a-b2 {
		image {
			vertical-align: middle;
			margin-right: 30rpx;
			margin-left: 30rpx;
			height: 34rpx;
			width: 34rpx;
		}
	}
}

.self-pickup-point {
	background: #FFF7F8;
	margin: 0 30rpx;
}

.op-area {
	height: 120rpx;
	display: flex;
	justify-content: flex-end;
	line-height: 120rpx;
	padding: 20rpx 0;
	text-align: right;

	.action-btn {
		min-width: 200rpx;
		background: #FFF7F8;
		border-radius: 32rpx;
		border: 2rpx solid #B22338;
		margin-right: 30rpx;
		height: 64rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #B22338;
		line-height: 64rpx;
	}

	.info-btn {
		width: 200rpx;
		background: #F5F5F5;
		border-radius: 32rpx;
		margin-right: 30rpx;
		height: 64rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #666666;
		line-height: 64rpx;
	}
}

.Ended {
	color: #666666 !important;
}

.Refused {
	color: #B42A3E !important;
}

.publish {
	position: fixed;
	bottom: 0;
	height: 100rpx;
	width: 100%;
	background: #F9F9F9;

	button {
		width: 90%;
		height: 84rpx;
		background: #B42A3E;
		border-radius: 8rpx;
		position: relative;
		top: 0;
		margin: auto;
		font-size: 28rpx;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 84rpx;
	}
}
	.bord{
		border: 1px solid red;
	}
</style>
