<template>
	<view :class="[AppTheme]" class="all_box" id="search">

		<view class="search">
			<u-search @clear="clear" v-model="search_value" @search="search" @custom="custom" :value="search_value">
			</u-search>
		</view>

		<view class="f-header m-t">
			<view class="center">
				<view class="line1 bg-primary"></view>
				<view class="tttle text-primary">商品列表</view>
				<view class="line2 bg-primary"></view>
			</view>
		</view>
		<view class="guess-section">
			<view v-for="(item, index) in goodsList" :key="index" class="guess-item" @click="navToDetailPage(item)">
				<u-image :src="item.pic" border-radius="10rpx" width="332rpx" height="340rpx" :lazy-load="true"
					:fade="true" duration="450">

					<view slot="error" style="font-size: 24rpx;">加载失败</view>
				</u-image>
				<view class="border">
					<view class="name">
						<view class="pri_fle">
							<image :src="settingFile.root_img+'/static/app/imgs/home/16.png'" mode="" class="hot">
							</image>
							{{ item.name }}
						</view>
					</view>
					<view class="price_num">
						<text class="price text-neutral">￥{{ item.price }}/{{ item.unit }}</text>
						<text class="oprice">￥{{ item.oprice }}</text>
					</view>
				</view>
			</view>
			
		</view>
		<u-empty margin-top="60" v-if="goodsList.length==0" text="暂无更多数据" mode="list"></u-empty>
	</view>
</template>

<script>
	import homeApi from '@/api/home/index.js';
	export default {
		name: 'Search',
		data() {
			return {
				primary: this.$theme.primary,
				search_value: '',
				goodsList: [],
				settingFile: getApp().globalData.siteinfo,
			};
		},
		onLoad(options) {
			console.log(options)
			this.search_value = options.value ? options.value : this.search_value;
		},
		onShow() {
			console.log(this.search_value)
			homeApi.searchs({
				name: encodeURI(this.search_value)
			}).then(res => {
				if (res.status == 200) {
					this.goodsList = res.data;
				}
			});
		},
		methods: {
			navToDetailPage(item) {
				let id = item.id;
				if (item.type == 0) {
					uni.navigateTo({
						url: `/pagesD/pages/product/product?id=${id}`
					});
				} else if (item.type == 1) {
					this.$u.route('/pagesD/pages/mall/group_cart', {
						id
					})
				} else if (item.type == 2) {
					uni.navigateTo({
						url: `/pagesD/pages/mall/Bargain_cart?id=${id}`
					})
				} else if (item.type == 3) {
					uni.navigateTo({
						url: `/pagesD/pages/mall/convert?id=${id}`
					});
				} else if (item.type == 5) {
					uni.navigateTo({
						url: `/pagesA/pages/adopt/adopt?id=${id}`
					});
				} else {
					this.$api.msg(`数据错误`);
				}
			},
			clear() {
				this.search_value = '';
			},
			search(val) {
				homeApi.searchs({
					name: encodeURI(val)
				}).then(res => {
					console.log(21321312)
					if (res.status == 200) {
						this.goodsList = res.data;
					}
				});
			},
			custom(val) {
				homeApi.searchs({
					name: encodeURI(val)
				}).then(res => {
					console.log(21321312)
					if (res.status == 200) {
						this.goodsList = res.data;
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	#search {
		.f-header {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 90upx;
			width: 100%;

			.center {
				text-align: center;
				display: flex;
				align-items: center;
				color: $tab-color-1;

				.line1,
				.line2 {
					width: 31rpx;
					height: 4rpx;
				}

				.line1 {
					margin-right: 10rpx;
				}

				.line2 {
					margin-left: 10rpx;
				}

				.title {
					font-size: 30px;
					font-family: PingFang SC;
					font-weight: bold;
				}
			}

			image {
				flex-shrink: 0;
				width: 80upx;
				height: 80upx;
				margin-right: 20upx;
			}

			.tit-box {
				flex: 1;
				display: flex;
				flex-direction: column;
			}

			.tit {
				font-size: $font-lg + 2upx;
				color: #font-color-dark;
				line-height: 1.3;
			}

			.tit2 {
				font-size: $font-sm;
				color: $font-color-light;
			}

			.icon-you {
				font-size: $font-lg + 2upx;
				color: $font-color-light;
			}
		}

		.search {
			padding: 20rpx 25rpx;
		}

		.guess-section,
		.guess-section2 {
			display: flex;
			flex-wrap: wrap;
			padding: 0 30upx;
			padding-bottom: 35rpx;
			// justify-content: center;

			.guess-item {
				display: flex;
				flex-direction: column;
				width: 48%;
				padding-bottom: 14rpx;
				// border: 1px solid #e5e5e5;
				box-shadow: $box-shadow;
				border-radius: 15rpx;
				margin-bottom: 28rpx;

				&:nth-child(2n + 1) {
					margin-right: 4%;
				}
			}

			.image-wrapper {
				width: 100%;
				height: 330upx;
				border-radius: 3px;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
					max-height: 330upx;
					max-width: 100%;
					opacity: 1;
					border-radius: 15rpx;
				}
			}

			.border {
				margin-top: 16rpx;
				border-radius: 0px 0px 8rpx 8rpx;
				border-style: none;

				.name {
					width: calc(100% - 20rpx);
					margin: 0 auto;

					.name1 {
						display: flex;
						justify-content: space-around;

						.item {
							width: 138rpx;
							height: 33rpx;
							text-align: center;
							line-height: 33rpx;
							background: #fde8ea;
							font-size: 24rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #ff2630;
							border-radius: 5px;
						}
					}

					.pri_fle {
						width: 100%;
						height: 70rpx;
						display: flex;
						font-size: 26rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #000000;
						line-height: 36rpx;
						width: 100%;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;

						.hot {
							position: relative;
							top: 4rpx;
							width: 34rpx;
							height: 23rpx;
							margin-right: 5rpx;
						}

						.title {
							font-size: 26rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #000000;
							line-height: 36rpx;
							width: 100%;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 2;
						}
					}
				}

				.price_num {
					width: calc(100% - 20rpx);
					margin: 12rpx auto;

					.price {
						font-size: 26rpx;
						font-family: PingFang SC;
						font-weight: bold;
					}

					.oprice {
						margin-left: 20rpx;
						margin-right: 20rpx;
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 400;
						text-decoration: line-through;
						color: #999999;
					}

					.num {
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}
			}
		}
	}
</style>
