<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索商品">
			</view>
			<view class="two">
				<tabs :tabs="tabs" @tabsChange="tabsChange">
					<view class="tabsList">
						<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
							<view class="list-scroll-view">
								<view class="list_1" v-if="tabs.active=='-0'">
									全部订单
								</view>
								<view class="list_2" v-for="(item,index) in list" :key="index" v-else-if="tabs.active=='0'">
									<view class="status">
										{{item.status=='0'?'待付款':item.status=='1'?'已支付':item.status=='-1'?'取消订单':item.status=='2'?'发货中':item.status=='-2'?'已退款':item.status=='3'?'已收货':item.status=='-3'?'申请售后':item.status=='-4'?'正在售后中':item.status=='-5'?'售后已结束':'未识别'}}
									</view>
									<view class="goods" v-for="(tag,indexs) in item.goods" :key="indexs">
										<view class="goods_1">
											<view class="shopname">
												<text class="iconfont icon-shangdian"></text>
												<text>{{tag.shop_name}}</text>
											</view>
										</view>
										<view class="goods_2">
											<view class="market" v-for="(tags,indexss) in tag.goods" :key="indexss">
												<view class="url">
													<image class="image" :src="tags.goods.file&&tags.goods.file.length>0?tags.goods.file[0].url:''" mode=""></image>
												</view>
												<view class="goodsname">
													{{tags.name}}
												</view>
												<view class="goodsother">
													<view class="price">
														¥{{tags.sell_money}}
													</view>
													<view class="num">
														×{{tags.buy_num}}
													</view>
												</view>
											</view>
										</view>
									</view>
									<view class="other">
										<text>共{{item.total_detail.freight_total}}件商品</text>
										<text>总价¥{{item.total_detail.goods_total}}</text>
									</view>
									<view class="btn">
										<button class="toCancel" v-if="item.status=='0'" type="default" size="mini" @click="toCancel(item)">取消订单</button>
										<button class="toPay" v-if="item.status=='0'" type="default" size="mini" @click="toPay(item)">付款</button>
										<button class="toAfter" v-if="item.status=='3'" type="default" size="mini" @click="toAfter(item)">申请售后</button>
										<button class="toRefund" v-if="item.status=='3'" type="default" size="mini" @click="toRefund(item)">申请退款</button>
									</view>
								</view>
								<view class="list_2" v-for="(item,index) in list" :key="index" v-else>
									<view class="status">
										{{item.status=='0'?'待付款':item.status=='1'?'已支付':item.status=='-1'?'取消订单':item.status=='2'?'发货中':item.status=='-2'?'已退款':item.status=='3'?'已收货':item.status=='-3'?'申请售后':item.status=='-4'?'正在售后中':item.status=='-5'?'售后已结束':'未识别'}}
									</view>
									<view class="goods" v-for="(tag,indexs) in item.order.goods" :key="indexs">
										<view class="goods_1">
											<view class="shopname">
												<text class="iconfont icon-shangdian"></text>
												<text>{{tag.shop_name}}</text>
											</view>
										</view>
										<view class="goods_2">
											<view class="market" v-for="(tags,indexss) in tag.goods" :key="indexss">
												<view class="url">
													<image class="image" :src="tags.goods.file&&tags.goods.file.length>0?tags.goods.file[0].url:''" mode=""></image>
												</view>
												<view class="goodsname">
													{{tags.name}}
												</view>
												<view class="goodsother">
													<view class="price">
														¥{{tags.sell_money}}
													</view>
													<view class="num">
														×{{tags.buy_num}}
													</view>
												</view>
											</view>
										</view>
									</view>
									<view class="other">
										<text>共{{item.buy_num_total||0}}件商品</text>
										<text>总价¥{{item.total_detail.goods_total}}</text>
									</view>
									<view class="btn">
										<button class="toCancel" v-if="item.status=='0'" type="default" size="mini" @click="toCancel(item)">取消订单</button>
										<button class="toPay" v-if="item.status=='0'" type="default" size="mini" @click="toPay(item)">付款</button>
										<button class="toAfter" v-if="item.status=='3'" type="default" size="mini" @click="toAfter(item)">申请售后</button>
										<button class="toRefund" v-if="item.status=='3'" type="default" size="mini" @click="toRefund(item)">申请退款</button>
									</view>
								</view>
							</view>
						</scroll-view>
					</view>
				</tabs>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	import tabs from '@/components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				user: {},
				status: '',
				searchInfo: {},
				tabs: {
					active: '0',
					menu: [ //菜单列表
						// {
						// 	title: '全部订单',
						// 	active: '-0'
						// },
						{
							title: '待付款',
							active: '0'
						},
						{
							title: '待发货',
							active: '1'
						},
						{
							title: '待收货',
							active: '2'
						},
						{
							title: '已收货',
							active: '3'
						}
					]
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 0
			};
		},
		onLoad: function(e) {
			const that = this;
			that.$set(that, `status`, e.status);
			// 监听用户是否登录
			that.watchLogin();
		},
		onShow: function() {},
		methods: {
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						if (user) that.$set(that, `user`, user);
						that.$set(that.tabs, `active`, that.status);
						that.search();
					},
					fail: function(err) {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				});
			},
			// 查询列表
			async search() {
				const that = this;
				let user = that.user;
				let status = that.status;
				if (status == '-0') {

				} else if (status == '0') {
					const res = await that.$api(`/order`, 'GET', {
						status: '0',
						customer: user._id
					});
					if (res.errcode == '0') {
						let list = [...that.list, ...res.data];
						that.$set(that, `list`, list)
						that.$set(that, `total`, res.total)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				} else {
					const res = await that.$api(`/orderDetail`, 'GET', {
						status: that.status,
						customer: user._id
					});
					if (res.errcode == '0') {
						let list = [...that.list, ...res.data];
						that.$set(that, `list`, list)
						that.$set(that, `total`, res.total)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else uni.showToast({
					title: '没有更多数据了'
				});
			},
			// 输入框
			toInput(e) {
				const that = this;
				that.$set(that.searchInfo, `name`, e.detail.value)
			},
			// 取消订单
			toCancel(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定取消订单吗?',
					success: async function(res) {
						if (res.confirm) {
							const arr = await that.$api(`/order/cancel`, 'POST', {
								order_id: e._id
							});
							if (arr.errcode == '0') {
								uni.showToast({
									title: '取消订单成功',
									icon: 'none'
								})
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 付款
			toPay(e) {
				const that = this;
				uni.getStorage({
					key: 'system',
					success: async function(res) {
						// 微信小程序支付
						if (res.data.uniPlatform == "mp-weixin") {
							const res = await that.$api('/pay/toPayOrder', 'POST', {
								order_id: e,
								type: '0'
							})
							console.log(res);
							uni.requestPayment({
								"provider": "wxpay",
								...res.data,
								success(res) {
									uni.showToast({
										title: '支付成功',
										icon: 'none'
									})
									that.clearPage();
									that.search();
								},
								fail(e) {
									console.log('in fail');
									console.log(e)
								}
							})
						} else if (res.data.uniPlatform == "app") {
							// app支付
							uni.requestPayment({
								provider: 'alipay',
								orderInfo: 'orderInfo', //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
								success: function(res) {
									console.log('success:' + JSON.stringify(res));
								},
								fail: function(err) {
									console.log('fail:' + JSON.stringify(err));
								}
							});
						} else {
							uni.showToast({
								title: `平台不支持支付`,
								icon: 'none'
							})
						}
					},
					fail: function(err) {}
				})
			},
			// 申请售后
			toAfter(e) {
				uni.navigateTo({
					url: `/pagesMy/order/service?id=${e._id}`
				})
			},
			// 申请退款
			toRefund(e) {
				console.log(e);
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.$set(that, `status`, e.active);
				that.clearPage();
				that.search()
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 5)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 2vw;

			input {
				padding: 2vw;
				background-color: var(--f1Color);
				font-size: var(--font14Size);
				border-radius: 5px;
			}
		}

		.two {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);

			.tabsList {
				position: relative;
				width: 100vw;
				height: 82vh;

				.list_2 {
					background-color: #fff;
					margin: 0 2vw 2vw 2vw;
					padding: 2vw;

					.status {
						text-align: right;
						margin: 0 0 1vw 0;
					}

					.goods {
						margin: 0 0 1vw 0;
						padding: 2vw;
						border-bottom: 1px solid #f5f5f5;

						.goods_1 {
							margin: 0 0 2vw 0;

							.shopname {
								text:last-child {
									padding: 0 0 0 2vw;
								}
							}
						}

						.goods_2 {
							margin: 0 0 1vw 0;

							.market {
								display: flex;

								.url {
									width: 20vw;

									.image {
										width: 100%;
										height: 20vw;
										border-radius: 5px;

									}
								}

								.goodsname {
									width: 60vw;
									padding: 0 2vw;
								}

								.goodsother {
									width: 15vw;
									text-align: right;
								}
							}
						}
					}

					.other {
						margin: 0 0 2vw 0;
						text-align: right;

						text {
							font-size: 14px;

							padding: 0 0 0 2vw;
						}
					}

					.btn {
						text-align: right;
						margin: 2vw 0 0 0;
						border-top: 1px solid #f1fff1;

						button {
							margin: 2vw 0 0 2vw;
						}

						.toPay {
							border: 1px solid #ff0000;
							color: #ff0000;
						}
					}
				}

				// .list {
				// 	background-color: #fff;
				// 	margin: 0 2vw 2vw 2vw;
				// 	padding: 2vw;

				// 	.list_1 {
				// 		margin: 0 0 1vw 0;
				// 		display: flex;
				// 		flex-direction: row;
				// 		justify-content: space-between;
				// 	}

				// 	.list_2 {
				// 		margin: 0 0 1vw 0;
				// 		display: flex;

				// 		.l {
				// 			width: 20vw;

				// 			.image {
				// 				width: 100%;
				// 				height: 20vw;
				// 				border-radius: 5px;

				// 			}
				// 		}

				// 		.c {
				// 			width: 60vw;
				// 			padding: 0 2vw;
				// 		}

				// 		.r {
				// 			width: 15vw;
				// 			text-align: right;
				// 		}
				// 	}

				// 	.other {
				// 		margin: 0 0 2vw 0;
				// 		text-align: right;

				// 		text {
				// 			font-size: 14px;

				// 			padding: 0 0 0 2vw;
				// 		}
				// 	}

				// 	.btn {
				// 		text-align: right;
				// 		margin: 2vw 0 0 0;
				// 		border-top: 1px solid #f1fff1;

				// 		button {
				// 			margin: 2vw 0 0 2vw;
				// 		}
				// 	}
				// }
			}
		}
	}

	// 	.two {
	// 		position: relative;
	// 		flex-grow: 1;
	// 		background-color: var(--f9Color);

	// 		.two_1 {
	// 			background-color: var(--fffColor);
	// 			padding: 2vw;
	// 			display: flex;
	// 			flex-direction: row;
	// 		}

	// 		.two_2 {
	// 			display: flex;
	// 			flex-direction: column;

	// 			.list {
	// 				width: 100vw;
	// 				margin: 2vw 0 0 0;
	// 				background-color: var(--mainColor);

	// 				.list_1 {
	// 					display: flex;
	// 					flex-direction: row;
	// 					justify-content: space-between;
	// 					padding: 2vw;

	// 					.name {
	// 						font-size: var(--font14Size);

	// 						text {
	// 							margin: 0 1vw 0 0;
	// 						}
	// 					}

	// 					.status {
	// 						font-size: var(--font14Size);
	// 						color: var(--ff0Color);
	// 					}
	// 				}

	// 				.list_2 {
	// 					display: flex;
	// 					flex-direction: row;
	// 					justify-content: space-between;
	// 					padding: 2vw;
	// 					background-color: var(--f8Color);

	// 					.image {
	// 						width: 20vw;
	// 						height: 20vw;
	// 						margin: 0 2vw 0 0;
	// 					}

	// 					.other {
	// 						display: flex;
	// 						flex-direction: column;
	// 						flex-grow: 1;

	// 						.name {
	// 							font-size: var(--font14Size);
	// 							font-weight: bold;
	// 							margin: 0 0 2vw 0;
	// 						}

	// 						.other_1 {
	// 							font-size: var(--font12Size);
	// 							color: var(--f85Color);
	// 						}
	// 					}

	// 					.money {
	// 						font-size: var(--font12Size);

	// 						.num {
	// 							text-align: right;
	// 						}
	// 					}
	// 				}

	// 				.list_3 {
	// 					display: flex;
	// 					justify-content: flex-end;
	// 					padding: 2vw;
	// 					border-bottom: 0.5vw solid var(--f9Color);
	// 					font-size: var(--font12Size);

	// 					text {
	// 						margin: 0 1vw;
	// 					}
	// 				}

	// 				.list_4 {
	// 					padding: 2vw;
	// 					text-align: right;

	// 					button {
	// 						margin: 0 1vw 0 2vw;
	// 					}
	// 				}
	// 			}
	// 		}
	// 	}
	// }

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>
