<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: 'type'
					},
					{
						label: '成果编号',
						model: 'num'
					},
					{
						label: '完成人',
						model: 'user_id'
					},
					{
						label: '完成单位',
						model: 'company'
					},
					{
						label: '研究方向',
						model: 'direction'
					},
					{
						label: '发表时间',
						model: 'time'
					},
				],
				list: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 1,
				// 字典表
				userList: [],
				directionList: [],
				typeList: [],
				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(`/otherAward`, 'GET', {
						...info,
						...that.searchInfo
					}, 'freeLabel')
					if (res.errcode == '0') {
						for (let val of res.data) {
							val.user_id = that.getDict(val.user_id, 'user_id')
							val.type = that.getDict(val.type, 'type')
							val.direction = that.getDict(val.direction, 'direction')
						}
						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 == 'user_id') {
					let list = [];
					for (const val of e) {
						let data = that.userList.find((i) => i._id == val);
						if (data) list.push(data.name);
					}
					return list.join(',');
				} else if (model == 'direction') {
					let data = that.directionList.find((i) => i._id == e);
					if (data) return data.name;
					else return '暂无';
				} else if (model == 'type') {
					let data = that.typeList.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: 'type'
					},
					{
						label: '成果编号',
						model: 'num'
					},
					{
						label: '成果名称',
						model: 'name'
					},
					{
						label: '完成人',
						model: 'user_id'
					},
					{
						label: '完成单位',
						model: 'company'
					},
					{
						label: '研究方向',
						model: 'direction'
					},
					{
						label: '发表时间',
						model: 'time'
					},
					{
						label: '是否公开',
						model: 'is_show',
						is_show: true,
					},
					{
						label: '附件',
						model: 'file',
						is_image: true
					},
				];
				let res = await that.$api(`/otherAward/${item._id}`, 'GET', {}, 'freeLabel')
				if (res.errcode == '0') {
					let info = res.data;
					//   整理数据
					if (info && info.type) info.type = that.getDict(info.type, 'type');
					if (info && info.user_id) info.user_id = that.getDict(info.user_id, 'user_id');
					if (info && info.direction) info.direction = that.getDict(info.direction, 'direction');
					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);
				}
				// 固定/流动人员 作者
				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);
				//类别
				res = await that.$api('/dictData', 'GET', {
					dict_type: 'cheng_resulttype',
					status: '0'
				}, 'jcyjdtglpt')
				if (res.errcode == '0') that.$set(that, `typeList`, 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);
			},
		}
	}
</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>