<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">
			<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="toView(item)">
								<view class="name">{{item.name||'暂无名称'}}</view>
								<view class="other">
									<view class="other_1 textOne">
										批建时间：{{item.build_time||'暂无'}}
									</view>
									<view class="other_1 textOne">
										依托单位名称：{{item.unit_name||'暂无'}}
									</view>
									<view class="other_1 textOne" v-if="item.assistant">
										科研助理姓名：<text :user-select='true'
											:selectable="true">{{item.assistant.name||'暂无'}}</text>
									</view>
									<view class="other_1 textOne" v-if="item.assistant">
										科研助理手机号：<text :user-select='true'
											:selectable="true">{{item.assistant.phone||'暂无'}}</text>
									</view>
									<view class="status textOne">
										状态：{{item.zhStatus||'暂无'}}
									</view>
								</view>
								<view class="button">
									<button class="warning" v-if="user.role_type == '0' ||  user.role_type == '1'"
										size="mini" type="warn" @tap.stop="toEdit(item)">修改</button>
								</view>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>没数据到底了！</text>
						</view>
					</scroll-view>
				</view>
			</tabs>
		</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">
					<view class="info">
						<uni-forms class="form" ref="form" :rules="rules" :modelValue="form" label-width='90'>
							<uni-forms-item label="状态" name="status">
								<uni-data-select v-model="form.status" :localdata="statusList" @change="statusChange">
								</uni-data-select>
							</uni-forms-item>
							<uni-forms-item label="批建时间" required name="build_time">
								<uni-datetime-picker type="date" v-model="form.build_time" />
							</uni-forms-item>
						</uni-forms>
						<view class="bottom">
							<button size="mini" class="button" type="primary" @click="submit('form')">提交保存</button>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import tabs from '../../components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				user: {},
				searchInfo: {},
				status: '-10',
				tabs: {
					active: '-10',
					bgColor: '#ffffff',
					menu: []
				},
				form: {},
				// 校验规则
				rules: {
					status: {
						rules: [{
							required: true,
							errorMessage: '请选择状态'
						}]
					}
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 字典表
				statusList: [],
				// 禁止滚动穿透
				show: false
			}
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.searchOther();
			await that.searchToken();
			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);
					} else {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				} 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,
				}
				if (status != '-10') info.status = status
				const res = await that.$api(`/basic`, 'GET', {
					...info,
					...that.searchInfo
				}, 'freeLabel')
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						const status = that.statusList.find(i => i.value == val.status)
						if (status) val.zhStatus = status.text
					}
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.$set(that, `status`, e.active)
				that.clearPage();
				that.search()
			},
			// 输入框
			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();
			},
			// 查看
			toView(item) {
				uni.navigateTo({
					url: `/pagesBasic/basic/info?id=${item._id||item.id}`
				})
			},
			// 修改
			toEdit(item) {
				const that = this;
				that.$set(that, `form`, item)
				that.$refs.popup.open()
			},
			statusChange(status) {
				const that = this;
				that.$set(that.form, `status`, status);
			},
			submit(ref) {
				const that = this;
				const form = that.form;
				that.$refs[ref].validate().then(async data => {
					let res = await that.$api(`/basic/${form._id}`, 'POST', data, 'freeLabel');
					if (res.errcode == '0') {
						uni.showToast({
							title: '信息维护成功',
							icon: 'none'
						})
						that.toClose()
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						})
					}
				}).catch(err => {
					console.log('err', err);
				})
			},
			// 关闭弹框
			toClose() {
				const that = this;
				that.$refs.popup.close();
			},
			async searchOther() {
				const that = this;
				let res;
				//状态
				res = await that.$api('/dictData', 'GET', {
					dict_type: 'label_status',
					status: '0'
				}, 'jcyjdtglpt')
				if (res.errcode == '0') {
					const statusList = res.data.map((item) => {
						return {
							value: item.dict_value,
							text: item.dict_label
						}
					})
					that.$set(that, `statusList`, statusList);
					const menu = res.data.map((item) => {
						return {
							title: item.dict_label,
							active: item.dict_value
						}
					})
					menu.unshift({
						title: '全部',
						active: '-10'
					})
					that.$set(that.tabs, `menu`, menu)
				}
			},
			// 分页
			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);
			},
			// 清空列表
			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" 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 {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);

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

				.list:first-child {
					margin: 2vw;
				}

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

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

					.other {
						padding: 2px 0 0 0;
						font-size: var(--font12Size);
						color: var(--f85Color);
					}
					.status{
						padding: 2px 0 0 0;
						font-size: var(--font12Size);
						color: var(--fF0Color);
					}

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

						.warning {
							background: var(--f3CColor);
						}

						button {
							margin: 0 1vw 0 0;
						}
					}
				}
			}
		}

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

		.popup {
			display: flex;
			flex-direction: column;
			width: 90vw;
			height: 35vh;
			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;
				max-height: 25vh;
				padding: 2vw;

				.info {
					.bottom {
						text-align: center;

						.button {
							background-color: var(--f07CColor);
						}
					}
				}
			}
		}
	}

	.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>