<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" @scroll="toScroll">
							<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'" @tap="toInfo(item)">
									<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.goods.name}}
													<view class="specs">
														{{tags.name}}
													</view>
												</view>
												<view class="goodsother">
													<view v-if="item.type=='0'" class="price">
														¥{{tags.sell_money}}
													</view>
													<view v-else class="price">
														¥{{tags.group_config.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 v-if="item.status=='0'" type="default" size="mini"
											@tap.stop="toCancel(item)">取消订单</button>
										<button class="toPay" v-if="item.status=='0'" type="default" size="mini"
											@tap.stop="toPay(item)">付款</button>
										<button v-if="item.status=='2'" type="default" size="mini"
											@tap.stop="toConfirm(item)">确认收货</button>
										<button v-if="item.status!='0'" type="default" size="mini"
											@tap.stop="toAfter(item)">申请售后</button>
										<button v-if="item.status=='3'&&!item.rate" type="default" size="mini"
											@tap.stop="toAppraise(item)">立即评价</button>
										<button v-if="item.status=='3'&&item.rate" type="default" size="mini"
											@tap.stop="toAppraise(item)">追加评价</button>
									</view>
								</view>
								<view class="list_2 list_3" v-for="(item,index) in list" :key="index"
									@tap="toInfo(item)" v-else>
									<view class="list_3_1">
										<view class="goods_1">
											<view class="shopname">
												<text class="iconfont icon-shangdian"></text>
												<text>{{item.shop.name}}</text>
											</view>
										</view>
										<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>
									<view class="goods" v-for="(tag,indexs) in item.goods" :key="indexs">
										<view class="goods_2">
											<view class="market">
												<view class="url">
													<image class="image" :src="tag.url" mode=""></image>
												</view>
												<view class="goodsname">
													{{tag.goods.name}}
													<view class="specs">
														{{tag.name}}
													</view>
												</view>
												<view class="goodsother">
													<view v-if="item.type=='0'" class="price">
														¥{{tag.sell_money}}
													</view>
													<view v-else class="price">
														¥{{tag.group_config.money}}
													</view>
													<view class="num">
														×{{tag.buy_num}}
													</view>
												</view>
											</view>
										</view>
									</view>
									<view class="other">
										<text>共{{item.buy_num_total||0}}件商品</text>
										<text>总价¥{{item.real_pay}}</text>
									</view>
									<view class="btn">
										<button v-if="item.status=='2'||item.status=='3'" type="default" size="mini"
											@tap.stop="toLogi(item)">查看物流</button>
										<button v-if="item.status=='0'" type="default" size="mini"
											@tap.stop="toCancel(item)">取消订单</button>
										<button class="toPay" v-if="item.status=='0'" type="default" size="mini"
											@tap.stop="toPay(item)">付款</button>
										<button v-if="item.status=='2'" type="default" size="mini"
											@tap.stop="toConfirm(item)">确认收货</button>
										<button v-if="item.status!='0'" type="default" size="mini"
											@tap.stop="toAfter(item)">申请售后</button>
										<button v-if="item.status=='3'&&!item.rate" type="default" size="mini"
											@tap.stop="toAppraise(item)">立即评价</button>
										<button v-if="item.status=='3'&&item.rate" type="default" size="mini"
											@tap.stop="toAppraise(item)">追加评价</button>
									</view>
								</view>
								<view class="is_bottom" v-if="is_bottom">
									<text>{{config.bottom_title}}</text>
								</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 {
				// 系统设置
				config: {},
				user: {},
				status: '',
				searchInfo: {},
				tabs: {
					active: '0',
					menu: [ //菜单列表
						// {
						// 	title: '全部订单',
						// 	active: '-0'
						// },
						{
							title: '待付款',
							active: '0'
						},
						{
							title: '待发货',
							active: '1'
						},
						{
							title: '待收货',
							active: '2'
						},
						{
							title: '已收货',
							active: '3'
						},
						{
							title: '取消订单',
							active: '-1'
						}
					]
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			};
		},
		onLoad: function(e) {
			const that = this;
			that.$set(that, `status`, e.status);
			that.searchConfig();
			// 监听用户是否登录
			that.watchLogin();
			
		},
		methods: {
			// 查询基本设置
			searchConfig() {
				const that = this;
				uni.getStorage({
					key: 'config',
					success: function(res) {
						if (res.data) that.$set(that, `config`, res.data)
					},
					fail: function(err) {
						console.log(err);
					}
				})
			},
			// 监听用户是否登录
			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 that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 输入框
			toInput(e) {
				const that = this;
				that.$set(that.searchInfo, `name`, e.detail.value)
			},
			// 订单详细
			toInfo(item) {
				uni.navigateTo({
					url: `/pagesMy/order/info?id=${item._id}&status=${item.status}`
				})
			},
			// 取消订单
			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") {
							uni.showLoading({
								title: '加载中'
							})
							const res = await that.$api('/pay/toPayOrder', 'POST', {
								order_id: e,
								type: '0'
							})
							uni.requestPayment({
								"provider": "wxpay",
								...res.data,
								success(res) {
									uni.showToast({
										title: '支付成功',
										icon: 'none'
									})
									uni.hideLoading();
									that.clearPage();
									that.search();
								},
								fail(e) {
									uni.showToast({
										title: `支付失败`,
										icon: 'none'
									})
									uni.hideLoading();
									that.clearPage();
									that.search();
								}
							})
						} 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) {}
				})
			},
			// 确认收货
			toConfirm(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定确认收货吗?',
					success: async function(res) {
						if (res.confirm) {
							const arr = await that.$api(`/orderDetail/${e._id}`, 'POST', {
								status: '3'
							});
							if (arr.errcode == '0') {
								uni.showToast({
									title: '确认收货成功',
									icon: 'none'
								})
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 申请售后 申请退款
			toAfter(e) {
				if (e.status == '3') {
					uni.navigateTo({
						url: `/pagesMy/order/service?id=${e._id}`
					})
				} else {
					uni.navigateTo({
						url: `/pagesMy/order/noService?id=${e._id}`
					})
				}
			},
			// 查看物流
			toLogi(e) {
				uni.navigateTo({
					url: `/pagesMy/logistics/index?id=${e._id}`
				})
			},
			// 立即评价
			toAppraise(e) {
				if (e.rate) {
					uni.navigateTo({
						url: `/pagesMy/order/appraise?id=${e._id}&rate_id=${e.rate}`
					})
				} else {
					uni.navigateTo({
						url: `/pagesMy/order/appraise?id=${e._id}`
					})
				}
			},
			// 选择选项卡
			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)
			}
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.$set(that, `list`, [])
			that.$set(that, `skip`, 0)
			that.$set(that, `limit`, 6)
			that.$set(that, `page`, 0)
			await that.search();
			uni.stopPullDownRefresh();
		}
	}
</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 {
						color: var(--ff0Color);
						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 {
									display: flex;
									flex-direction: column;
									width: 60vw;
									padding: 0 2vw;

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



								.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_3 {
					.list_3_1 {
						display: flex;
						justify-content: space-between;
					}
				}

				// .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;
		}
	}

	.is_bottom {
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: #858585;
			font-size: 14px;
		}
	}
</style>
