<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<input type="text" v-model="searchInfo.name" @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="two_1">
							<view :class="['list',condActive==index?'activeList':'']" v-for="(item,index) in condList" :key="index" @tap="toCond(index,item)">
								<view class="name">
									{{item.name}}
								</view>
								<view class="icon">
									<view class="icon_1">
										<text :class="['iconfont',item.shangActive]" v-if="condActive==index&&shang=='1'"></text>
										<text :class="['iconfont',item.shang]" v-else></text>
									</view>
									<view class="icon_1">
										<text :class="['iconfont', item.xiaActive]" v-if="condActive==index&&xia=='-1'"></text>
										<text :class="['iconfont', item.xia]" v-else></text>
									</view>
								</view>
							</view>
							<view class="screenList" v-if="pid">
								<picker @change="screenChange" name="screen" :value="screen" :range="screenList" range-key="label">
									<view class="uni-input">{{screen_name||'筛选'}}</view>
								</picker>
							</view>
						</view>
						<view class="two_2">
							<view class="pubu">
								<view class="list" v-for="(item,index) in list" :key="index" @tap="toBuy(item)">
									<image class="image" :src="item.file&&item.file.length>0?item.file[0].url:''" mode="">
									</image>
									<view class="sale" v-if="item.is_sale==true">
										<text>已售尽</text>
									</view>
									<view class="name textOver">
										{{item.name}}
									</view>
									<view class="other">
										<view class="money">
											<text>¥</text>
											<text>{{item.sell_money}}</text>
										</view>
										<view class="other_1">
											{{item.view_num||0}}人浏览
										</view>
										<view class="btn" v-if="item.is_sale==false">
											<text class="iconfont icon-gouwuche"></text>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
			<view class="is_bottom" v-if="is_bottom">
				<text>{{config.bottom_title}}</text>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				// 系统设置
				config: {},
				tags: '',
				// 一级id
				pid: '',
				searchInfo: {},
				list: [],
				total: 0,
				page: 0,
				skip: 0,
				limit: 10,
				condActive: 0,
				shang: '',
				xia: '',
				condList: [ // 筛选
				],
				// 筛选
				screenList: [],
				screen: '',
				screen_name: '',
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0
			};
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `tags`, e.tags || '');
			that.$set(that, `pid`, e.pid || '');
			that.searchConfig();
			await that.searchOther();
			await that.search();
		},
		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);
					}
				})
			},
			// 选择二级
			screenChange(e) {
				const that = this;
				let data = that.screenList[e.detail.value];
				if (data) {
					that.$set(that, `tags`, data.code);
					that.$set(that, `screen_name`, data.label);
					that.clearPage();
					that.search();
				}
			},
			async searchOther() {
				const that = this;
				let config = that.$config;
				if (config) {
					// 筛选条件
					that.$set(that, `condList`, config.condList)
				}
				if (that.pid) {
					let info = {};
					info.pid = that.pid;
					const res = await that.$api(`/goodsTags/tree`, 'GET', {
						...info
					})
					if (res.errcode == '0' && res.data.length > 0) {
						that.$set(that, `screenList`, res.data[0].children)
					}
				}
			},
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
					tags: that.tags
				}
				const res = await that.$api(`/viewGoods/indexGoodsList`, `GET`, {
					...info,
					...that.searchInfo
				})
				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() {
				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, `name`, e.detail.value);
				that.clearPage();
				that.search();
			},
			// 筛选
			toCond(index, e) {
				const that = this;
				let condActive = that.condActive;
				that.$set(that, `condActive`, index);
				if (condActive != index && that.xia == '') {
					that.$set(that, `shang`, '0');
					that.$set(that, `xia`, '-1');
				} else if (condActive == index && that.xia == '-1') {
					that.$set(that, `shang`, '1');
					that.$set(that, `xia`, '0');
				} else if (condActive == index && that.shang == '1') {
					that.$set(that, `shang`, '0');
					that.$set(that, `xia`, '-1');
				} else if (condActive = index && that.shang == '1') {
					that.$set(that, `shang`, '0');
					that.$set(that, `xia`, '-1');
				}
				let value;
				if (index != 0) {
					value = that.shang == '0' ? that.xia : that.shang;
				}
				if (index == 1) {
					that.$set(that, `searchInfo`, {})
					that.$set(that.searchInfo, `sell_num`, value);
				} else if (index == 2) {
					that.$set(that, `searchInfo`, {})
					that.$set(that.searchInfo, `sell_money`, value);
				} else if (index == 3) {
					that.$set(that, `searchInfo`, {})
					that.$set(that.searchInfo, `view_num`, value);
				}
				that.clearPage();
				that.search();
			},
			toBuy(e) {
				uni.navigateTo({
					url: `/pagesHome/order/detail?id=${e.id||e._id}`
				})
			},
			// 清空列表
			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;

			.two_1 {
				background-color: var(--fffColor);
				padding: 2vw;
				display: flex;
				flex-direction: row;
				justify-content: space-around;

				.list {
					display: flex;
					flex-direction: row;

					.icon {
						position: relative;
						top: -5px;
						left: 2px;

						.icon_1 {
							height: 10px;

							.iconfont {
								font-size: 12px;
							}
						}
					}
				}

				.activeList {
					.name {
						color: #ff0000;
					}

				}

				.screenList {
					width: 17vw;
					display: inline-block;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;

					.uni-input {
						text-align: center;
					}
				}
			}

			.two_2 {
				display: flex;
				flex-direction: column;
				padding: 2vw;
				background-color: var(--f1Color);

				.pubu {
					// column-count: 2;
					// column-gap: 2vw;
					display: flex;
					justify-content: space-between;
					flex-wrap: wrap;

					.list {
						width: 43vw;
						// break-inside: avoid;
						position: relative;
						background-color: var(--fffColor);
						padding: 2vw;
						margin: 0 0 2vw 0;
						border-radius: 9px;

						.image {

							width: 100%;
							height: 40vw;
						}

						.sale {
							position: absolute;
							top: 18vw;
							text-align: center;
							width: 43vw;

							text {
								background-color: #0000005f;
								border-radius: 90px;
								display: inline-block;
								width: 15vw;
								height: 15vw;
								color: var(--fffColor);
								text-align: center;
								line-height: 15vw;
							}
						}

						.name {
							font-size: var(--font16Size);
							margin: 0 0 1vw 0;
						}

						.other {
							display: flex;
							flex-direction: row;
							justify-content: space-between;

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

								text:nth-child(1) {
									font-size: var(--font12Size);
								}
							}

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

								text {
									font-weight: bold;
									padding: 0 2vw;
									color: var(--ff0Color);
								}
							}

							.btn {
								button {
									font-size: var(--font20Size);
								}
							}
						}
					}
				}
			}
		}
	}

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