<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">/{{ 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">{{ item.visitorType === 1 ? '学生家长预约' : '其他访客预约' }}</view>
					</view>
					<view class="info_box">
						<view class="box_key">来访时间</view>
						<view class="box_value">{{ item.visitorTime }}</view>
					</view>
					<view class="info_box">
						<view class="box_key">访问事由</view>
						<view class="box_value">{{ item.visitReason }}</view>
					</view>
					<view class="info_box">
						<view class="box_key">{{ item.visitorType === 1 ? '受访学生' : '受访人' }}</view>
						<view class="box_value">{{ item.respondentName }}</view>
					</view>
				</view>
				<view class="box_btn" v-if="item.statuStr == '待审核'">
					<view class="button reject" @click.stop="handleReject(item.id)">拒绝</view>
					<view class="button agree" @click.stop="handleAgree(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.visitorType === 1 ? '学生家长预约' : '其他访客预约' }}</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">{{ 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.carNum : '无' }}</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 class="pop_btn" v-if="popObj.statuStr == '待审核'">
					<view class="button reject" @click="handleReject(popObj.id)">拒绝</view>
					<view class="button agree" @click="handleAgree(popObj.id)">同意，并推送</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'

// 分段器当前索引
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 () => {
	const res = await myRequest({
		url: '/wanzai/api/smartVisitor/visitingAgencys',
		data: {
			currentPage: currentPage.value,
			pageCount: 6,
			userId: uni.getStorageSync('userInfo').id,
			type: activeCurrent.value - 0 + 1
		}
	})
	// console.log(res)
	const result = res.data
	total.value = result.totalCount
	list.value = [...list.value, ...(result.list || [])]
}

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

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

// 点击拒绝按钮回调
const handleReject = (id) => {
	handleExamine(id, 2)
}

// 点击同意按钮回调
const handleAgree = (id) => {
	handleExamine(id, 1)
}

// 审核回调
const handleExamine = (id, type) => {
	uni.showModal({
		title: '提示',
		content: type == 1 ? '确定同意并推送吗？' : '确定拒绝吗？',
		success: (res) => {
			if (res.confirm) {
				handleExamineReq(id, type)
			}
		}
	})
}

// 审核请求
const handleExamineReq = async (id, type) => {
	const res = await myRequest({
		url: '/wanzai/api/smartVisitor/examineRecord',
		data: {
			id,
			type
		}
	})
	// console.log(res)
	uni.showToast({
		title: type == 1 ? '已同意该审核' : '已拒绝该审核',
		icon: 'success',
		duration: 2000
	})
	setTimeout(() => {
		list.value = []
		currentPage.value = 1
		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: 146rpx;
					height: 80rpx;
					color: #fff;
					font-size: 28rpx;
					border-radius: 10rpx;
				}

				.reject {
					background-color: #d43030;
				}

				.agree {
					margin-left: 28rpx;
					width: 214rpx;
					background-color: #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;
			margin-top: 22rpx;
			padding: 0 35rpx;
			font-size: 28rpx;

			.info_key {
				color: #999999;
			}

			.info_value {
				flex: 1;
			}

			.blue {
				color: #0061ff;
			}

			.red {
				color: #d43030;
			}

			.green {
				color: #00baad;
			}
		}

		.pop_btn {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			margin-top: 28rpx;
			padding-top: 40rpx;
			box-sizing: border-box;
			height: 129rpx;
			border-top: 1rpx solid #e6e6e6;

			.button {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 146rpx;
				height: 80rpx;
				color: #fff;
				font-size: 28rpx;
				border-radius: 10rpx;
			}

			.reject {
				background-color: #d43030;
			}

			.agree {
				margin-left: 28rpx;
				margin-right: 36rpx;
				width: 214rpx;
				background-color: #0061ff;
			}
		}
	}
}

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