<template>
	<view class="content">
		<view class="one">
			<input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索用户姓名">
		</view>
		<view class="two">
			<tabs :tabs="tabs" @tabsChange="tabsChange">
				<view class="tabsList">
					<scroll-view 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" @tap="toInfo(item)">
								<view class="list_1">
									<text>
										<text class="iconfont icon-yonghu"></text>
										{{item.name}}
									</text>
									<text class="status">{{item.zhStatus}}</text>
								</view>
								<view class="list_2">
									<view class="other">
										<text>联系电话：</text>
										<text>{{item.tel||'暂无'}}</text>
									</view>
									<view class="other">
										<text>性别：</text>
										<text>{{item.zhGender||'暂无'}}</text>
									</view>
									<view class="other">
										<text>角色：</text>
										<text>{{item.zhRole||'暂无'}}</text>
									</view>
									<view class="other">
										<text>所属街道：</text>
										<text>{{item.zhStreet||'暂无'}}</text>
									</view>
									<view class="other">
										<text>所属社区：</text>
										<text>{{item.zhCommunity||'暂无'}}</text>
									</view>
								</view>
								<view class="bottom" v-if="item.status=='0'">
									<button size="mini" type="default" @tap.stop="toExam(item,'0')">审核通过</button>
									<button size="mini" type="warn" @tap.stop="toExam(item,'1')">审核拒绝</button>
								</view>
							</view>
							<view class="is_bottom" v-if="is_bottom">
								<text>{{config.bottom_title}}</text>
							</view>
						</view>
					</scroll-view>
				</view>
			</tabs>
		</view>
	</view>
</template>

<script>
	import tabs from '../../components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				// 系统设置
				config: {},
				user: {},
				status: '',
				searchInfo: {},
				tabs: {
					active: '0',
					menu: []
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 字典表
				statusList: [],
				genderList: [],
				roleList: [],
				benlongList: []
			}
		},
		onLoad: function(e) {
			const that = this;
			that.$set(that, `status`, e.status || '0');
			that.$set(that.tabs, `active`, e.status || '0');
			that.searchConfig();
		},
		onShow: async function(e) {
			const that = this;
			await that.searchOther();
			that.searchToken();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						that.$set(that, `user`, res);
						that.clearPage();
						that.search();
					}
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				let user = that.user;
				let status = that.status;
				let info = {
					skip: that.skip,
					limit: that.limit,
					street: user.street,
					status: status,
				}
				let res;
				res = await that.$api(`/User/queryInfo`, 'GET', {
					...info,
					...that.searchInfo
				});
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						val.zhStatus = that.searchDict(val.status, 'status')
						val.zhGender = that.searchDict(val.gender, 'gender')
						val.zhRole = that.searchDict(val.role, 'role')
						val.zhStreet = that.searchDict(val.street, 'street')
						val.zhCommunity = that.searchDict(val.community, 'community')
					}
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
			},
			// 转换字典表
			searchDict(e, model) {
				const that = this;
				let data;
				if (model == 'status') {
					data = that.statusList.find((i) => i.value == e);
					if (data) return data.label
					else return '暂无'
				} else if (model == 'gender') {
					data = that.genderList.find((i) => i.value == e);
					if (data) return data.label
					else return '暂无'
				} else if (model == 'role') {
					data = that.roleList.find((i) => i.code == e);
					if (data) return data.name
					else return '暂无'
				} else if (model == 'street') {
					data = that.benlongList.find((i) => i._id == e);
					if (data) return data.name
					else return '暂无'
				} else if (model == 'community') {
					data = that.benlongList.find((i) => i._id == e);
					if (data) return data.name
					else return '暂无'
				}
			},
			// 确认发货
			async toExam(item, type) {
				const that = this;
				let user = that.user;
				let obj = {}
				if (type == '0') obj.status == '1'
				else obj.status == '2'
				uni.showModal({
					title: '提示',
					content: '是否要确认审核?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/User/${item._id}`, 'POST', obj);
							if (res.errcode == '0') {
								uni.showToast({
									title: '维护信息成功',
									icon: 'none'
								})
								that.clearPage()
								that.search()
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 查看详情
			toInfo(e) {
				console.log(e);
			},
			// 分页
			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;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.name) that.$set(that.searchInfo, `name`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询状态
				res = await that.$api(`/DictData`, 'GET', {
					type: 'status',
					is_use: '0'
				})
				if (res.errcode == '0') {
					that.$set(that, `statusList`, res.data);
					var menu = res.data.map((item) => {
						return {
							title: item.label,
							active: item.value
						}
					})
					that.$set(that.tabs, `menu`, menu)
				}
				// 性别
				res = await that.$api(`/DictData`, 'GET', {
					type: 'gender',
					is_use: '0'
				})
				if (res.errcode == '0') that.$set(that, `genderList`, res.data);
				// 角色
				res = await that.$api(`/Role`, 'GET', {
					is_use: '0'
				})
				if (res.errcode == '0') that.$set(that, `roleList`, res.data);
				// 街道/社区
				res = await that.$api(`/Office`, 'GET', {
					is_use: '0'
				})
				if (res.errcode == '0') that.$set(that, `benlongList`, res.data);
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.$set(that, `status`, e.active);
				that.clearPage();
				that.search()
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 2vw;

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

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

			.tabsList {
				position: relative;
				width: 100vw;
				height: 82vh;

				.list {
					background-color: #fff;
					border: 1px solid var(--f5Color);
					padding: 2vw;
					margin: 0 2vw 2vw 2vw;
					border-radius: 5px;

					.list_1 {
						display: flex;
						justify-content: space-between;
						padding: 2vw 0;
						font-size: var(--font16Size);

						.status {
							font-size: var(--font14Size);
							color: var(--fF0Color);
						}
					}

					.list_2 {
						.other {
							font-size: var(--font14Size);

							text:last-child {
								color: var(--f85Color);
							}
						}

					}

					.bottom {
						text-align: center;
						padding: 1vw 0;

						button {
							margin: 0 5px 0 0;
						}
					}
				}
			}
		}
	}

	.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);
		}
	}
</style>
