<template>
	<view class="container">
		<!-- 标题区域 -->
		<view class="title">{{ info.theme }}</view>

		<!-- 时间区域 -->
		<view class="time">时间：{{ dayjs(info.startTime).format('YYYY-MM-DD HH:mm:ss') }} 至 {{ dayjs(info.endTime).format('YYYY-MM-DD HH:mm:ss') }}</view>

		<!-- 地址区域 -->
		<view class="address">
			地点：{{ info.address }}
			<uni-icons type="location" size="20" color="#007AFF"></uni-icons>
		</view>

		<!-- 咨询方式 -->
		<view class="phone">
			咨询方式：
			<view class="phone_value" @click="clickPhone(info.phone)">
				{{ info.phone }}
			</view>
		</view>

		<!-- 富文本区域 -->
		<view class="richtext">
			<uv-parse :content="info.themeDetail" :tagStyle="tagStyle"></uv-parse>
		</view>

		<!-- 参与说明区域 -->
		<view class="box">
			<view class="box_icon"></view>
			<view class="box_title">参与说明</view>
		</view>
		<view class="desc">
			{{ info.describes }}
		</view>

		<!-- 已报名区域 -->
		<view class="box" v-if="info.reportDatas?.length">
			<view class="box_icon"></view>
			<view class="box_title">已报名（{{ info.reportDatas.length }}人）</view>
			<view class="box_more" @click="goDetail(1)">
				查看更多&nbsp;
				<uni-icons type="right" size="18" color="#007AFF"></uni-icons>
			</view>
		</view>
		<view class="sign" v-if="info.reportDatas?.length">
			<!-- 每一个报名人员 -->
			<view class="sign_box" v-for="(item, index) in info.reportDatas" :key="index">
				<image class="img" src="@/static/images/9.png" mode="aspectFill"></image>
				<view class="box_text">{{ item.name }}</view>
			</view>
		</view>

		<!-- 签到人员区域 -->
		<view class="box" v-if="info.signinDatas?.length">
			<view class="box_icon"></view>
			<view class="box_title">签到人员（{{ info.signinDatas.length }}人）</view>
			<view class="box_more" @click="goDetail(2)">
				查看更多&nbsp;
				<uni-icons type="right" size="18" color="#007AFF"></uni-icons>
			</view>
		</view>
		<view class="sign" v-if="info.signinDatas?.length">
			<!-- 每一个签到人员 -->
			<view class="sign_box" v-for="(item, index) in info.signinDatas" :key="index">
				<image class="img" src="@/static/images/9.png" mode="aspectFill"></image>
				<view class="box_text">{{ item.name }}</view>
			</view>
		</view>

		<!-- 活动相册区域 -->
		<view class="box" v-if="info.isImage == 1 && info.isReport == 1">
			<view class="box_icon"></view>
			<view class="box_title">活动相册</view>
			<view class="box_more" @click="goDetail(3)">
				{{ info.images?.length ? '查看更多&nbsp;' : '去上传&nbsp;' }}
				<uni-icons type="right" size="18" color="#007AFF"></uni-icons>
			</view>
		</view>
		<view class="album" v-if="info.isImage == 1 && info.images?.length">
			<!-- 每一张照片区域 -->
			<image class="img" :src="item" mode="aspectFill" v-for="(item, index) in info.images.splice(0, 3)" :key="item"></image>
		</view>

		<!-- 按钮区域 -->
		<view v-if="info.isReport == 2">
			<!-- 未报名时 -->
			<view v-if="dayjs(info.signsTime).valueOf() > Date.now()" class="btn nostart">
				报名未开始
				<view class="btn_text">{{ dayjs(info.signsTime).format('YYYY-MM-DD HH:mm:ss') }} 开始报名</view>
			</view>

			<view v-if="dayjs(info.signeTime).valueOf() > Date.now()" class="btn" @click="handleApply_throttle">
				我要报名
				<view class="btn_text">{{ dayjs(info.signeTime).format('YYYY-MM-DD HH:mm:ss') }} 报名截止</view>
			</view>

			<view v-else class="btn off">报名已截止</view>
		</view>

		<view v-if="info.isReport == 1 && info.isSign == 2">
			<!-- 未签到时 -->

			<view v-if="dayjs(info.startTime).valueOf() > Date.now()" class="btn nostart">
				活动未开始
				<view class="btn_text">{{ dayjs(info.startTime).format('YYYY-MM-DD HH:mm:ss') }} 开始签到</view>
			</view>

			<view v-if="dayjs(info.endTime).valueOf() > Date.now()" class="btn" @click="handleSign_throttle">
				我要签到
				<view class="btn_text">{{ dayjs(info.endTime).format('YYYY-MM-DD HH:mm:ss') }} 签到截止</view>
			</view>

			<view v-else class="btn off">活动已结束</view>
		</view>

		<view class="btn off" v-if="info.isReport == 1 && info.isSign == 1">已签到</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { getDetailInfoById, getReportById, getSigninById } from '@/api/index.js'
import dayjs from 'dayjs'
import { calculateDistance } from '@/utils/calculateDistance.js'
import lodash from 'lodash'

// 富文本样式
let tagStyle = {
	img: 'height:350rpx'
}

// 详情数据
const info = ref({})
// 活动ID
const currentId = ref()

const lat = ref('')
const lng = ref('')

