<template>
	<view class="container">
		<!-- 分段器区域 -->
		<uni-segmented-control :current="activeCurrent" :values="controlList" @clickItem="onClickItem" styleType="text" activeColor="#0061FF"></uni-segmented-control>
		<!-- 预约记录区域 -->
		<view class="body">
			<!-- 每一条预约记录区域 -->
			<view class="body_box" v-for="item in list" :key="item.id" @click="handleClick(item)">
				<view class="box_title">
					<view class="title_msg">
						<view class="msg_name">{{ item.userName }}</view>
						<view class="msg_time">/{{ time_format(item.createTime) }}</view>
					</view>
					<view class="title_type" v-if="item.statuStr == '待审核'">{{ item.statuStr }}</view>
					<view class="title_type red" v-if="item.statuStr == '已拒绝'">{{ item.statuStr }}</view>
					<view class="title_type green" v-if="item.statuStr == '已推送'">{{ item.statuStr }}</view>
				</view>
				<view class="box_info">
					<view class="info_box">
						<view class="box_key">来访时间</view>
						<view class="box_value">{{ time_format(item.visitorTime) }}</view>
					</view>
					<view class="info_box">
						<view class="box_key">访问事由</view>
						<view class="box_value">{{ item.visitReason }}</view>
					</view>
				</view>
				<view class="box_btn" v-if="item.statuStr == '待审核'">
					<view class="button" @click.stop="handleCancel(item.id)">取消预约</view>
				</view>
			</view>
		</view>

		<!-- 没有数据时展示的页面 -->
		<NoData v-if="!list.length" />

		<!-- 详情弹窗区域 -->
		<uni-popup ref="popup" type="center" :is-mask-click="false">
			<view class="popup_box">
				<!-- 弹窗标题区域 -->
				<view class="pop_header">
					预约详情
					<view class="header_icon" @click="closePop">×</view>
				</view>

				<!-- 弹窗详细信息区域 -->
				<view class="pop_info">
					<view class="info_key">状态：</view>
					<view class="info_value blue" v-if="popObj.statuStr == '待审核'">{{ popObj.statuStr }}</view>
					<view class="info_value red" v-if="popObj.statuStr == '已拒绝'">{{ popObj.statuStr }}</view>
					<view class="info_value green" v-if="popObj.statuStr == '已推送'">{{ popObj.statuStr }}</view>
				</view>

				<view class="pop_info">
					<view class="info_key">访客姓名：</view>
					<view class="info_value">{{ popObj.userName }}</view>
				</view>

				<view class="pop_info">
					<view class="info_key">访客手机号：</view>
					<view class="info_value">{{ popObj.userPhone }}</view>
				</view>

				<view class="pop_info">
					<view class="info_key">来访时间：</view>
					<view class="info_value" v-if="popObj.visitorTime">{{ time_format(popObj.visitorTime) }}</view>
				</view>

				<view class="pop_info">
					<view class="info_key">证件号：</view>
					<view class="info_value">{{ popObj.userNumber || '暂无' }}</view>
				</view>

				<view class="pop_info">
					<view class="info_key">访问事由：</view>
					<view class="info_value">{{ popObj.visitReason }}</view>
				</view>

				<view class="pop_info">
					<view class="info_key">车牌号：</view>
					<view class="info_value">{{ popObj.carNum ? popObj.carNum : '暂无' }}</view>
				</view>

				<view class="pop_info">
					<view class="info_key">同行人数：</view>
					<view class="info_value">{{ popObj.peerNum ? popObj.peerNum : '暂无' }}</view>
				</view>

				<view class="pop_info">
					<view class="info_key">{{ popObj.visitorType == 1 ? '受访学生' : '受访人' }}：</view>
					<view class="info_value">{{ popObj.respondentName }}（{{ popObj.responcode || popObj.respondentPhone }}）</view>
				</view>
			</view>
		</uni-popup>

		<!-- 底部导航栏区域 -->
		<Tabber />
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad, onReachBottom } from '@dcloudio/uni-app'
import Tabber from '@/components/tabber.vue'
import NoData from '@/components/noData.vue'
import { myRequest } from '@/utils/api.js'
import { decryptDes } from '@/utils/des.js'
import { time_format } from '@/utils/formatTime.js'

// 分段器当前索引
const activeCurrent = ref(0)

// 当前页
const currentPage = ref(1)

// 记录总条数
const total = ref(0)

// 分段器数组
const controlList = ['全部', '待审核', '已拒绝', '进校核销', '出校核销', '已完成']

// 预约记录数组
const list = ref([])
// 弹窗元素标记
const popup = ref(null)

// 弹窗详细信息
const popObj = ref({})

onLoad(() => {
	getData()
})

