<template>
	<view class="all-services-page">
		<nav-bar title="全部分类" :showBack="false"></nav-bar>
		<!-- 顶部搜索栏 -->
		<view class="header-search">
			<view class="search-input-wrap">
				<uni-icons type="search" size="14" color="#999"></uni-icons>
				<input class="search-input" v-model="searchValue" placeholder="搜索服务内容"
					placeholder-class="placeholder-style" />
			</view>
		</view>

		<!-- 主体分类区域 -->
		<view class="main-content">
			<!-- 左侧侧边栏 -->
			<scroll-view scroll-y class="sidebar">
				<view v-for="(item, index) in allData" :key="index"
					:class="['sidebar-item', { active: activeSidebar === index }]" @click="activeSidebar = index">
					<text>{{ item.title }}</text>
				</view>
			</scroll-view>

			<!-- 右侧服务列表 -->
			<scroll-view scroll-y class="content-view">
				<view class="category-section" v-for="(cat, index) in currentCategories" :key="index">
					<view class="service-grid">
						<view class="service-cell" v-for="(service, sIndex) in cat.items" :key="sIndex"
							@click="onServiceClick(service)">
							<view class="icon-wrapper">
								<image :src="service.icon" class="service-icon" mode="aspectFit"></image>
							</view>
							<text class="service-name">{{ service.name }}</text>
						</view>
					</view>
				</view>

			</scroll-view>
		</view>

		<custom-tabbar></custom-tabbar>
	</view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import navBar from '@/components/nav-bar/index.vue'
import customTabbar from '@/components/custom-tabbar/index.vue'
import { listAll as getClassifications } from '@/api/service/classification'
import { listAll as getServices } from '@/api/service/list'

const searchValue = ref('')
const activeSidebar = ref(0)
const allData = ref([])
const currentCategories = computed(() => {
	if (allData.value.length === 0 || !allData.value[activeSidebar.value]) return []
	return allData.value[activeSidebar.value].categories
})

const loadData = async () => {
	try {
		const [classifications, services] = await Promise.all([
			getClassifications(),
			getServices()
		])

		console.log('分类数据:', classifications)
		console.log('服务数据:', services)

		allData.value = classifications.map(classification => {
			const categoryServices = services.filter(
				service => service.classificationId === classification.id
			)

			console.log(`分类 ${classification.name} 的服务:`, categoryServices)

			return {
				title: classification.name,
				categories: [{
					items: categoryServices.map(service => ({
						...service,
						name: service.name,
						icon: service.iconUrl,
						type: service.id
					}))
				}]
			}
		})

		console.log('最终数据结构:', allData.value)
	} catch (error) {
		console.error('加载服务数据失败:', error)
		uni.showToast({ title: '加载失败', icon: 'none' })
	}
}

onMounted(() => {
	loadData()
})

const onServiceClick = (service) => {
	if (service.id) {
		uni.setStorageSync('currentService', service)
		uni.navigateTo({ url: `/pages/service/detail/index?serviceId=${service.id}` })
	} else {
		uni.showToast({ title: service.name + ' 功能即将上线', icon: 'none' })
	}
}
</script>

<style lang="scss" scoped>
.all-services-page {
	height: 100vh;
	display: flex;
	flex-direction: column;
	background-color: #fff;
}

.header-search {
	padding: 16rpx 32rpx;
	background-color: #fff;
	border-bottom: 1rpx solid #f2f2f2;
}

.search-input-wrap {
	flex: 1;
	display: flex;
	align-items: center;
	background: #f5f5f5;
	border-radius: 32rpx;
	padding: 12rpx 20rpx;
	gap: 12rpx;
}

.search-input {
	flex: 1;
	font-size: 26rpx;
}

.placeholder-style {
	color: #999;
	font-size: 26rpx;
}

.main-content {
	flex: 1;
	display: flex;
	overflow: hidden;
}

.sidebar {
	width: 200rpx;
	background-color: #f7f8fa;
}

.sidebar-item {
	padding: 28rpx 0;
	text-align: center;
	font-size: 26rpx;
	color: #666;
	position: relative;
}

.sidebar-item.active {
	background-color: #fff;
	color: #333;
	font-weight: bold;
}

.sidebar-item.active::before {
	content: '';
	position: absolute;
	left: 0;
	top: 50%;
	transform: translateY(-50%);
	width: 6rpx;
	height: 40rpx;
	background-color: #f7ca3e;
	border-radius: 0 6rpx 6rpx 0;
}

.content-view {
	flex: 1;
	padding: 32rpx;
}

.category-section {
	margin-bottom: 48rpx;
}

.service-grid {
	display: flex;
	flex-wrap: wrap;
}

.service-cell {
	width: 33.33%;
	display: flex;
	flex-direction: column;
	align-items: center;
	margin-bottom: 32rpx;
}

.icon-wrapper {
	width: 96rpx;
	height: 96rpx;
	background-color: #fff;
	border-radius: 24rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
	margin-bottom: 16rpx;
}

.service-icon {
	width: 56rpx;
	height: 56rpx;
}

.service-name {
	font-size: 24rpx;
	color: #666;
	text-align: center;
}
</style>