onLoad((options) => {
	if (options.id) {
		currentId.value = options.id
		// 根据ID获取活动数据详情
		getData(options.id)
		// 获取当前用户经纬度
		getAddress()
	}
})

// 根据ID获取活动数据详情
const getData = async (id) => {
	let data = {
		id
	}
	const res = await getDetailInfoById(data)
	// console.log(res)
	info.value = res.data
}

// 获取当前用户经纬度
const getAddress = () => {
	uni.getLocation({
		// type: 'wgs84',
		success: (res) => {
			lng.value = res.longitude
			lat.value = res.latitude
		}
	})
}

// 点击查看更多回调
const goDetail = (e) => {
	// 1为已报名,2为签到人员,3为活动相册
	if (e == 1 || e == 2) {
		let temList = e == 1 ? info.value.reportDatas : info.value.signinDatas
		let list = encodeURIComponent(JSON.stringify(temList))
		uni.navigateTo({
			url: `/pages/people_detail/people_detail?type=${e}&list=${list}`
		})
	} else if (e == 3) {
		let imgList = encodeURIComponent(JSON.stringify(info.value.images))
		uni.navigateTo({
			url: `/pages/act_album/act_album?imgList=${imgList}&currentId=${currentId.value}`
		})
	}
}

// 我要报名按钮回调
const handleApply = () => {
	uni.showModal({
		title: '提示',
		content: '确定报名吗？',
		success: async (res) => {
			if (res.confirm) {
				let data = {
					id: currentId.value
				}
				const res = await getReportById(data)
				// console.log(res)
				if (res.code == 200) {
					uni.showToast({
						title: res.message,
						icon: 'success',
						mask: true
					})
					setTimeout(() => {
						getData(currentId.value)
					}, 1500)
				}
			}
		}
	})
}

// 节流
const handleApply_throttle = lodash.throttle(handleApply, 1000, {
	trailing: false
})

// 我要签到按钮回调
const handleSign = () => {
	let rangValue = 500

	// 计算出距离
	let distance = calculateDistance(info.value.lat, info.value.lng, lat.value, lng.value)
	// console.log(distance)

	if (rangValue > distance) {
		uni.showModal({
			title: '提示',
			content: '确定签到吗？',
			success: async (res) => {
				if (res.confirm) {
					let data = {
						id: currentId.value
					}
					const res = await getSigninById(data)
					// console.log(res)
					if (res.code == 200) {
						uni.showToast({
							title: res.message,
							icon: 'success',
							mask: true
						})
						setTimeout(() => {
							getData(currentId.value)
						}, 1500)
					}
				}
			}
		})
	} else {
		uni.showToast({
			title: '超出签到范围，无法签到',
			icon: 'none',
			mask: true
		})
	}
}

// 节流
const handleSign_throttle = lodash.throttle(handleSign, 1000, {
	trailing: false
})

// 拨打电话回调
const clickPhone = (phone) => {
	uni.makePhoneCall({
		phoneNumber: phone
	})
}
</script>

<style lang="scss" scoped>
.container {
	padding: 20rpx 18rpx 150rpx;
	margin-bottom: 60rpx;
	// min-height: 100vh;
	font-size: 28rpx;

	.title {
		font-size: 32rpx;
		font-weight: bold;
	}

	.time {
		margin-top: 15rpx;
		font-size: 24rpx;
		color: #808080;
	}

	.address {
		margin-top: 15rpx;
		line-height: 45rpx;
		font-size: 24rpx;
		color: #808080;
	}

	.phone {
		display: flex;
		margin-top: 15rpx;
		line-height: 45rpx;
		font-size: 24rpx;
		color: #808080;

		.phone_value {
			color: #007aff;
		}
	}

	.richtext {
		margin: 15rpx 0;
	}

	.box {
		display: flex;
		align-items: center;

		.box_icon {
			width: 10rpx;
			height: 27rpx;
			border-radius: 30rpx;
			background-color: #007aff;
		}

		.box_title {
			margin-left: 16rpx;
			font-weight: bold;
		}

		.box_more {
			display: flex;
			align-items: center;
			margin-left: auto;
			color: #007aff;
			font-size: 24rpx;
		}
	}

	.desc {
		margin: 20rpx 0;
		line-height: 40rpx;
	}

	.sign {
		display: grid;
		grid-template-columns: repeat(6, 1fr);
		margin: 20rpx 0;

		.sign_box {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 120rpx;

			.img {
				margin-bottom: 5rpx;
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
			}

			.box_text {
				flex: 1;
				text-align: center;
				overflow: hidden;
			}
		}
	}

	.album {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		margin: 20rpx 0 120rpx;

		.img {
			width: 200rpx;
			height: 200rpx;
			border-radius: 10rpx;
		}
	}

	.btn {
		position: fixed;
		left: 50%;
		bottom: 40rpx;
		transform: translateX(-50%);
		display: flex;
		flex-direction: column;
		justify-content: space-evenly;
		align-items: center;
		width: 661rpx;
		height: 100rpx;
		color: #fff;
		font-size: 28rpx;
		border-radius: 8rpx;
		background-color: #007aff;

		.btn_text {
			font-size: 20rpx;
		}
	}

	.off {
		color: #a6a6a6;
		background-color: #e5e5e5;
	}

	.nostart {
		color: #fff;
		background-color: #007aff;
		opacity: 0.3;
	}
}
</style>
