<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" v-for="(item,index) in list" :key="index" @tap="toInfo(item)">
									<view class="list_1" v-if="tabs.active=='0'">
										<view class="goods">
											<view class="goodsList" v-for="(tag,indexs) in item.goods" :key="indexs">
												<view class="shopname">
													<view class="shop">
														<text class="iconfont icon-shangdian"></text>
														<text>{{tag.shop_name}}</text>
													</view>
													<view class="status">
														{{item.zhStatus}}
													</view>
												</view>
												<view class="goodsList_1">
													<view class="market" v-for="(tags,indexss) in tag.goods"
														:key="indexss">
														<view class="market_1">
															<image class="image"
																:src="tags.goods.file&&tags.goods.file.length>0?tags.goods.file[0].url:''"
																mode=""></image>
														</view>
														<view class="market_2">
															<view class="goodsname textOver">
																{{tags.goods.name}}
															</view>
															<view class="specs">
																{{tags.name}}
															</view>
														</view>
														<view class="market_3">
															<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>
										<view class="other">
											<text>共{{item.total_detail.freight_total}}件商品</text>
											<text>总价¥{{item.total_detail.goods_total}}</text>
										</view>
									</view>
									<view class="list_2" v-else>
										<view class="list_2_1">
											<view class="shopname">
												<text class="iconfont icon-shangdian"></text>
												<text>{{item.shop.name}}</text>
											</view>
											<view class="status">
												{{item.zhStatus}}
											</view>
										</view>
										<view class="list_2_2">
											<view class="goods" v-for="(tag,indexs) in item.goods" :key="indexs">
												<view class="goods_1">
													<image class="image" :src="tag.url" mode=""></image>
												</view>
												<view class="goods_2">
													<view class="goodsname textOver">
														{{tag.goods.name}}
													</view>
													<view class="specs">
														{{tag.name}}
													</view>
												</view>
												<view class="goods_3">
													<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 class="other">
											<text>共{{item.buy_num_total||0}}件商品</text>
											<text>总价¥{{item.real_pay}}</text>
										</view>
									</view>
									<view class="btn">
										<button v-if="item.status=='0'" size="mini"
											@tap.stop="toCancel(item)">取消订单</button>
										<button v-if="item.status=='0'" size="mini" @tap.stop="toPay(item)">支付</button>
										<button v-if="item.status=='1'&&item.is_afterSale!=true" size="mini"
											@tap.stop="toCancels(item)">取消订单</button>
										<button v-if="item.status=='2'||item.status=='3'" size="mini"
											@tap.stop="toLogi(item)">查看物流</button>
										<button v-if="item.status=='2'&&item.is_afterSale!=true" size="mini"
											@tap.stop="toConfirm(item)">确认收货</button>
										<button v-if="item.status=='2'&&item.is_afterSale!=true" size="mini"
											@tap.stop="toReject(item)">拒收</button>
										<button v-if="item.status=='3'" size="mini"
											@tap.stop="toAfter(item)">申请售后</button>
										<button v-if="item.status=='3'" type="default" size="mini"
											@tap.stop="toAppraise(item)">{{item.rate?'追加评价':'立即评价'}}</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: {},
				// 设备信息
				system: {},
				user: {},
				status: '',
				searchInfo: {},
				tabs: {
					active: '0',
					menu: []
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 字典表
				statusList: []
			};
		},
		onLoad: function(e) {
			const that = this;
			that.$set(that, `status`, e.status || '');
			that.$set(that.tabs, `active`, e.status || '');
			that.searchConfig();
		},
		onShow: async function(e) {
			const that = this;
			await that.searchOther();
			await that.watchlogin();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 查询基本设置
			searchConfig() {
				const that = this;
				uni.getStorage({
					key: 'config',
					success: function(res) {
						if (res.data) that.$set(that, `config`, res.data)
						// 设备平台信息
						let config = that.$config;
						that.$set(that, `system`, config.system);
					},
					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.search();
						}
					}
				})
			},
			async search() {
				const that = this;
				let user = that.user;
				let status = that.status;
				let info = {
					skip: that.skip,
					limit: that.limit,
					customer: user._id,
					status: status
				}
				let res;
				if (status == '0') {
					res = await that.$api(`/order`, 'GET', {
						...info,
						...that.searchInfo
					});
				} else {
					res = await that.$api(`/orderDetail`, 'GET', {
						...info,
						...that.searchInfo
					});
				}
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						val.zhStatus = that.searchStatus(val.status)
					}
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
			},
			// 查询状态
			searchStatus(e) {
				const that = this;
				let data = that.statusList.find((i) => i.value == e);
				if (data) return data.label
				else return '暂无'
			},
			// 分页
			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'
								})
							}
						}
					}
				});
			},
			// 支付
			async toPay(e) {
				const that = this;
				let system = that.system;
				if (system.uniPlatform == "mp-weixin") {
					uni.showLoading({
						title: '加载中'
					})
					// 支付信息
					let p2 = await that.$api('/pay/toPayOrder', 'POST', {
						order_id: e,
						type: '0'
					})
					if (p2.errcode == '0' && p2.data.paySign) {
						uni.requestPayment({
							"provider": "wxpay",
							...p2.data,
							success: function(res) {
								uni.showToast({
									title: '支付成功',
									icon: 'none'
								})
							},
							fail: function(err) {
								uni.showToast({
									title: `支付失败`,
									icon: 'none'
								})
							},
							complete: function() {
								uni.hideLoading();
								that.clearPage();
								that.search();
							}
						})
					} else {
						uni.showToast({
							title: p2.errmsg || '错误信息',
							icon: 'none'
						})
					}
				} 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'
					})
				}
			},
			// 取消订单-已支付
			toCancels(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定取消订单吗?',
					success: async function(res) {
						if (res.confirm) {
							let arr = await that.$api(`/afterSale`, 'POST', {
								order_detail: e._id,
								type: '4'
							})
							if (arr.errcode == '0') {
								uni.showToast({
									title: '申请取消订单完成',
									icon: 'none'
								})
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 查看物流
			toLogi(e) {
				const that = this;
				that.clearPage();
				uni.navigateTo({
					url: `/pagesMy/logistics/index?id=${e._id}`
				})
			},
			// 确认收货
			async 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'
								})
							}
						}
					}
				});
			},
			// 拒收
			async toReject(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定拒收快递吗?',
					success: async function(res) {
						if (res.confirm) {
							let arr = await that.$api(`/afterSale`, 'POST', {
								order_detail: e._id,
								type: '5'
							})
							if (arr.errcode == '0') {
								uni.showToast({
									title: '拒收申请完成',
									icon: 'none'
								})
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 立即评价
			toAppraise(e) {
				const that = this;
				that.clearPage();
				uni.navigateTo({
					url: `/pagesMy/order/appraise?id=${e._id}`
				})
			},
			// 申请售后
			toAfter(e) {
				const that = this;
				that.clearPage();
				uni.navigateTo({
					url: `/pagesMy/order/service?id=${e._id}&&status=${e.status}`
				})
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询状态
				res = await that.$api(`/dictData`, 'GET', {
					code: 'order_process'
				})
				if (res.errcode == '0') {
					that.$set(that, `statusList`, res.data);
					var menu = res.data.map((item) => {
						return {
							title: item.label,
							active: item.value
						}
					})
					that.$set(that.tabs, `menu`, menu)
				}
			},
			// 选择选项卡
			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`, 6)
				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 {
					background-color: #fff;
					border: 1px solid #ff0000;
					padding: 2vw;
					margin: 0 2vw 2vw 2vw;
					border-radius: 5px;

					.list_1 {


						.goods {
							.goodsList {
								border-bottom: 1px solid #f1f1f1;

								.shopname {
									display: flex;
									justify-content: space-between;
									margin: 0 0 2vw 0;

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

									.status {
										text-align: right;
										color: var(--ff0Color);
										margin: 0 0 1vw 0;
									}
								}

								.goodsList_1 {
									.market {
										display: flex;
										padding: 0 0 2vw 0;

										.market_1 {
											width: 20vw;
											height: 20vw;

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

										.market_2 {
											width: 55vw;
											padding: 0 0 0 2vw;

											.goodsname {
												font-size: 16px;
												margin: 0 0 1vw 0;
											}

											.specs {
												font-size: 14px;
												color: #858585;
											}
										}

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

						.other {
							padding: 0 0 2vw 0;
							margin: 2vw 0;
							text-align: right;
							border-bottom: 1px solid #f1f1f1;

							text {
								font-size: 14px;
								padding: 0 0 0 2vw;
							}
						}
					}

					.list_2 {
						.list_2_1 {
							display: flex;
							justify-content: space-between;
							margin: 0 0 2vw 0;

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

							.status {
								color: var(--ff0Color);
							}
						}

						.list_2_2 {
							border-bottom: 1px solid #f1f1f1;

							.goods {
								display: flex;
								padding: 0 0 2vw 0;

								.goods_1 {
									width: 20vw;
									height: 20vw;

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

								.goods_2 {
									width: 55vw;
									padding: 0 0 0 2vw;

									.goodsname {
										font-size: 16px;
										margin: 0 0 1vw 0;
									}

									.specs {
										font-size: 14px;
										color: #858585;
									}
								}

								.goods_3 {
									width: 15vw;
									text-align: right;

									.price {
										color: #ff0000;
									}
								}
							}
						}

						.other {
							padding: 0 0 2vw 0;
							margin: 2vw 0;
							text-align: right;
							border-bottom: 1px solid #f1f1f1;

							text {
								font-size: 14px;
								padding: 0 0 0 2vw;
							}
						}
					}

					.btn {
						text-align: right;

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


			}
		}
	}

	.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>
