<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<input type="text" v-model="searchInfo.goods" @blur="toInput" placeholder="搜索商品">
			</view>
			<view class="two">
				<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">
							<view class="list_1">
								<view class="shopname">
									<text class="iconfont icon-shangdian"></text>
									<text>{{item.shop.name}}</text>
								</view>
							</view>
							<view class="list_2">
								<view class="market">
									<view class="url">
										<image class="image" :src="item.goods.file&&item.goods.file.length>0?item.goods.file[0].url:''" mode=""></image>
									</view>
									<view class="name">
										{{item.goods.name}}
										<view class="specs">
											{{item.zhStatus}}
										</view>
										<view class="specs">
											购买时间：{{item.buy_time}}
										</view>
										<view class="specs">
											备注：{{item.remarks||'暂无'}}
										</view>
									</view>
									<view class="other">
										<view class="price">
											{{item.goods.cost}}
										</view>
										<view class="num">
											×{{item.buy_num}}
										</view>
									</view>
								</view>
							</view>
							<view class="btn">
								<button v-if="item.status=='2'||item.status=='3'" type="default" size="mini" @click="toLogi(item)">查看物流</button>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>{{config.bottom_title}}</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				// 系统设置
				config: {},
				searchInfo: {},
				user: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				// 状态
				statusList: [],
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			};
		},
		onShow: async function() {
			const that = this;
			that.searchConfig();
			await that.searchOther();
			await that.watchLogin();
		},
		onHide: function() {
			const that = this;
			that.clearPage();
		},
		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)
					},
					fail: function(err) {
						console.log(err);
					}
				})
			},
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user)
						that.search()
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let user = that.user;
				if (user._id) {
					let info = {
						skip: that.skip,
						limit: that.limit,
						customer: user._id,
					}
					let res = await that.$api(`/zrOrder`, 'GET', {
						...info,
						...that.searchInfo
					}, `integral`)
					if (res.errcode == '0') {
						let list = [...that.list, ...res.data];
						for (let val of list) {
							let status = that.statusList.find(i => i.value == val.status)
							if (status) val.zhStatus = status.label;
						}
						that.$set(that, `list`, list);
						that.$set(that, `total`, res.total)
					}
				}
			},
			// 查询其他信息
			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)
				}
			},
			// 分页
			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;
				if (e.detail.value == '') {
					that.$set(that, `searchInfo`, {})
				} else {
					that.$set(that.searchInfo, `goods`, e.detail.value);
				}
				that.clearPage();
				that.search();
			},
			// 查看物流
			toLogi(e) {
				uni.navigateTo({
					url: `/pagesMy/logistics/index?id=${e._id}&type=${'integral'}`
				})
			},

			// 清空列表
			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 {
			border-bottom: 1px solid var(--f85Color);
			padding: 2vw;

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

		.two {
			position: relative;
			flex-grow: 1;
			padding: 2vw 0 0 0;

			.list {
				width: 92vw;
				border: 0.5vw solid var(--f1Color);
				margin: 2vw 2vw 0 1.5vw;
				padding: 2vw;
				border-radius: 5px;

				.list_1 {
					margin: 0 0 2vw 0;

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

				.list_2 {
					margin: 0 0 1vw 0;

					.market {
						display: flex;

						.url {
							width: 20vw;

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

							}
						}

						.name {
							display: flex;
							flex-direction: column;
							width: 60vw;
							padding: 0 2vw;
							margin: 0 0 1vw 0;

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



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

							.price {
								color: var(--fFB1Color);
							}
						}
					}
				}

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

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

	.is_bottom {
		text-align: center;

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