<!-- 实验室列表 -->
<template>
	<view id="laboratoryList">
		<view class='top-search-value-max-box'>
			<view class='top-search-value-box'>
				<img :src="imagesUrl('commonality/icon_sys_ss@1x.png')" @click="searchValueClick()">
				<input class="input-box" v-model="queryParams.searchValue" 
							 maxlength="10" placeholder="搜索实验室/房间号"
							 type="text" @confirm="searchValueClick()">
				<view v-if="queryParams.searchValue" class="button-box" @click="delSearchValue">清除</view>
			</view>
		</view>
		<view class="top-picker-max-box">
			<view class="top-picker-box">
				<picker @change="facultyChange" :value="facultyIndex" :range="facultyArray">
					<view class="picker-view">
						<view>{{!facultyIndex&&facultyIndex!=0?'二级单位':facultyArray[facultyIndex]}}</view>
						<img class="picker-img" :src="imagesUrl('commonality/icon_06.png')" alt="">
					</view>
				</picker>
			</view>
			<view class="top-picker-box">
				<picker @change="subjectChange" :value="subjectIndex" :range="subjectArray">
					<view class="picker-view">
						<view>{{!subjectIndex&&subjectIndex!=0?'分类':subjectArray[subjectIndex]}}</view>
						<img class="picker-img" :src="imagesUrl('commonality/icon_06.png')" alt="">
					</view>
				</picker>
			</view>
			<view class="top-picker-box">
				<picker @change="levelChange" :value="levelIndex" :range="levelArray">
					<view class="picker-view">
						<view>{{!levelIndex&&levelIndex!=0?'分级':levelArray[levelIndex]}}</view>
						<img class="picker-img" :src="imagesUrl('commonality/icon_06.png')" alt="">
					</view>
				</picker>
			</view>
		</view>
		<scroll-view scroll-y @scrolltolower="scrollGet" class="for-max-box">
			<img class="null-img" v-if="!dataList[0]" :src="imagesUrl('commonality/null-data-1.png')">
			<view class="for-box" v-for="(item,index) in dataList" :key="index" @click="tableButton('subDetail',item)">
				<view class="top-title-box">
					<view>
						{{item.subName}} {{item.roomName?'('+item.roomName+')':''}}
					</view>
				</view>
				<view class="bottom-text-max-box">
					<view class="bottom-text-box">
						<view v-if="item.levelName" class="lv-p" :style="'border:1rpx solid '+item.levelColor+';background:#fff;color:'+item.levelColor+';'">{{item.levelName}}</view>
						<view v-if="item.typeName" class="type-p">{{item.typeName}}</view>
						<view v-if="item.moldName" class="class-p">{{item.moldName}}</view>
						<view v-if="item.subArea" class="num-p">{{item.subArea}}m²</view>
					</view>
					<view class="bottom-address-box">
						<img :src="imagesUrl('commonality/icon_sys_dz@1x.png')">
						<view>{{item.deptName}}丨{{item.buildName}}-{{item.floorName}}</view>
					</view>
				</view>
			</view>
			<view class="get-data-null-p" v-if="getDataType">- 没有更多数据 -</view>
		</scroll-view>
		<tab-bar></tab-bar>
	</view>
</template>

