<template>
	<u-popup
		v-model="show"
		mode="bottom"
		length="90%"
		:mask-close-able="false"
		closeable
		@close="handleClose"
	>
		<view class="u-wrap">
			<!-- 搜索 -->
			<view
				style="width: 90%; height: 50px; position: fixed; top: 0; z-index: 1; background-color: #fff;"
			>
				<uni-search-bar
					placeholder="输入搜索商品"
					bgColor="#EEEEEE"
					clearButton="auto"
					cancelButton="none"
					@confirm="search"
				/>
			</view>
			<view class="u-menu-wrap">
				<scroll-view
					scroll-y
					scroll-with-animation
					class="u-tab-view menu-scroll-view"
					:scroll-top="scrollTop"
				>
					<view
						v-for="(item, index) in categoryList"
						:key="item.id"
						class="u-tab-item"
						:class="[current == index ? 'u-tab-item-active' : '']"
						:data-current="index"
						@tap.stop="swichMenu(index, item.id)"
					>
						<text class="u-line-1">{{ item.name }}</text>
					</view>
				</scroll-view>
				<view class="right-cates">
					<block v-for="(item, index) in currentSubCategoryData" :key="item.id">
						<view class="item-container" @tap="cateGoryGoods(item)">
							<u-image
								width="100rpx"
								height="100rpx"
								:src="
									item.picUrl.split('http').length > 1
										? item.picUrl
										: baseUrl + item.picUrl
								"
							></u-image>
							<view class="content">
								<view class="title">
									<text>{{ item.name }}</text>
									<text style="color: #646566;">{{ item.brief }}</text>
								</view>
							</view>
						</view>
					</block>
				</view>
			</view>
		</view>
	</u-popup>

	<search-popup
		ref="searchPopupRef"
		:show="showSearchPopup"
		@search-close="searchClose"
		@cateGoryGoods="cateGoryGoods"
	/>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { getCatalogIndex, getGoodsList } from '@/api/category';
import config from '@/utils/config';
import searchPopup from './searchPopup.vue';

let baseUrl = config.baseUrl;

const scrollTop = ref(0); //tab标题的滚动条位置
const current = ref(0); // 预设当前项的值
const menuHeight = ref(0); // 左边菜单的高度
const menuItemHeight = ref(0); // 左边菜单item的高度
const categoryList = ref([] as any); //右侧tabber数据
const currentSubCategoryData = ref([] as any); //初始tabber数据
const listQuery = ref({
	categoryId: 0,
	page: 1,
	limit: 10,
});
let status = ref('loadmore');
let total = ref(0);
let emits = defineEmits(['close', 'categoryData']);
const showSearchPopup = ref(false);
const searchPopupRef = ref();

let props = defineProps({
	show: {
		type: Boolean,
		default: false,
	},
});

const search = (val: any) => {
	// if (!val.value) return;
	searchPopupRef.value.searchQuery.keyword = val.value;
	searchPopupRef.value.initList();
	showSearchPopup.value = true;
};

const cateGoryGoods = (row: any) => {
	emits('categoryData', row);
	handleClose();
};

// 点击左边的栏目切换
const swichMenu = async (index: any, id: number) => {
	currentSubCategoryData.value = [];
	current.value = index;
	listQuery.value.categoryId = categoryList.value[index].id;
	initGoodsList(listQuery.value);
	// 如果为0，意味着尚未初始化
	if (menuHeight.value == 0 || menuItemHeight.value == 0) {
		await getElRect('menu-scroll-view', 'menuHeight');
		await getElRect('u-tab-item', 'menuItemHeight');
	}
	// 将菜单菜单活动item垂直居中
	scrollTop.value =
		index * menuItemHeight.value + menuItemHeight.value / 2 - menuHeight.value / 2;
};