// 页面上拉到最底部时触发的回调
onReachBottom(() => {
	if (total.value > list.value.length) {
		currentPage.value++
		getData()
	} else {
		uni.showToast({
			title: '没有更多数据了',
			icon: 'none'
		})
	}
})

// 获取预约记录数据
const getData = async () => {
	let type
	if (activeCurrent.value == 0) {
		type = 0
	} else if (activeCurrent.value == 1) {
		type = 1
	} else if (activeCurrent.value == 2) {
		type = 2
	} else if (activeCurrent.value == 3) {
		type = 7
	} else if (activeCurrent.value == 4) {
		type = 8
	} else if (activeCurrent.value == 5) {
		type = 9
	}
	const res = await myRequest({
		url: '/wanzai/api/smartVisitor/appointmentPageRecord',
		data: {
			currentPage: currentPage.value,
			pageCount: 6,
			userId: uni.getStorageSync('userInfo').id,
			type
		}
	})
	// console.log(res)
	const result = JSON.parse(decryptDes(res.data))
	// console.log(result)
	total.value = result.totalCount
	list.value = [...list.value, ...result.list]
}

// 点击每一条预约记录时的回调
const handleClick = (item) => {
	popObj.value = item
	popup.value.open()
}

// 切换分段器时的回调
const onClickItem = (e) => {
	if (activeCurrent.value != e.currentIndex) {
		activeCurrent.value = e.currentIndex
		currentPage.value = 1
		list.value = []
		getData()
	}
}

// 点击取消按钮回调
const handleCancel = (id) => {
	uni.showModal({
		title: '提示',
		content: '确定取消预约吗？',
		success: (res) => {
			if (res.confirm) {
				handleCancelReq(id)
			}
		}
	})
}

// 取消预约请求
const handleCancelReq = async (id) => {
	const res = await myRequest({
		url: '/wanzai/api/smartVisitor/deleteSmartVisitorById',
		data: {
			id
		}
	})
	// console.log(res)
	uni.showToast({
		title: '取消预约成功',
		icon: 'none',
		duration: 2000
	})
	setTimeout(() => {
		currentPage.value = 1
		list.value = []
		getData()
	}, 2000)
}

// 点击弹窗 x 图标时的回调
const closePop = () => {
	popup.value.close()
}
</script>

<style lang="scss" scoped>
.container {
	box-sizing: border-box;
	padding-bottom: 30rpx;
	min-height: 100vh;
	background-color: #f1f6fe;

	.body {
		padding: 0 20rpx;

		.body_box {
			padding: 0 20rpx 0 30rpx;
			box-sizing: border-box;
			margin-top: 20rpx;
			width: 710rpx;
			border-radius: 15rpx;
			background-color: #fff;

			.box_title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 83rpx;
				font-size: 28rpx;
				border-bottom: 1rpx solid #e6e6e6;

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

					.msg_name {
						font-weight: bold;
					}

					.msg_time {
						margin-left: 15rpx;
						color: #a6a6a6;
						font-size: 24rpx;
					}
				}

				.title_type {
					color: #0061ff;
				}

				.red {
					color: #d43030;
				}

				.green {
					color: #00baad;
				}
			}

			.box_info {
				display: flex;
				flex-direction: column;

				.info_box {
					display: flex;
					align-items: center;
					height: 65rpx;
					font-size: 24rpx;

					.box_key {
						width: 120rpx;
						color: #a6a6a6;
					}

					.box_value {
						flex: 1;
						margin-left: 95rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
				}
			}

			.box_btn {
				display: flex;
				justify-content: flex-end;
				align-items: center;
				height: 130rpx;
				border-top: 1rpx solid #e6e6e6;

				.button {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 160rpx;
					height: 80rpx;
					color: #0061ff;
					font-size: 28rpx;
					border-radius: 10rpx;
					border: 1rpx solid #0061ff;
				}
			}
		}
	}

	.popup_box {
		padding-bottom: 50rpx;
		width: 710rpx;
		border-radius: 22rpx;
		background-color: #fff;

		.pop_header {
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;
			height: 94rpx;
			font-size: 28rpx;
			border-bottom: 1rpx solid #e6e6e6;

			.header_icon {
				position: absolute;
				top: 18rpx;
				right: 25rpx;
				font-size: 40rpx;
			}
		}

		.pop_info {
			display: flex;
			box-sizing: border-box;
			margin-top: 22rpx;
			padding: 0 35rpx;
			width: 100%;
			font-size: 28rpx;

			.info_key {
				color: #999999;
			}

			.info_value {
				flex: 1;
			}

			.blue {
				color: #0061ff;
			}

			.red {
				color: #d43030;
			}

			.green {
				color: #00baad;
			}
		}
	}
}

// 修改顶部分段器样式
::v-deep {
	.segmented-control {
		height: 100rpx;
		background-color: #fff;
	}
}
</style>