<script>
	import tabBar from '@/pages/component/tabBar.vue'
	import $mqtt from '@/utils/mqtt.min.js';
	import {
		config
	} from '@/api/request/config.js'
	import {
		systemDeptDropList,
		laboratoryClassTypeGetList,
		laboratoryClassLevelGetList,
		laboratoryAppletSubList,
		laboratoryAppletGetSubDetailInfo
	} from '@/pages/api/index.js'
	export default {
		components: {
			tabBar,
		},
		data() {
			return {
				total: 0,
				dataList: [],
				getDataType: false,
				//院系选择器数据
				facultyList: [],
				facultyArray: [],
				facultyIndex: "",
				//分类选择器数据
				subjectList: [],
				subjectArray: [],
				subjectIndex: 0,
				//级别选择器数据
				levelList: [],
				levelArray: [],
				levelIndex: 0,
				// 查询参数
				queryParams: {
					page: 1,
					pageSize: 10,
					searchValue:"",
					deptId:"",
					typeId:"",
					levelId:"",
				},
			}
		},
		onLoad() {
			Promise.all([
				this.laboratoryClassTypeGetList(),
				this.laboratoryClassLevelGetList()
			]).then((result) => {
				this.systemDeptDropList();
			}).catch((error) => {
				uni.showToast({
					title: '数据异常,请稍候再试!',
					icon: "none",
					duration: 3000
				});
			})
		},
		onShow() {
		},
		methods: {
			//选择学院
			facultyChange: function(e) {
				if (this.facultyArray[0]) {
					this.facultyIndex = parseInt(e.target.value);
					this.$set(this.queryParams,'page',1);
					this.getList();
				}
			},
			//选择类型
			subjectChange: function(e) {
				if (this.subjectArray[0]) {
					this.subjectIndex = parseInt(e.target.value);
					this.$set(this.queryParams,'page',1);
					this.getList();
				}
			},
			//选择级别
			levelChange: function(e) {
				if (this.levelArray[0]) {
					this.levelIndex = parseInt(e.target.value);
					this.$set(this.queryParams,'page',1);
					this.getList();
				}
			},
			//获取院系
			async systemDeptDropList() {
				const {
					data
				} = await systemDeptDropList({
					deptName: '',
					level: 2,
					deptType: 1
				});
				if (data.code == 200) {
					let list = [];
					for (let i = 0; i < data.data.length; i++) {
						list.push(data.data[i].deptName)
					}
					if(uni.getStorageSync('subAdmin')==1){
						this.facultyArray.push('我的实验室')
						this.facultyList.push({
							deptName: "我的实验室",
							deptId: "-1"
						})
					}
					if(config.schoolId == uni.getStorageSync('user').position){
						this.facultyArray.push('全部')
						this.facultyList.push({
							deptName: "全部",
							deptId: ""
						})
						this.facultyArray = this.facultyArray.concat(list)
						this.facultyList = this.facultyList.concat(data.data)
					}else{
						this.facultyArray.push(uni.getStorageSync('userDeptName'))
						this.facultyList.push({
							deptName: uni.getStorageSync('userDeptName'),
							deptId: uni.getStorageSync('userDeptId')
						})
					}
					if(this.facultyList[0]){
						this.$set(this,'facultyIndex',0);
						this.getList()
					}
				}
			},
			//分类下拉列表
			async laboratoryClassTypeGetList() {
				const {
					data
				} = await laboratoryClassTypeGetList();
				if (data.code == 200) {
					let list = [];
					for (let i = 0; i < data.data.length; i++) {
						list.push(data.data[i].subType)
					}
					this.subjectArray = list;
					list.unshift('全部分类');
					this.subjectList = data.data;
					this.subjectList.unshift({
						subType: "全部分类",
						typeId: ""
					})
				}
			},
			//分级下拉列表
			async laboratoryClassLevelGetList() {
				const {
					data
				} = await laboratoryClassLevelGetList();
				if (data.code == 200) {
					let list = [];
					for (let i = 0; i < data.data.length; i++) {
						list.push(data.data[i].titleName)
					}
					this.levelArray = list;
					list.unshift('全部分级');
					this.levelList = data.data;
					this.levelList.unshift({
						titleName: "全部分级",
						levelId: ""
					})
				}
			},
			//滚动加载事件
			scrollGet() {
				let self = this;
				if (self.total / self.queryParams.pageSize <= self.queryParams.page) {
					this.$set(this, 'getDataType', true);
				} else {
					this.queryParams.page += 1;
					this.$nextTick(() => {
						this.getList();
					})
				}
			},
			delSearchValue(){
				this.$set(this.queryParams,'searchValue','')
				this.$set(this.queryParams,'page',1);
				this.$nextTick(() => {
					this.getList();
				})
			},
			searchValueClick(){
				if(this.facultyList[0]){
					this.$set(this.queryParams,'page',1);
					this.$nextTick(() => {
						this.getList();
					})
				}
			},
			//获取实验室列表
			async getList() {
				if(!this.facultyList[0]||uni.getStorageSync('userType')==2){
					return
				}
				let self = this;
				let obj = JSON.parse(JSON.stringify(this.queryParams));
				obj.deptId = this.facultyIndex||this.facultyIndex==0 ? this.facultyList[this.facultyIndex].deptId : '';
				obj.typeId = this.subjectIndex||this.subjectIndex==0 ? this.subjectList[this.subjectIndex].typeId : '';
				obj.levelId = this.levelIndex||this.levelIndex==0 ? this.levelList[this.levelIndex].levelId : '';
				const {
					data
				} = await laboratoryAppletSubList(obj);
				if (data.code == 200) {
					if(self.queryParams.page == 1){
						this.dataList = data.data.records;
						this.total = data.data.total;
						if (data.data.total / self.queryParams.pageSize <= self.queryParams.page) {
							this.$set(this, 'getDataType', true);
						}
					}else{
						this.dataList = [...this.dataList, ...data.data.records]
						this.total = data.data.total;
						if (data.data.total / self.queryParams.pageSize <= self.queryParams.page) {
							this.$set(this, 'getDataType', true);
						}
					}
				}
			},
			tableButton(type, row) {
				let self = this;
				if (type == 'subDetail') {
					//实验室详情
					this.laboratoryAppletGetSubDetailInfo(row.infoId)
				}
			},
			//获取实验室详情
			async laboratoryAppletGetSubDetailInfo(infoId) {
				const {
					data
				} = await laboratoryAppletGetSubDetailInfo({
					infoId: infoId
				});
				if (data.code == 200) {
					uni.navigateTo({
						url: '/pages_manage/views/laboratory/safetyCardScan?infoData=' + encodeURIComponent(JSON.stringify(data.data))+'&saoCode=true'
						// url: '/pages_manage/views/laboratory/infoPage?infoData=' + encodeURIComponent(JSON.stringify(data.data))
					});
				}
			},
		},
	}
