<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<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="one_1">
									<input type="text" v-model="searchInfo.name" @blur="toInput" placeholder="搜索商品名称">
								</view>
								<view class="one_2">
									<view class="list" v-for="(item, index) in list" :key="index"
										@click="toShare(item)">
										<view class="list_1" v-if="tabs.active=='0'">
											<view class="list_1_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_1_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>
														<text v-if="tag.gift.length>0" class="gift">赠品</text>
														<text v-if="tag.sp_price" class="act">特价</text>
													</view>
													<view class="goods_3">
														<view v-if="item.type=='0'" class="price">
															¥{{tag.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 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'||item.status=='2-'"
													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.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="list_2" v-else-if="tabs.active=='-1'">
											<view class="list_2_1">
												<view class="shopname">
													<text class="iconfont icon-shangdian"></text>
													<text>{{item.shop.name}}</text>
												</view>
												<view class="type">
													{{item.zhType||'暂无'}}
												</view>
											</view>
											<view class="list_2_2">
												<image class="image"
													:src="item.url&&item.url.length>0?item.url[0].url:''" mode="">
												</image>
												<view class="other">
													<view class="name textOver">
														{{item.goods.goods.name||'暂无'}}
													</view>
													<view class="other_1">
														商品规格：<text>{{item.goods.name||'暂无'}}</text>
													</view>
													<view class="other_1" v-if="item.type!='3'">
														退款：<text>¥{{item.money||0}}</text>
													</view>
													<view class="other_1">
														申请时间：<text>{{item.apply_time||'暂无'}}</text>
													</view>
													<view class="other_1">
														售后类型：<text>{{item.zhStatus||'暂无'}}</text>
													</view>
												</view>
											</view>
											<view class="btn">
												<button type="default" size="mini"
													@tap.stop="toView(item)">详细信息</button>
												<button v-if="item.status=='0'" type="default" size="mini"
													@tap.stop="toCancel(item)">取消售后</button>
												<button
													v-if="item.type!='1'&&item.status=='2'||item.status=='3'&&!item.transport.shop_receive"
													type="default" size="mini"
													@tap.stop="toMaintain(item)">维护单号</button>
												<button
													v-if="item.type=='3'&&item.status=='3'&&item.transport.shop_receive==true"
													type="default" size="mini" @tap.stop="toLogi(item)">查看物流</button>
												<button
													v-if="item.type=='3'&&item.status=='3'&&item.transport.shop_receive==true&&!item.transport.customer_receive"
													type="default" size="mini" @tap.stop="toConfirm(item)">确认收货</button>
											</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="type">
													{{item.zhStatus||'暂无'}}
												</view>
											</view>
											<view class="list_2_2">
												<view class="other">
													<view class="name textOver">
														{{item.goods_name||'暂无'}}
													</view>
													<view class="other_1">
														开始时间：<text>{{item.start_time||'暂无'}}</text>
													</view>
													<view class="other_1">
														结束时间：<text>¥{{item.end_time||'暂无'}}</text>
													</view>
													<view class="other_1">
														人数限制：<text>{{item.person_limit||'暂无'}}</text>
													</view>
												</view>
											</view>
											<view class="btn">
												<button type="default" size="mini"
													@tap.stop="toView(item)">详细信息</button>
											</view>
										</view>
									</view>
								</view>
							</view>
						</scroll-view>
					</view>
				</tabs>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	import moment from 'moment';
	import tabs from '@/components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				user: {},
				searchInfo: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				statusList: [],
				tabs: {
					active: '0',
					menu: [{
							title: '团购订单',
							active: '0'
						},
						{
							title: '团购售后',
							active: '-1'
						}
					]
				},
				// 订单状态
				ostatusList: [],
				// 售后状态
				astatusList: [],
				// 团状态
				gstatusList: [],
				// 售后类型
				typeList: [],
			};
		},
		onShow: async function() {
			const that = this;
			await that.searchOther();
			await that.watchLogin();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search(that.tabs.active)
			uni.stopPullDownRefresh();
		},
		methods: {
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: async function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user);
						if (user.is_leader == '0' && that.tabs.menu.length <= 2) {
							that.tabs.menu.push({
								title: '团长处理',
								active: '1'
							})
						}
						that.search(that.tabs.active)
					},
					fail: function(err) {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				})
			},
			// 查询列表
			async search(active) {
				const that = this;
				let user = that.user;
				if (user._id) {
					let info = {
						skip: that.skip,
						limit: that.limit,
					}
					let res;
					if (active == '0') {
						info.customer = user._id;
						res = await that.$api(`/orderDetail`, 'GET', {
							...info,
							...that.searchInfo
						})
						if (res.errcode == '0') {
							let list = [...that.list, ...res.data];
							for (let val of list) {
								let status = that.ostatusList.find(i => i.value == val.status)
								if (status) val.zhStatus = status.label;
							}
							that.$set(that, `list`, list);
							that.$set(that, `total`, res.total)
						}
					} else if (active == '-1') {
						info.customer = user._id;
						res = await that.$api(`/afterSale`, 'GET', {
							...info,
							...that.searchInfo
						})
						if (res.errcode == '0') {
							let list = [...that.list, ...res.data];
							for (let val of list) {
								let type = that.typeList.find(i => i.value == val.type)
								if (type) val.zhType = type.label;
								let status = that.astatusList.find(i => i.value == val.status)
								if (status) val.zhStatus = status.label;
								val.url = val?.goods?.goods?.file;
							}
							that.$set(that, `list`, list);
							that.$set(that, `total`, res.total)
						}
					} else {
						info.leader = user._id;
						res = await that.$api(`/group`, 'GET', {
							...info,
							...that.searchInfo
						}, 'group')
						if (res.errcode == '0') {
							let list = [...that.list, ...res.data];
							for (let val of list) {
								let status = that.gstatusList.find(i => i.value == val.status)
								if (status) val.zhStatus = status.label;
							}
							that.$set(that, `list`, list);
							that.$set(that, `total`, res.total)
						}
					}
				}
			},
			// 分页
			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 {}
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (e.detail.value) that.$set(that.searchInfo, `name`, e.detail.value);
				that.clearPage();
				that.search();
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.clearPage();
				that.search(e.active)
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询状态
				res = await that.$api(`/dictData`, 'GET', {
					code: 'order_process'
				})
				if (res.errcode == '0') that.$set(that, `ostatusList`, res.data);
				res = await that.$api(`/dictData`, 'GET', {
					code: "afterSale_type"
				});
				if (res.errcode == '0') that.$set(that, `typeList`, res.data)
				res = await that.$api(`/dictData`, 'GET', {
					code: "afterSale_status"
				});
				if (res.errcode == '0') that.$set(that, `astatusList`, res.data)
				res = await that.$api(`/dictData`, 'GET', {
					code: "group_status"
				});
				if (res.errcode == '0') that.$set(that, `gstatusList`, res.data)

			},
			// 分享
			toShare(e) {
				const that = this;
				if (that.tabs.active == '1') {
					uni.navigateTo({
						url: `/pagesHome/group/share?id=${e.goods}`
					})
					that.clearPage();
				}
			},
			// 清空列表
			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 {
			position: relative;
			flex-grow: 1;
			padding: 2vw 0 0 0;

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

				.one_1 {
					padding: 2vw;

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

				.one_2 {
					.list {
						background-color: #fff;
						border: 1px solid var(--f5Color);
						padding: 2vw;
						margin: 0 2vw 2vw 2vw;
						border-radius: 5px;

						.list_1 {
							.list_1_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_1_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;
								}
							}
						}

						.list_2 {
							.list_2_1 {
								display: flex;
								justify-content: space-between;
								font-size: var(--font16Size);
								margin: 0 0 2vw 0;

								.shopname {
									text {
										margin: 0 0 0 1vw;
									}
								}

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

							.list_2_2 {
								display: flex;
								justify-content: space-between;

								.image {
									width: 25vw;
									height: 25vw;
									margin: 0 2vw 1vw 0;
									border: 1px solid var(--f85Color);
								}

								.other {
									width: 65vw;
									flex-grow: 1;

									.name {
										font-size: var(--font16Size);
									}

									.other_1 {
										font-size: var(--font14Size);

										text {
											color: var(--f85Color);
										}
									}
								}
							}

							.btn {
								text-align: right;

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

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

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