<template>
	<view class="container main">
		<view class="one">
			<view class="one_1">
				<input type="text" v-model="searchInfo.name" placeholder="请输入姓名">
			</view>
			<view class="one_2">
				<button size="mini" class="button" type="primary" @click="toInput">搜索</button>
			</view>
		</view>
		<view class="two">
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-view" @scrolltolower="toPage"
				@scroll="toScroll">
				<view class="list-scroll-view">
					<view class="list" v-for="(item, index) in list" :key="index">
						<view class="value">
							<view class="title">姓名：</view>
							<view class="label">{{item.name||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">手机号：</view>
							<view class="label">{{item.phone||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">身份证号：</view>
							<view class="label">{{item.card||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">电子邮箱：</view>
							<view class="label">{{item.email||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">微信/QQ：</view>
							<view class="label">{{item.communication||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">报名时间：</view>
							<view class="label">{{item.time||'暂无'}}</view>
						</view>
						<view class="bottom">
							<button v-if="item.status=='0'" class="button button_2" type="default" size="mini"
								@tap.stop="toExam(item)">审核</button>
						</view>
					</view>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottom_title||'到底了！'}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<uni-popup ref="popup" background-color="#fff" type="center" :is-mask-click="false" @change="change">
			<view class="popup">
				<view class="close">
					<text>审核信息</text>
					<text @click="toClose" class="t-icon t-icon-guanbi"></text>
				</view>
				<view class="info_1">
					<uni-forms ref="baseForm" :rules="rules" :modelValue="form" label-width="80px">
						<uni-forms-item label="审核状态" required name="status">
							<uni-data-select v-model="form.status" :localdata="statusList"
								@change="statusChange"></uni-data-select>
						</uni-forms-item>
					</uni-forms>
					<view class="button">
						<button type="primary" @click="submit('baseForm')">保存</button>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				user: {},
				config: {},
				searchInfo: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				statusList: [],
				// 禁止滚动穿透
				show: false,
				form: {
					status: "",
				},
				// 校验规则
				rules: {
					status: {
						rules: [{
							required: true,
							errorMessage: '状态不能为空'
						}]
					}
				}
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			await that.searchToken();
			await that.searchConfig();
			await that.searchOther();
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 禁止滚动穿透
			change(e) {
				const that = this;
				that.show = e.show
			},
			// 用户信息
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					}
				} catch (e) {}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {}
			},
			async searchOther() {
				const that = this;
				let res;
				// 查询证件类型
				res = await that.$api(`/dictData`, 'GET', {
					code: 'examStatus',
					is_use: '0',
				})
				if (res.errcode == '0') {
					for (let val of res.data) {
						val.text = val.label
						val.value = val.value
					}
					that.$set(that, `statusList`, res.data)
				}
			},
			// 查询
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
					match: that.id,
				}
				const res = await that.$api(`/sign`, 'GET', {
					...info,
					...that.searchInfo
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					});
				}
			},
			// 输入框
			toInput() {
				const that = this;
				if (!that.searchInfo.name) that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 修改
			toView(item) {
				const that = this;
				console.log(item);
			},
			// 审核
			toExam(item) {
				const that = this;
				that.$set(that, `form`, item)
				that.$refs.popup.open('center')
			},
			// 选择状态
			statusChange(e) {
				const that = this;
				let data = that.statusList.find(i => i.value == e);;
				if (data) {
					that.$set(that.form, `status`, data.value)
				}
			},
			// 保存
			submit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async res => {
					let arr = await that.$api(`/sign/${that.id}`, 'POST', res)
					if (arr.errcode == '0') {
						uni.showModal({
							content: "审核成功！",
							showCancel: false
						});
						that.toClose()
					} else {
						uni.showToast({
							title: arr.errmsg,
						});
					}
				}).catch(err => {
					console.log('err', err);
				})
			},
			// 关闭弹框
			toClose() {
				const that = this;
				that.$set(that, `form`, {});
				that.$refs.popup.close();
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
				// 检查滚动位置是否达到div显示的条件
				if (e.detail.scrollTop >= 300 && !that.showDiv) {
					that.showDiv = true;
				} else if (e.detail.scrollTop < 300 && that.showDiv) {
					that.showDiv = false;
				}
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 5)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.one {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vw;
			background-color: var(--mainColor);

			.one_1 {
				padding: 0 2vw;
				width: 75vw;

				input {
					padding: 2vw;
					background-color: var(--f1Color);
					font-size: var(--font14Size);
					border-radius: 5px;
				}
			}

			.one_2 {
				.button {
					background-color: var(--f3CColor);
					color: var(--mainColor);
				}
			}
		}

		.two {
			position: relative;
			flex-grow: 1;
			background-color: var(--f1Color);

			.list {
				background-color: var(--mainColor);
				border-bottom: 1px solid var(--f5Color);
				margin: 2vw 2vw 0 2vw;
				border-radius: 4px;
				padding: 3vw;


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

					.title {
						font-size: var(--font14Size);
						font-weight: bold;
					}

					.label {
						font-size: var(--font12Size);
						color: var(--f85Color);
					}
				}

				.bottom {
					margin: 2vw 0 0 0;
					text-align: center;

					.button {
						color: var(--mainColor);
						font-size: var(--font14Size);
						border-radius: 2vw;
					}

					.button_1 {
						margin: 0 2vw 0 0;
						background-color: var(--f3CColor);
					}

					.button_2 {
						background-color: var(--fF0Color);
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		width: 100%;
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}

	.uni-popup {
		z-index: 9999 !important;
	}

	.popup {
		display: flex;
		flex-direction: column;
		width: 90vw;
		height: 30vh;
		padding: 2vw;
		background-color: var(--mainColor);


		.close {
			display: flex;
			justify-content: space-between;
			padding: 2vw;

			text:first-child {
				font-size: var(--font16Size);
				font-weight: bold;
			}
		}

		.info_1 {
			position: relative;
			display: flex;
			flex-direction: column;
			height: 20vh;
			padding: 2vw;

			.button {
				margin: 2vw 0 0 0;

				button {
					background-color: var(--f3CColor);
					font-size: var(--font14Size);
					border-radius: 2vw;
				}
			}
		}
	}
</style>