<template>
	<!-- 禁止滚动穿透 -->
	<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>
	<view class="main">
		<view class="one">
			<input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索姓名">
		</view>
		<view class="two">
			<list name="list" :list="list" :fields="fields" :page="page" :total="total" :pageSize="limit"
				@toPage="toPage" @toView="toView">
			</list>
		</view>
		<uni-popup ref="popup" background-color="#fff" type="center" :is-mask-click="false" @change="change">
			<view class="popup">
				<view class="close">
					<text>信息管理</text>
					<uni-icons @tap="toClose" type="close" size="20"></uni-icons>
				</view>
				<view class="info_1">
					<info name="info" :data="info" :infoFields="infoFields" :showList="showList"></info>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import list from '@/components/list/index.vue';
	import info from '@/components/view/index.vue';
	export default {
		components: {
			list,
			info
		},
		data() {
			return {
				id: '',
				user: {},
				searchInfo: {},
				fields: [{
						label: '姓名',
						model: 'name',
						is_name: true
					},
					{
						label: '年度',
						model: 'year'
					},

					{
						label: '指导教师',
						model: 'teacher'
					},
					{
						label: '学位',
						model: 'education'
					},
					{
						label: '状态',
						model: 'state'
					},
					{
						label: '完成时间',
						model: 'time'
					},
				],
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 1,
				// 字典表
				userList: [],
				degreeList: [],
				directionList: [],
				cardtypeList: [],
				showList: [],
				// 查看
				info: {},
				infoFields: [],
				// 禁止滚动穿透
				show: false
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			uni.showLoading({
				title: '正在加载请稍后',
				mask: true
			})
			await that.searchOther();
			that.searchToken();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			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);
					} else {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				if (that.id) {
					let info = {
						skip: that.skip,
						limit: that.limit,
						lab_id: that.id
					}
					const res = await that.$api(`/doctor`, 'GET', {
						...info,
						...that.searchInfo
					}, 'freeLabel')
					if (res.errcode == '0') {
						for (let val of res.data) {
							val.teacher = that.getDict(val.teacher, 'teacher')
							val.education = that.getDict(val.education, 'education')
						}
						that.$set(that, `list`, res.data)
						that.$set(that, `total`, res.total)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
				uni.hideLoading();
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.name) that.$set(that.searchInfo, `name`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.search();
			},
			// 查询字典表
			getDict(e, model) {
				const that = this;
				if (!e) return '暂无'
				if (model == 'teacher') {
					let data = that.userList.find((i) => i._id == e);
					if (data) return data.name;
					else return e || '暂无';
				} else if (model == 'direction') {
					let data = that.directionList.find((i) => i._id == e);
					if (data) return data.name;
					else return e || '暂无';
				} else if (model == 'education') {
					const data = that.degreeList.find((i) => i.dict_value == e);
					if (data) return data.dict_label;
					else return e || '暂无';
				} else if (model == 'cardtype') {
					const data = that.cardtypeList.find((i) => i.dict_value == e);
					if (data) return data.dict_label;
					else return e || '暂无';
				} else if (model == 'is_show') {
					const data = that.showList.find((i) => i.dict_value == e);
					if (data) return data.dict_label;
					else return e || '暂无';
				}
			},
			// 查看详情
			async toView(item) {
				const that = this;
				let infoFields = [{
						label: '年度',
						model: 'year'
					},
					{
						label: '姓名',
						model: 'name'
					},
					{
						label: '证件类型',
						model: 'cardtype'
					},
					{
						label: '证件号码',
						model: 'card'
					},
					{
						label: '指导教师',
						model: 'teacher'
					},
					{
						label: '研究方向',
						model: 'direction'
					},
					{
						label: '学位',
						model: 'education'
					},
					{
						label: '状态',
						model: 'state'
					},
					{
						label: '是否公开',
						model: 'is_show',
						is_show: true
					},
					{
						label: '附件',
						model: 'file',
						is_image: true
					},
				];
				let res = await that.$api(`/doctor/${item._id}`, 'GET', {}, 'freeLabel')
				if (res.errcode == '0') {
					let info = res.data;
					// 数据整理
					if (info && info.teacher) info.teacher = that.getDict(info.teacher, 'teacher');
					if (info && info.direction) info.direction = that.getDict(info.direction, 'direction');
					if (info && info.education) info.education = that.getDict(info.education, 'education');
					if (info && info.cardtype) info.cardtype = that.getDict(info.cardtype, 'cardtype');
					that.$set(that, `info`, info)
					that.$set(that, `infoFields`, infoFields)
					that.$refs.popup.open()
				}
			},
			// 关闭弹框
			toClose() {
				const that = this;
				that.$refs.popup.close();
			},
			// 分页
			toPage(data) {
				const that = this;
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				that.$set(that, `page`, data.page);
				that.$set(that, `skip`, data.skip)
				that.search();
			},
			async searchOther() {
				const that = this;
				let res;
				if (that.id) {
					//研究方向
					res = await that.$api('/direction', 'GET', {
						lab_id: that.id
					}, 'freeLabel')
					if (res.errcode == '0') that.$set(that, `directionList`, res.data);
				}
				//是否启用
				res = await that.$api('/dictData', 'GET', {
					dict_type: 'info_show',
					status: '0'
				}, 'jcyjdtglpt')
				if (res.errcode == '0') that.$set(that, `showList`, res.data);
				//证件类别
				res = await that.$api('/dictData', 'GET', {
					dict_type: 'leading_cadre_cardtype',
					status: '0'
				}, 'jcyjdtglpt')
				if (res.errcode == '0') that.$set(that, `cardtypeList`, res.data);
				//学位
				res = await that.$api('/dictData', 'GET', {
					dict_type: 'degree',
					status: '0'
				}, 'jcyjdtglpt')
				if (res.errcode == '0') that.$set(that, `degreeList`, res.data);
				// 固定/流动人员 作者
				let personList = [];
				res = await that.$api('/fixedpersonnel', 'GET', {
					status: '0'
				}, 'freeLabel')
				if (res.errcode == '0') personList = res.data;
				res = await that.$api('/flowpersonnel', 'GET', {
					status: '0'
				}, 'freeLabel')
				if (res.errcode == '0') personList.push(...res.data);
				that.$set(that, `userList`, personList);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		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 {
			background-color: var(--f9Color);
		}

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

		.popup {
			display: flex;
			flex-direction: column;
			width: 90vw;
			height: 40vh;
			background-color: var(--f9Color);

			.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;
				padding: 2vw;
			}
		}
	}
</style>