</script>

<style lang="stylus" scoped>
	@import '@/api/request/imagesUrl.styl';
	#laboratoryList {
		height: 100%;
		display flex;
		flex-direction column;
		.top-search-value-max-box{
			background-color: #fff;
			.top-search-value-box{
				display: flex;
				border:50rpx;
				height:80rpx;
				border:1px solid #E0E0E0;
				border-radius:40rpx;
				margin: 20rpx 30rpx 10rpx;
				img{
					width:26rpx;
					height:26rpx;
					margin:27rpx 30rpx;
				}
				.input-box{
					height:78rpx;
					line-height:78rpx;
					flex:1;
				}
				.button-box{
					width:120rpx;
					line-height:78rpx;
					height:78rpx;
					margin-right:10rpx;
					text-align: center;
					color:#0183FA;
					font-size:24rpx;
				}
			}
		}
		.top-picker-max-box {
			display: flex;
			padding: 0 20rpx;
			background #fff;
			border-bottom: 1rpx solid #E0E0E0;

			.top-picker-box {
				line-height: 80rpx;
				height: 80rpx;

				.picker-view {
					display flex;

					view {
						display: block;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						font-size: 28rpx;
					}

					.picker-img {
						width: 24rpx;
						height: 13rpx;
						margin-top: 36rpx;
						margin-left: 13rpx;
					}
				}
			}

			.top-picker-box:nth-child(1) {
				text-align left;
				width: 320rpx;

				.picker-view {
					view {
						max-width: 260rpx;
					}
				}
			}

			.top-picker-box:nth-child(2) {
				text-align left;
				width: 215rpx;

				.picker-view {
					view {
						max-width: 156rpx;
					}
				}
			}

			.top-picker-box:nth-child(3) {
				text-align right;
				width: 195rpx;

				.picker-view {
					view {
						flex: 1;

					}
				}
			}
		}

		.for-max-box {
			flex: 1;

			overflow-y scroll;

			.null-img {
				display block;
				width: 276rpx;
				height: 321rpx;
				position absolute;
				top: 200rpx;
				left: 274rpx;
			}

			.for-box:nth-child(1) {
				border-top: none;
			}

			.for-box {
				width:690rpx;
				margin:20rpx 30rpx 0;
				.top-title-box{
					height:126rpx;
					width:690rpx;
					background: url($imagesUrl+'commonality/img_sys_bg@1x.png');
					background-size 100%;
					background-repeat: no-repeat;
					view{
						height:42rpx;
						line-height:42rpx;
						font-size:30rpx;
						color:#333333;
						padding:20rpx 37rpx;
						display:block;
						overflow:hidden;
						text-overflow:ellipsis;
						white-space:nowrap;
					}
				}
				.bottom-text-max-box{
					overflow: hidden;
					border-radius:0 0 20rpx 20rpx;
					padding-bottom:26rpx;
					width:690rpx;
					background #ffffff;
					.bottom-text-box{
						padding:0 30rpx;
						view{
							margin-left:10rpx;
							margin-bottom:5rpx;
							display:inline-block;
							overflow: hidden;
							padding:0 20rpx;
							text-align: center;
							border-radius:10rpx;
						}
						.lv-p{
							height:38rpx;
							line-height:36rpx;
							font-size:28rpx;
							color: #FF0000;
						}
						.type-p,.class-p,.num-p{
							height:40rpx;
							line-height:38rpx;
							font-size:24rpx;
							color:#0183FA;
							background: rgba(1,131,250,0.2);
						}
					}
					.bottom-address-box{
						display: flex;
						padding:21rpx 30rpx 0;
						img{
							display: block;
							width:30rpx;
							height:30rpx;
							margin:0 15rpx 0 12rpx;
						}
						view{
							height:30rpx;
							line-height:30rpx;
							font-size:28rpx;
							display:block;
							overflow:hidden;
							text-overflow:ellipsis;
							white-space:nowrap;
						}
					}
				}
			}
			.get-data-null-p {
				text-align: center;
				height: 100rpx;
				line-height: 100rpx;
				color: #999;
				padding-bottom:200rpx;
			}
		}
	}
</style>
