<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<view class="one_1" v-if="barActive=='0'">
					<scroll-view scroll-y="true" class="scroll-view">
						<view class="one_1_1">
							<image class="image" :src="info.logo&&info.logo.length>0?info.logo[0].url:''" mode="">
							</image>
						</view>
						<view class="one_1_2">
							<view class="pubu">
								<view class="list" v-for="(item,index) in shoplist" :key="index" @tap="toBuy(item)">
									<image class="image" :src="item.file&&item.file.length>0?item.file[0].url:''"
										mode=""></image>
									<view class="name textOver">
										{{item.name}}
									</view>
									<view class="other">
										<view class="money">
											<view class="money_1">
												<text>¥</text><text>{{item.sell_money||0}}</text>
											</view>
											<view class="money_2">
												<text>¥</text><text>{{item.flow_money||0}}</text>
											</view>
										</view>
									</view>
									<view class="other" v-if="item.p_act">
										<text class="act" v-for="(tag,index) in item.p_act" :key="index">{{tag}}</text>
									</view>
									<view class="other" v-if="item.leader_price">
										<view class="leader">
											<text>团长价</text><text>¥</text><text>{{item.leader_price||0}}</text>
										</view>
									</view>
									<view class="acttags">
										<text v-for="i in item.actTagsShow" :key="i">{{i.label}}</text>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
				<view class="one_3" v-else-if="barActive=='1'">
					<view class="first">
						<input type="text" v-model="searchInfo.name" @blur="toInput" placeholder="搜索商品">
					</view>
					<view class="second">
						<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
							<view class="list-scroll-view">
								<view class="second_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>
								<view class="second_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">
													<view class="money_1">
														<text>¥</text><text>{{item.sell_money||0}}</text>
													</view>
													<view class="money_2">
														<text>¥</text><text>{{item.flow_money||0}}</text>
													</view>
												</view>
											</view>
											<view class="other">
												<text class="act" v-for="(tag,index) in item.p_act"
													:key="index">{{tag}}</text>
											</view>
											<view class="other" v-if="item.leader_price">
												<view class="leader">
													<text>团长价</text><text>¥</text><text>{{item.leader_price||0}}</text>
												</view>
											</view>
											<view class="acttags">
												<text v-for="i in item.actTagsShow" :key="i">{{i.label}}</text>
											</view>
										</view>
									</view>
								</view>
								<view class="is_bottom" v-if="is_bottom">
									<text>{{config.bottom_title}}</text>
								</view>
							</view>
						</scroll-view>
					</view>
				</view>
				<view class="one_4" v-else-if="barActive=='2'">
					<view class="first">
						<image class="image" :src="info.logo&&info.logo.length>0?info.logo[0].url:''" mode=""></image>
						<view class="name">{{info.name}}</view>
					</view>
					<view class="second">
						<view class="second_1">
							<view class="grade">
								<view>{{info.goods_score||5}}</view>
								商品
							</view>
							<view class="grade">
								<view>{{info.send_score||5}}</view>
								发货
							</view>
							<view class="grade">
								<view>{{info.service_score||5}}</view>
								服务
							</view>
						</view>
						<view class="qrcode_1">
							<uqrcode ref="uqrcode" canvas-id="qrcode" :value="uqrcodeInfo"
								:options="{ margin:10,backgroundColor:'#f1f1f1' }">
							</uqrcode>
							<view class="txt">
								店铺二维码
							</view>
						</view>
					</view>
					<view class="collect">
						<text v-if="!collection" @click="toCollect" class="iconfont icon-yduishoucangkongxin"></text>
						<text v-else @click="toCollect" class="iconfont icon-yduishoucangshixin"></text>
					</view>
				</view>
			</view>
			<view class="two">
				<view class="list" v-for="(item,index) in barList" :key="index" @tap="barChange(index,item)">
					<view class="icon">
						<text :class="['iconfont',barActive==index?item.acticon:item.icon]"></text>
					</view>
					<view :class="['name',barActive==index?'activename':'']">
						{{item.name}}
					</view>
				</view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				// 系统设置
				config: {},
				user: {},
				id: '',
				barActive: '0',
				barList: [ //底部菜单
					{
						icon: 'icon-shangdian',
						acticon: "icon-shangdian-copy",
						name: '微店首页'
					},
					{
						icon: 'icon-shangpinfenlei',
						acticon: "icon-shangpinfenlei-copy",
						name: '全部商品'
					},
					{
						icon: 'icon-qiyejianjie',
						acticon: "icon-qiyejianjie-copy",
						name: '店铺简介'
					}
				],
				// 店铺信息
				info: {},
				// 店铺部分商品
				shoplist: [],
				// 收藏
				collection: false,
				// 全部商品
				searchInfo: {},
				list: [],
				total: 0,
				page: 0,
				skip: 0,
				limit: 10,
				condActive: 0,
				shang: '',
				xia: '',
				condList: [ // 筛选
				],
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 二维码内容
				uqrcodeInfo: ''
			};
		},
		onLoad: async function(e) {
			const that = this;
			let id = '';
			if (e && e.id) id = e.id;
			else id = decodeURIComponent(e.q).split('id=')[1];
			that.$set(that, `id`, id);
			that.searchConfig();
			await that.searchOther();
			await that.watchLogin();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPages();
			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);
					}
				})
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let config = that.$config;
				if (config) {
					// 筛选条件
					that.$set(that, `condList`, config.condList)
				}
				let user = that.user;
				let shop = that.info;
				if (user && user._id && shop && shop._id) {
					// 商铺是否收藏
					let arr = await that.$api(`/storeShop/check`, `GET`, {
						customer: user._id,
						shop: shop._id
					});
					if (arr.errcode == '0') {
						that.$set(that, `collection`, arr.data)
					}
				}
			},
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user);
					},
					fail: function(err) {}
				})
			},
			// 查询信息
			async search() {
				const that = this;
				if (that.id) {
					// 查询店铺信息
					const res = await that.$api(`/shop/${that.id}`, 'GET');
					if (res.errcode == '0') that.$set(that, `info`, res.data);
					let arr = await that.$api(`/storeShop/check`, `GET`, {
						customer: that.user._id,
						shop: that.id
					});
					if (arr.errcode == '0') {
						that.$set(that, `collection`, arr.data)
					}
					// 查询店铺商品
					that.searchShopMarket();
					// 查询全部商品
					that.searchAll();
				}
			},
			// 查询店铺商品
			async searchShopMarket() {
				const that = this;
				const res = await that.$api(`/viewGoods/indexGoodsList`, 'GET', {
					shop: that.id,
					skip: 0,
					limit: 20
				})
				if (res.errcode == '0') that.$set(that, `shoplist`, res.data);
			},
			// 查询全部产品
			async searchAll() {
				const that = this;
				let info = {
					shop: that.id,
					skip: that.skip,
					limit: that.limit
				}
				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,
					});
				}
			},
			// 创建二维码
			createQrcode() {
				const that = this;
				if (that.id) {
					const url = `${that.$config.serverUrl}/shopinfo?id=${that.id}`;
					that.$set(that, `uqrcodeInfo`, url);
					that.$refs.uqrcode.make({
							enableDelay: true
						})
						.then(res => {
							console.log(res);
						})
						.finally(() => {
							console.log('2');
						})
				}
			},
			// 购买
			toBuy(item) {
				if (item && item._id) {
					uni.navigateTo({
						url: `/pagesHome/order/detail?id=${item._id}`
					})
				}
			},
			// 全部产品-分页
			async 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.searchAll();
					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);
				that.clearPage();
				that.searchAll();
			},
			// 筛选
			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.searchAll();
			},
			// 收藏
			async toCollect() {
				const that = this;
				let user = that.user;
				let shop = that.info;
				if (user && user._id && shop && shop._id) {
					let res = await that.$api(`/storeShop`, `POST`, {
						customer: user._id,
						shop: shop._id
					});
					if (res.errcode == '0') {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
						that.$set(that, `collection`, res.data.result)
					}
				} else {
					uni.showToast({
						title: '缺少必要信息，无法收藏',
						icon: 'none'
					})
				}
			},
			// 选择底部菜单
			barChange(index, item) {
				const that = this;
				that.$set(that, `barActive`, index);
				uni.setNavigationBarTitle({
					title: item.name
				});
				// 店铺简介时,绘制二维码
				if (index == '2') that.createQrcode();
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			},
			// 清空总列表
			clearPages() {
				const that = this;
				that.$set(that, `info`, {})
				that.$set(that, `shoplist`, [])
				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;

			.one_1 {
				height: 92vh;
				background-color: var(--fFB1Color);

				.one_1_1 {
					padding: 2vw;

					.image {
						width: 100%;
						height: 50vw;
					}
				}

				.one_1_2 {
					padding: 0 2vw 2vw 2vw;

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

						.list {
							// break-inside: avoid;
							position: relative;
							width: 43vw;
							background: #fff;
							padding: 2vw;
							border-radius: 5px;
							margin: 0 0 2vw 0;


							.image {
								width: 100%;
								height: 50vw;
							}

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

							.other {
								display: flex;
								flex-direction: row;
								padding: 1vw 0 0 0;

								.money {
									display: flex;

									.money_1 {
										color: var(--fFB1Color);
										font-size: 12px;

										text:last-child {
											font-size: 16px;
											padding: 0 0 0 1vw;
										}
									}

									.money_2 {
										font-size: 12px;
										margin: 0 0 0 2vw;
										color: var(--f99Color);

										text {
											text-decoration: line-through;
										}

										text:last-child {
											font-size: 16px;
											padding: 0 0 0 1vw;
										}
									}
								}
								.leader {
									font-size: 12px;
									text-align: center;
									color: #23B67A;
								
									text:last-child {
										font-size: 16px;
										padding: 0 0 0 1vw;
									}
								}

								.act {
									font-size: 12px;
									color: #FFA500;
									border: 1px solid #FFA500;
									border-radius: 5px;
									padding: 0 1vw;
									margin: 0 1vw 0 0;
								}
							}

							.acttags {
								position: absolute;
								top: 2vw;
								width: 93%;

								text {
									display: inline-block;
									background-color: #ff0000;
									color: #fff;
									border-radius: 1vw;
									padding: 0.5vw;
									font-size: 12px;

									margin: 0 1vw 0 0;
								}
							}
						}
					}
				}
			}

			.one_2 {
				height: 91vh;
				display: flex;
				flex-direction: row;
				padding: 2vw;

				.first_1 {
					position: relative;
					width: 25vw;
					background-color: #fafafa;
					display: flex;
					flex-direction: column;

					.list {
						text-align: center;
						padding: 2.5vw 0;
						border-bottom: 1px solid var(--f1Color);

						text {
							font-size: var(--font14Size);
						}
					}

					.listActive {
						background-color: var(--fffColor);
					}
				}

				.first_2 {
					padding: 2vw;
					flex-grow: 1;
					position: relative;
					display: flex;
					flex-direction: column;

					.list {
						margin: 0 0 2vw 0;
						padding: 2vw;

						.title {
							font-size: var(--font16Size);
							margin: 0 0 2vw 0;
						}

						.market {
							display: flex;
							flex-direction: row;
							flex-wrap: wrap;

							.marketList {
								text-align: center;
								margin: 0 2vw 2vw 0;
								width: 22vw;

								.image {
									width: 100%;
									height: 15vw;
								}

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

							.marketList:nth-child(3n) {
								margin: 0 0 2vw 0;
							}
						}
					}
				}
			}

			.one_3 {
				display: flex;
				flex-direction: column;
				width: 100vw;
				height: 92vh;

				.first {
					border-bottom: 1px solid var(--f85Color);
					padding: 2vw;

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

				.second {
					position: relative;
					flex-grow: 1;

					.second_1 {
						width: 96vw;
						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;
							}

						}
					}

					.second_2 {
						padding: 2vw;
						background-color: var(--f1Color);

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

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


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

								.other {
									display: flex;
									flex-direction: row;
									padding: 1vw 0 0 0;

									.money {
										display: flex;

										.money_1 {
											color: var(--fFB1Color);
											font-size: 12px;

											text:last-child {
												font-size: 16px;
												padding: 0 0 0 1vw;
											}
										}

										.money_2 {
											font-size: 12px;
											margin: 0 0 0 2vw;
											color: var(--f99Color);

											text {
												text-decoration: line-through;
											}

											text:last-child {
												font-size: 16px;
												padding: 0 0 0 1vw;
											}
										}
									}
									.leader {
										font-size: 12px;
										text-align: center;
										color: #23B67A;
									
										text:last-child {
											font-size: 16px;
											padding: 0 0 0 1vw;
										}
									}
									

									.act {
										font-size: 12px;
										color: #FFA500;
										border: 1px solid #FFA500;
										border-radius: 5px;
										padding: 0 1vw;
										margin: 0 1vw 0 0;
									}
								}

								.acttags {
									position: absolute;
									top: 2vw;
									width: 93%;

									text {
										display: inline-block;
										background-color: #ff0000;
										color: #fff;
										border-radius: 1vw;
										padding: 0.5vw;
										font-size: 12px;

										margin: 0 1vw 0 0;
									}
								}
							}
						}
					}
				}
			}

			.one_4 {
				.first {
					display: flex;
					flex-direction: column;
					align-items: center;
					background-color: var(--fFB1Color);
					border-bottom-right-radius: 10vw;
					border-bottom-left-radius: 10vw;

					.image {
						width: 20vw;
						height: 20vw;
						border-radius: 20vw;
					}

					.name {
						margin: 2vw 0;
						font-size: var(--font16Szie);
						font-weight: bold;
						color: var(--mainColor);
					}
				}

				.second {
					margin: 2vw 0;
					position: relative;

					.second_1 {
						display: flex;
						justify-content: space-evenly;

						.grade {
							display: flex;
							flex-direction: column;
							align-items: center;
							color: var(--f85Color);
							font-size: var(--font13Size);

							view {
								margin: 1vw 0 0 0;
								color: var(--ff0Color);
							}
						}
					}

					.qrcode_1 {

						.uqrcode {
							top: 10vw;
							left: 20%;

						}

						.txt {
							margin: 2vw 0 0 0;
							position: absolute;
							text-align: center;
							width: 100vw;
							bottom: -22vw;
						}

					}

				}
			}

			.collect {
				position: fixed;
				top: 0;
				right: 2vw;

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

		.two {
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			border-top: 1px solid #f1f1f1;

			.list {
				padding: 1vw 0;
				text-align: center;

				.icon {}

				.name {
					font-size: 12px;
				}

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

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

		.list-scroll-view {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
		}
	}

	.is_bottom {
		text-align: center;
		width: 100%;

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