<template>
	<view class="nav-title"
		:style="{height:navBarHeight+'px',opacity: navOpacity,transition: 'opacity 0.3s ease-in-out',background: backgroundColor }">
		<view class="back-icon" :style="{paddingTop:statusBarHeight+'px'}" @click="openSellocation">
			<zzx-icon name="location" size="13"></zzx-icon>
			<view class="location-text textHidden">{{addressName}}</view>
			<zzx-icon name="right" size="10"></zzx-icon>
		</view>
	</view>
	<view class="index-header">
		<image class="header-bg" src="@/static/index_bg.png" mode="widthFix"></image>
		<view class="back-icon" :style="{paddingTop:(statusBarHeight+10)+'px'}" v-if="navOpacity==0"
			@click="openSellocation">
			<zzx-icon name="location" size="13"></zzx-icon>
			<view class="location-text textHidden">{{addressName}}</view>
			<zzx-icon name="right" size="10"></zzx-icon>
		</view>
		<view class="content" style="padding-top:180rpx;">
			<view class="header-search">
				<zzx-icon name="search" size="18"></zzx-icon>
				<swiper class="i-search-swiper" :vertical="true" :circular="true" :autoplay="true" :interval="5000"
					:duration="1000" @click="gotoPage('/pages/index/search/index')" @change="transitionChange">
					<swiper-item v-for="(item,index) in searchList" :key="item.id">
						<view class="">{{item.searchContent}}</view>
					</swiper-item>
				</swiper>
				<!-- <input type="text" placeholder="搜索俱乐部名称" /> -->
				<view class="search-btn" @click="searchInfoBtn">
					搜索
				</view>
			</view>
		</view>
		<view class="header-banner">
			<view style="height: 200rpx;line-height: 200rpx;" v-if="indexLoading">
				<zs-loading></zs-loading>
			</view>
			<swiper class="header-swiper" :autoplay="true" :current="currentIndex" :circular="true"
				previous-margin="80rpx" next-margin="80rpx" :interval="4000" :duration="500" @change="swierChange">
				<swiper-item v-for="(item,i) in bannerList" :key="i">
					<image :src="item.imageUrl" class="slide-image" :class="currentIndex === i?'active':''"></image>
				</swiper-item>
			</swiper>
		</view>
	</view>
	<view class="content">
		<view class="announcement" @click="gotoPage('/pages/index/messageNotice/index')" v-if="indexMsg.length>0">
			<image src="/src/static/gg-icon.png" mode="heightFix"></image>
			<view class="">
				<swiper class="announcement-swiper" :vertical="true" :circular="true" :autoplay="true" :interval="3000"
					:duration="1000">
					<swiper-item v-for="(item,index) in indexMsg" :key="item.id">
						<view class="swiper-item textHidden">{{item.titile}}</view>
					</swiper-item>
				</swiper>
			</view>
			<zzx-icon name="ashRight" size="10"></zzx-icon>
		</view>
		<view class="grid-card">
			<view class="grid-item" @click="gotoPage('/pages/index/venue/index')">
				<view class="item-icon">
					<image src="/src/static/cd-icon.png" mode=""></image>
				</view>
				<view class="item-text">场地</view>
			</view>
			<view class="grid-item" @click="gotoPage('/pages/index/training/index')">
				<view class="item-icon">
					<image src="/src/static/px-icon.png" mode=""></image>
				</view>
				<view class="item-text">培训</view>
			</view>
			<view class="grid-item" @click="gotoPage('/pages/index/events/index')">
				<view class="item-icon">
					<image src="/src/static/ss-icon.png" mode=""></image>
				</view>
				<view class="item-text">赛事</view>
			</view>
		</view>
		<zs-loading v-if="indexLoading"></zs-loading>
		<view class="course-card" v-else>
			<view class="commonweal" @click="gotoPage(`/pages/index/training/index?priceType=1`)">
				<view class="commonweal-title">
					<view class="title-left">
						<image src="/src/static/gy-icon.png" mode=""></image>
						<view class="">公益课</view>
					</view>
					<zzx-icon name="right" size="14"></zzx-icon>
				</view>
				<view class="commonweal-detail">
					<view class="detail-list" v-for="item in gyCourseList" :key="item.id">
						<image :src="item.cover"></image>
						<view class="detail-text">免费</view>
					</view>
				</view>
			</view>
			<view class="audition" @click="gotoPage(`/pages/index/training/index?priceType=0`)">
				<view class="audition-title">
					<view class="title-left">
						<image src="/src/static/st-icon.png" mode=""></image>
						<view class="">试听课</view>
					</view>
					<zzx-icon name="whiteRight" size="14"></zzx-icon>
				</view>
				<view class="audition-detail">
					<view class="detail-list" v-for="item in stCourseList" :key="item.id">
						<image :src="item.cover"></image>
						<view class="detail-text">￥{{item.sellingPrice||'0'}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="hot-instructor" v-if="!indexLoading">
			<view class="i-title">
				<view class="i-hot">
					<view class="i-text">
						<text>热门教练</text>
					</view>
					<image src="/src/static/hot-icon1.png" mode="widthFix"></image>
				</view>
				<view class="i-more" @click="gotoPage('/pages/index/allInstructor/index')">
					<view class="">全部教练</view>
					<zzx-icon name="ashRight" size="10"></zzx-icon>
				</view>
			</view>
			<view class="i-info-card">
				<view class="i-card-list"
					:style="{background:index % 2=== 0?'linear-gradient(179deg, #FFD5FB 0%, #FFFFFF 40%)':'linear-gradient( 179deg, #F2FFBE 0%, #FFFFFF 40%)'}"
					v-for="(item,index) in instructorList" :key="item.id">
					<view class="i-info" @click="gotoPage(`/pages/index/instructorDetail/index?id=${item.id}`)">
						<view class="header" :style="{background:index % 2=== 0?'#FFB8F9':'#C8FF0C'}">
							<image class="head-img" :src="item.avatar" mode=""></image>
							<image class="head-tag" src="/src/static/badge.png" mode=""></image>
						</view>
						<view class="i-name-tag">
							<view class="name-tag">
								<view class="name">
									<view>
										<text class="textHidden" style="width: 130rpx;">{{item.name}}</text>
									</view>
									<image v-if="index % 2=== 0" src="/src/static/name_bg.png" mode=""></image>
									<image v-else src="/src/static/name_bg1.png" mode=""></image>
								</view>
								<view class="tag" v-for="(tag,index) in item.list">{{tag}}</view>
							</view>
							<view class="ascription textHidden">{{item.orgName}}</view>
							<view class="sign textHidden">{{item.teachingPhilosophy}}</view>
						</view>
						<view class="type-icon">
							<image src="/src/static/hot-icon2.png" mode=""></image>
						</view>
					</view>
					<view class="i-data">
						<view class="acclaim">
							<zzx-icon name="acclaim"></zzx-icon>
							<view style="color: #FFCC00;">好评 {{item.goodRate}}%</view>
						</view>
						<view class="acclaim">
							<zzx-icon name="order"></zzx-icon>
							<view style="color: #4DDF96;">订单数 {{item.orderCount||'0'}}</view>
						</view>
						<view class="acclaim">
							<zzx-icon name="number"></zzx-icon>
							<view style="color: #AB7BFF;">授课人数 {{item.teachingCount}}</view>
						</view>
					</view>
					<view class="i-skill">{{item.excelMsg}}</view>
					<view class="i-course" v-for="(course,index) in item.courseList" :key="course.id">
						<view class="course-name">
							<view class="price">￥{{(course.sellingPrice).toFixed(2)||'0.00'}}</view>
							<view class="name textHidden">{{course.name}}</view>
						</view>
						<view class="num">已售{{item.count||'0'}}</view>
					</view>
					<view class="more" @click="gotoPage(`/pages/index/instructorDetail/index?id=${item.id}`)">更多课程
					</view>
				</view>
			</view>
			<!-- <view style="position: relative;z-index: 1;"> -->
			<zs-empty v-if="!loading && instructorList.length === 0" />
			<!-- </view> -->
		</view>
		<view class="login-tips" v-if="!token">
			<view class="">终于等到你！快去登录吧~</view>
			<view class="" @click="submitLogin">马上登录</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import zsLoading from '@/components/zzx-loading/zzx-loading.vue'
	import { ref, onMounted, computed } from 'vue'
	import { onLoad, onPageScroll, onPullDownRefresh } from '@dcloudio/uni-app';
	import zsEmpty from '@/components/zs-empty/index.vue'
	import { RouterUtils, TipsUtils } from '@/utils/util';
	import { http } from '@/utils/http'
	import { useCacheStore } from '@/stores/cache'
	const cache = useCacheStore()
	const token = computed(() => {
		return cache.get('TOKEN')
	})
	const navBarHeight = ref(0);
	const statusBarHeight = ref(0);
	const titleBarHeight = ref(0);
	const navOpacity = ref(0);
	const backgroundColor = ref('#D2FD73');
	const instructorList = ref();
	onLoad(() => {
		get_location()
	})
	onPullDownRefresh(() => {
		get_indexInfo()
		get_message()
		get_searchList()
	})
	onMounted(() => {
		get_navheight()
		get_indexInfo()
		get_message()
		get_searchList()
	})
	onPageScroll((e) => {
		navOpacity.value = Math.min(Math.max(e.scrollTop / navBarHeight.value, 0), 1);
	})
	const get_navheight = () => {
		const systemInfo = uni.getSystemInfoSync();
		statusBarHeight.value = systemInfo.statusBarHeight || 0;
		const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
		const titleBarHeightCalc = (menuButtonInfo.top - statusBarHeight.value) * 2 + menuButtonInfo.height;
		titleBarHeight.value = titleBarHeightCalc;
		navBarHeight.value = statusBarHeight.value + titleBarHeight.value;
	}

	const currentIndex = ref(0);
	const bannerList = ref([{
		img: 'https://bpic.588ku.com/element_origin_min_pic/23/07/11/d32dabe266d10da8b21bd640a2e9b611.jpg!r650'
	}, {
		img: 'https://ts1.tc.mm.bing.net/th/id/R-C.6f18e58bf3bacbcee226cb76dfc089a3?rik=61%2bMLnhOzFiZOg&riu=http%3a%2f%2fimages.shejidaren.com%2fwp-content%2fuploads%2f2014%2f07%2f085628m6c.jpg&ehk=v1vDvzVgPRc3irw6n%2bs5gF%2b5SEc1uCstErDhRaxqlgE%3d&risl=&pid=ImgRaw&r=0'
	}, {
		img: 'https://ts1.tc.mm.bing.net/th/id/R-C.a8553f142638e741396e386b43c2bca7?rik=dzSGk6XRsxAAsQ&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50062%2f5890.jpg_wh1200.jpg&ehk=BgxiqxvzoNQd0pZHWV4VPOMbYgqM76WDMt8RDzCjoYY%3d&risl=&pid=ImgRaw&r=0'
	}])
	const swierChange = (e) => {
		currentIndex.value = e.detail.current
	}

	const gotoPage = (path : string) => {
		if (!token.value) return TipsUtils.tips_alert('请先登录~', false)
		uni.navigateTo({
			url: path
		})
	}

	const submitLogin = () => {
		uni.login({
			provider: 'weixin',
			success: (res) => {
				get_logininfo(res.code)
			}
		})
	}

	const get_logininfo = (item : string) => {
		http.get('/user/loginByCode', { data: { code: item }, loading: true }).then(res => {
			cache.set('TOKEN', res.result.token)
			cache.set('USER_INFO', res.result)
			get_indexInfo()
			get_message()
			get_searchList()
			get_userinfo()
			TipsUtils.tips_toast('登录成功')
		})
	}

	const get_userinfo = () => {
		http.get('/user/getUserInfo', { loading: true }).then(res => {
			cache.set('USER_INFO', res.result)
		})
	}

	const get_location = () => {
		uni.authorize({
			scope: 'scope.userLocation',
			success() {
				uni.getLocation({
					type: 'gcj02',
					success: function (res) {
						uni.hideLoading()
						cache.set('LON', res.longitude)
						cache.set('LAT', res.latitude)
					},
					fail: function (err) {
						if (err.errMsg === 'getFuzzyLocation:fail system permission denied') {
							TipsUtils.tips_alert('系统定位未开启，请开启定位以便提供更好服务', false)
						} else if (err.errMsg === 'getFuzzyLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' ||
							err.errMsg === 'getFuzzyLocation:fail system permission denied' ||
							err.errMsg === 'getFuzzyLocation:fail:system permission denied' || err.errMsg === 'getFuzzyLocation:fail auth deny') {
							showAuthGuide()
						}
						console.log(err, '错误信息')
					}
				});
			},
			fail: (err) => {
				showAuthGuide()
			}
		});
	}

	// 引导操作
	const showAuthGuide = async () => {
		let res : any = await TipsUtils.tips_alert('授权定位以提供更好服务', true)
		if (res.confirm) {
			uni.openSetting()
		}
	}
	
	// 手动选择
	const addressName = ref('选择定位')
	const openSellocation = () => {
		uni.chooseLocation({
			success: function (res) {
				addressName.value = res.name
				cache.set('LON', res.longitude)
				cache.set('LAT', res.latitude)
			},
			fail(res) {
				console.log(res);
				// showAuthGuide()
			}
		});
	}

	// 首页基础信息
	const gyCourseList = ref()
	const stCourseList = ref()
	const indexLoading = ref(true)
	const get_indexInfo = () => {
		http.get('/home/homeInfo').then((res) => {
			indexLoading.value = false
			bannerList.value = res.result.bannerList
			gyCourseList.value = res.result.courseList.filter(item => item.priceType === 0)
			stCourseList.value = res.result.courseList.filter(item => item.priceType === 1)
			instructorList.value = res.result.instructorList
			uni.stopPullDownRefresh()
		})
	}

	// 消息通知
	const indexMsg = ref([])
	const get_message = () => {
		http.get('/home/getMsg').then((res) => {
			indexMsg.value = res.result
		})
	}

	// 搜索相关
	const searchList = ref([])
	const get_searchList = () => {
		http.get('/home/getHotSearch').then((res) => {
			searchList.value = res.result
		})
	}
	const searchCurrent = ref()
	const transitionChange = (e) => {
		searchCurrent.value = e.detail.current
	}
	const searchInfoBtn = () => {
		if (searchList.value < 1) return TipsUtils.tips_toast('请输入关键词')
		let keyword = ''
		let venueType = null
		if (searchList.value.length > 1) {
			keyword = searchList.value[searchCurrent.value].searchContent
			venueType = searchList.value[searchCurrent.value].venueType
		} else {
			searchList.value.map((item) => {
				keyword = item.searchContent
				venueType = item.venueType
			})
		}
		gotoPage(`/pages/index/searchPage/index?keyword=${keyword}&venueType=${venueType}`)
	}
</script>

<style lang="less" scoped>
	.nav-title {
		position: fixed;
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		z-index: 9999;
		display: flex;
		justify-content: center;
		align-items: center;

		.back-icon {
			position: absolute;
			left: 20rpx;
			z-index: 9999;
			display: flex;
			align-items: center;
			gap: 10rpx;
			font-weight: 800;
			font-size: 28rpx;
			color: #222222;

			.location-text {
				max-width: 460rpx;
			}
		}
	}

	.index-header {
		position: relative;
		height: 580rpx;
		.header-bg {
			position: absolute;
			width: 100%;
		}

		.back-icon {
			position: absolute;
			left: 20rpx;
			z-index: 9999;
			display: flex;
			align-items: center;
			gap: 10rpx;
			font-weight: 800;
			font-size: 28rpx;
			color: #222222;

			.location-text {
				max-width: 460rpx;
			}
		}

		.header-search {
			position: absolute;
			display: flex;
			align-items: center;
			gap: 10rpx;
			background: #fff;
			width: 685rpx;
			padding: 8rpx;
			border-radius: 40rpx;

			.i-search-swiper {
				height: 40rpx;
				line-height: 40rpx;
				width: 670rpx;
				font-weight: bold;
				font-size: 24rpx;
				color: #CCCCCC;
			}

			.search-btn {
				width: 144rpx;
				height: 52rpx;
				background: #222222;
				border-radius: 28rpx;
				font-weight: bold;
				font-size: 28rpx;
				color: #C8FF0C;
				line-height: 52rpx;
				text-align: center;
			}
		}

		.header-banner {
			position: absolute;
			top: 240rpx;
			left: 0;
			width: 100%;
			height: 340rpx;

			.header-swiper {
				height: 340rpx;
			}

			.slide-image {
				position: absolute;
				height: 200rpx;
				width: 570rpx;
				border-radius: 15rpx;
				z-index: 5;
				opacity: 0.7;
				top: 18%;
				margin: 0 20rpx;
			}

			swiper {
				height: 430rpx;
			}

			.active {
				opacity: 1;
				z-index: 10;
				height: 316rpx;
				width: 572rpx;
				top: 7%;
				transition: all 0.2s ease-in 0s;
			}
		}
	}

	.announcement {
		margin-top: 20rpx;
		padding: 20rpx;
		background: #FFFFFF;
		border-radius: 32rpx;
		display: flex;
		justify-content: space-around;
		z-index: 9999;

		&>image {
			height: 32rpx;
		}

		&>view {
			font-size: 28rpx;
			color: #222222;
			width: 440rpx;

			.announcement-swiper {
				height: 40rpx;
			}
		}

	}

	.grid-card {
		padding: 20rpx;
		background: #FFFFFF;
		border-radius: 32rpx;
		display: flex;
		justify-content: space-around;
		margin-top: 20rpx;

		.grid-item {
			display: flex;
			flex-direction: column;
			align-items: center;

			.item-icon {
				width: 80rpx;
				height: 80rpx;

				&>image {
					width: 80rpx;
					height: 80rpx;
				}
			}

			.item-text {
				font-size: 24rpx;
				color: #222222;
				margin-top: 20rpx;
			}
		}
	}

	.course-card {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;

		.commonweal {
			background: #C8FF0C;
			border-radius: 32rpx;
			padding: 20rpx;

			.commonweal-title {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.title-left {
					font-weight: 800;
					font-size: 32rpx;
					color: #222222;
					display: flex;
					gap: 10rpx;

					&>image {
						width: 40rpx;
						height: 40rpx;
					}
				}
			}

			.commonweal-detail {
				display: flex;
				justify-content: space-around;
				gap: 20rpx;
				margin-top: 20rpx;

				.detail-list {
					display: flex;
					flex-direction: column;
					align-items: center;

					&>image {
						width: 80rpx;
						height: 80rpx;
						border-radius: 50%;
					}

					.detail-text {
						text-align: center;
						margin-top: 16rpx;
						font-size: 24rpx;
						color: #222222;
					}
				}
			}
		}

		.audition {
			background: #AB7BFF;
			border-radius: 32rpx;
			padding: 20rpx;

			.audition-title {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.title-left {
					font-weight: 800;
					font-size: 32rpx;
					color: #FFFFFF;
					display: flex;
					gap: 10rpx;

					&>image {
						width: 40rpx;
						height: 40rpx;
					}
				}
			}

			.audition-detail {
				display: flex;
				justify-content: space-around;
				gap: 20rpx;
				margin-top: 20rpx;

				.detail-list {
					display: flex;
					flex-direction: column;
					align-items: center;
					text-align: center;

					&>image {
						width: 80rpx;
						height: 80rpx;
						border-radius: 50%;
					}

					.detail-text {
						text-align: center;
						margin-top: 16rpx;
						font-size: 24rpx;
						color: #fff;
					}
				}
			}
		}
	}

	.hot-instructor {
		margin-top: 20rpx;
		position: relative;
		.i-title {
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;
			padding: 0 20rpx 0 20rpx;
			border-radius: 32rpx 32rpx 0 0;

			.i-hot {
				display: flex;
				align-items: center;
				font-weight: bold;
				font-size: 32rpx;
				color: #222222;
				position: relative;
				height: 100rpx;
				.i-text {
					position: relative;
					width: 130rpx;
					margin-top: 30rpx;

					&>text {
						position: absolute;
						bottom: 0;
						z-index: 1;
					}
				}

				.i-text::after {
					position: absolute;
					content: '';
					bottom: -6rpx;
					left: 20rpx;
					width: 80rpx;
					height: 20rpx;
					background-color: #D3FD76;
					border-radius: 4rpx;
				}

				&>image {
					position: absolute;
					width: 300rpx;
					top: 0;
					left: 140rpx;
				}
			}

			.i-more {
				font-size: 24rpx;
				color: #AAAAAA;
				display: flex;
				align-items: center;
				gap: 10rpx;
			}
		}

		.i-info-card {
			position: absolute;

			.i-card-list {
				margin-bottom: 24rpx;
				border-radius: 32rpx;
				padding: 20rpx;
				width: 670rpx;
				max-height: 480rpx;
			}

			.i-info {
				display: flex;
				align-items: center;
				justify-content: space-around;

				.header {
					width: 150rpx;
					height: 150rpx;
					border-radius: 32rpx;
					// background: #FFB8F9;
					padding: 4rpx;
					position: relative;

					.head-img {
						width: 150rpx;
						height: 150rpx;
						border-radius: 32rpx;
					}

					.head-tag {
						position: absolute;
						bottom: 0;
						right: -14rpx;
						width: 36rpx;
						height: 36rpx;
					}
				}

				.i-name-tag {
					.name-tag {
						display: flex;
						align-items: center;
						gap: 8rpx;
						.name {
							width: 134rpx;
							height: 60rpx;
							font-weight: 800;
							font-size: 28rpx;
							color: #222222;
							position: relative;

							&>view {
								&>text {
									position: absolute;
									z-index: 1;
									top: 10rpx;
								}
							}

							&>image {
								top: 0;
								left: 0;
								position: absolute;
								width: 133rpx;
								height: 48rpx;
							}
						}

						.tag {
							font-size: 24rpx;
							color: #AAAAAA;
						}
					}

					.ascription {
						max-width: 360rpx;
						font-size: 24rpx;
						color: #222222;
					}

					.sign {
						width: 360rpx;
						margin-top: 20rpx;
						font-weight: bold;
						font-size: 24rpx;
						color: #222222;
					}
				}

				.type-icon {
					&>image {
						width: 106rpx;
						height: 100rpx;
					}
				}
			}

			.i-data {
				display: flex;
				align-items: center;
				gap: 28rpx;
				margin-top: 20rpx;

				.acclaim {
					display: flex;
					align-items: center;
					gap: 10rpx;
					font-size: 24rpx;
				}
			}

			.i-skill {
				width: 700rpx;
				margin-top: 20rpx;
				font-size: 24rpx;
				color: #AAAAAA;
			}

			.i-course {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 20rpx;

				.course-name {
					display: flex;
					align-items: center;
					gap: 20rpx;

					.price {
						font-weight: bold;
						font-size: 28rpx;
						color: #FB5B5B;
					}

					.name {
						width: 440rpx;
						font-weight: bold;
						font-size: 28rpx;
						color: #222222;
					}
				}

				.num {
					font-size: 24rpx;
					color: #AAAAAA;
				}
			}

			.more {
				margin: auto;
				width: 100rpx;
				margin-top: 20rpx;
				text-align: center;
				font-size: 24rpx;
				color: #CCCCCC;
				position: relative;
			}

			.more::after {
				position: absolute;
				bottom: -10rpx;
				left: 25rpx;
				content: '';
				width: 38rpx;
				height: 4rpx;
				border-radius: 2rpx;
				background-color: #CCCCCC;
			}
		}
	}

	.login-tips {
		position: fixed;
		bottom: 22rpx;
		width: 686rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 10rpx;
		background: rgba(0, 0, 0, 0.6);
		border-radius: 8rpx;
		z-index: 9999;

		&>view:first-child {
			font-size: 28rpx;
			color: #FFFFFF;
		}

		&>view:last-child {
			width: 160rpx;
			height: 60rpx;
			background: #C8FF0C;
			border-radius: 8rpx;
			font-size: 28rpx;
			color: #222222;
			text-align: center;
			line-height: 60rpx;
		}
	}
</style>