<template>
	<view class="flex-item input-wrap">
		<input class="uni-input" maxlength="50" v-model="keyword" @confirm="debounceBtn()" @input="getSelect(pageData)"
			:placeholder="placeholder" />
		<text class="iconfont icon-sousuo3" @tap.stop="debounceBtn()" v-if="isShowIcon"></text>
		<view class="search-list" v-if="searchList.length &&showSelectList">
			<view class="search-item" v-for="(item,index) in searchList" :key="index" @tap.stop="search(item)">
				{{item[keyName]}}
			</view>
			<view class="pageCard" v-if="searchList.length==pageData.size">
				<view class="previous">
					<text v-if="pageData.num !=1" @tap.stop="previous()">
						上一页
					</text>
					<text v-else></text>
				</view>
				<view class="next" v-if="page_count>pageData.num" @tap.stop="next()">
					下一页
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "searchSelect",
		props: {
			// 输入框提示文字
			placeholder: {
				type: String,
				default: "请输入您要搜索的商品名称/编号",
			},
			// 是否显示搜索图标
			isShowIcon: {
				type: Boolean,
				default: true
			},
			// 下拉列表请求地址
			apiUrl: {
				type: String,
				default: '/api/goodssku/getNamePage',
			},
			keyName: {
				type: String,
				default: 'goods_name',
			}
		},
		data() {
			return {
				keyword: "",
				page_count: 0,
				pageData: {
					num: 1,
					size: 10,
				},
				searchList: [],
				showSelectList: false
			};
		},
		methods: {
			getSelect(mescroll) {
				this.$api.sendRequest({
					url: this.apiUrl,
					data: {
						page: mescroll.num,
						page_size: mescroll.size,
						keyword: this.keyword,
					},
					success: res => {
						console.log(res);
						if (this.keyword) {
							this.showSelectList = true
							this.searchList = res.data.list
							this.page_count = res.data.page_count
						} else {
							this.searchList = []
						}
					}
				})
			},
			debounceBtn() {
				uni.$u.debounce(this.search, 1000)
				this.showSelectList = false;
			},
			search(item) {
				if (item) {
					this.showSelectList = false
					this.keyword = item.sku_name;
				}
				this.pageData = {
					num: 1,
					size: 10,
				}
				this.$emit("search", this.keyword);
			},
			previous() {
				uni.$u.debounce(this.previousPage, 2000)
			},
			next() {
				uni.$u.debounce(this.nextPage, 2000)
			},
			previousPage() {
				this.pageData.num--
				this.getSelect(this.pageData)
			},
			nextPage() {
				this.pageData.num++
				this.getSelect(this.pageData)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input-wrap {
		flex: 1;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: $color-bg;
		min-height: 70rpx;
		padding-left: 10rpx;
		border-radius: 70rpx;

		input {
			width: 90%;
			background: $color-bg;
			font-size: $font-size-tag;
			height: 50rpx;
			padding: 10rpx 25rpx 10rpx 40rpx;
			line-height: 50rpx;
			border-radius: 40rpx;
		}

		text {
			font-size: $font-size-toolbar;
			color: $color-tip;
			width: 80rpx;
			text-align: center;
			margin-right: 20rpx;
		}

		.iconfont {
			margin-left: 16rpx;
			font-size: 36rpx;
		}

		.search-list {
			position: absolute;
			top: 100rpx;
			left: 0;
			right: 0;
			background: #fff;
			z-index: 999999 !important;
			border-radius: 20rpx;
			padding: 20rpx;
			box-shadow: 0 10rpx 10rpx rgba(0, 0, 0, 0.1);

			.search-item {
				padding: 10rpx 0;
				border-bottom: 1px solid #eee;

				&:last-child {
					border-bottom: none;

				}
			}

			.search-item:hover {
				background: #eee;
			}

			.pageCard {
				display: flex;
				justify-content: space-between;
				padding-top: 20rpx;
			}

		}
	}
</style>