// 获取一个目标元素的高度
const getElRect = (elClass?: any, dataVal?: any) => {
	new Promise((resolve: any, reject: any) => {
		const query = uni.createSelectorQuery().in(this);
		query
			.select('.' + elClass)
			.fields({ size: true }, (res: any) => {
				// 如果节点尚未生成，res值为null，循环调用执行
				if (!res) {
					setTimeout(() => {
						getElRect(elClass);
					}, 10);
					return;
				}
				dataVal = res.height;
			})
			.exec();
	});
};

const initIndex = () => {
	getCatalogIndex().then((res: any) => {
		if (res.data.code === 200) {
			categoryList.value = res.data.data.categoryList;
			if (listQuery.value.categoryId === 0)
				listQuery.value.categoryId = categoryList.value[0].id;
			initGoodsList(listQuery.value);
			// console.log(categoryList.value, '分类数据');
		}
	});
};

const initGoodsList = (data: any) => {
	status.value = 'loading';
	getGoodsList(data).then((res: any) => {
		if (res.data.code === 200) {
			if (res.data.data.total > 10) status.value = 'loadmore';
			else status.value = 'nomore';
			total.value = res.data.data.total;
			currentSubCategoryData.value = [...currentSubCategoryData.value, ...res.data.data.list];
		}
	});
};
let handleClose = () => {
	emits('close', false);
};

const searchClose = () => {
	showSearchPopup.value = false;
};

defineExpose({
	initIndex,
	currentSubCategoryData,
});
</script>

<style lang="scss" scoped>
.right-cates {
	width: 100%;
	height: 100%;
	background-color: #fff;
	padding: 0 24rpx;
	.item-container {
		box-sizing: border-box;
		width: 100%;
		height: 160rpx;
		padding: 12rpx 0;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		border-bottom: 1rpx solid #f5f5f5;
		.content {
			height: 100%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			margin-left: 20rpx;
			.title {
				display: flex;
				flex-direction: column;
			}
			.price {
				.new-price {
					font-size: 32rpx;
				}
				.old-price {
					margin-left: 10rpx;
					color: #969799;
					font-size: 20rpx;
					text-decoration: line-through;
				}
			}
		}
	}
}

.u-wrap {
	height: calc(100vh);
	/* #ifdef H5 */
	height: calc(100vh - var(--window-top));
	/* #endif */
	display: flex;
	flex-direction: column;
}

.u-search-box {
	padding: 18rpx 30rpx;
}

.u-menu-wrap {
	flex: 1;
	display: flex;
	overflow: hidden;
	margin-top: 100rpx;
}

.u-search-inner {
	background-color: rgb(234, 234, 234);
	border-radius: 100rpx;
	display: flex;
	align-items: center;
	padding: 10rpx 16rpx;
}

.u-search-text {
	font-size: 26rpx;
	color: $u-tips-color;
	margin-left: 10rpx;
}

.u-tab-view {
	width: 200rpx;
	height: 100%;
}

.u-tab-item {
	height: 110rpx;
	background: #f6f6f6;
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 26rpx;
	color: #444;
	font-weight: 400;
	line-height: 1;
	padding-left: 20rpx;
}

.u-tab-item-active {
	position: relative;
	color: #000;
	font-size: 30rpx;
	font-weight: 600;
	background: #fff;
	padding-left: 20rpx;
}

.u-tab-item-active::before {
	content: '';
	position: absolute;
	border-left: 4px solid $u-type-primary;
	height: 32rpx;
	left: 0;
	top: 39rpx;
}

.u-tab-view {
	height: 100%;
}

.right-box {
	background-color: rgb(250, 250, 250);
}

.page-view {
	padding: 16rpx;
}

.class-item {
	margin-bottom: 30rpx;
	background-color: #fff;
	padding: 16rpx;
	border-radius: 8rpx;
}

.item-title {
	font-size: 26rpx;
	color: $u-main-color;
	font-weight: bold;
}

.item-menu-name {
	font-weight: normal;
	font-size: 24rpx;
	color: $u-main-color;
}

.item-container {
	display: flex;
	flex-wrap: wrap;
}

.thumb-box {
	width: 33.333333%;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
	margin-top: 20rpx;
}

.item-menu-image {
	width: 120rpx;
	height: 120rpx;
}
</style>
