<!-- 远程开门 -->
<template>
	<view id="empowerOpen">
		<view class="header">
			<view class="header-n">
				<input class="header-l" type="text" @confirm="subNameChange()" v-model="getData.searchValue" placeholder="实验室名称">
				<view class="header-r" v-if="getData.searchValue" @click="clearSearch()">
					<img :src="imagesUrl('commonality/clear.png')">
				</view>
			</view>
		</view>
		<scroll-view scroll-y @scrolltolower="scrollGet" class="list">
			<view class="list-li" v-for="(item,index) in dataList">
				<view class="list-li-l">{{item.subName}}{{item.roomNum?'（'+item.roomNum+'）':''}}</view>
				<view class="list-li-r" @click="openButton(item)">
					<view class="null-p"></view>
					<view>开门</view>
					<view class="null-p"></view>
				</view>
			</view>
			<view class="get-data-null-p" v-if="getDataType">- 没有更多数据 -</view>
			<view class="get-data-null-p" v-else="getDataType">- 滑动加载更多 -</view>
		</scroll-view>
		<view class="position-button" @click="goPage()">授权记录</view>
		<view class="success" v-if="successVisible">
			<view class="null-box" @click="closeTip()"></view>
			<view class="panel">
				<img class="panel-t" v-if="authorizeStatus==1||authorizeStatus==3" :src="imagesUrl('commonality/icon_kcxq_cg.png')"/>
				<img class="panel-t" v-if="authorizeStatus==2" :src="imagesUrl('commonality/icon_kcxq_sb.png')"/>
				<view class="panel-m">{{authorizeStatus==1?'开门成功':(authorizeStatus==2?'开门失败':(authorizeStatus==3?'开门中请稍后':''))}}
				</view>
				<view class="panel-b">({{count}}S)</view>
			</view>
		</view>
	</view>

</template>

<script>
	import {
		config
	} from '@/api/request/config.js'
	import {
		systemSubjectGetAppSubjectByLogin,
		laboratoryApplyRemoteOpenLock,
		applyRemoteOpenLockConfirm,
		laboratoryLabOpenLockLogUpdateLockData
	} from '@/pages_manage/api/index.js'
	export default {
		components: {

		},
		data() {
			return {
				pageType: 1,
				baseUrl: config.base_url,
				newData: {

				},
				addForm: {

				},
				dataList: [],
				authorizeStatus: 2, //1成功 2失败
				successVisible: false,
				timer: null,
				count: '',
				total: 0,
				getData: {
					page: 1,
					pageSize: 20,
					searchValue: '',
					// adminId: uni.getStorageSync('userId'),
				},
				getDataType: false,
				msgId:'',
			}
		},
		onLoad(option) {

		},
		onShow() {

		},
		mounted() {
			this.systemSubjectGetAppSubjectByLogin();
		},
		methods: {
			//跳转授权记录
			goPage(){
				uni.navigateTo({
					url: '/pages_manage/views/accessControl/authorizationRecord'
				});
			},
			openButton(row) {
				let self = this;
				uni.showModal({
					content: '确定对 ' + row.subName + ' 进行远程开门操作?',
					cancelColor: "#999",
					confirmColor: "#0183FA",
					success: function(res) {
						if (res.confirm) {
							self.openClick(row);
						}
					}
				});
			},
			//开门
			async openClick(row) {
				let self = this;
				let obj = {
					labId: row.subId,
				}
				const {
					data
				} = await laboratoryApplyRemoteOpenLock(obj);
				if (data.code == 200) {
					if (data.data[0]) {
						this.$set(this, 'msgId', data.data[0])
						this.$set(this, 'authorizeStatus', 3)
						this.$set(this, 'successVisible', true)
						this.getMsg(row.subId);
					}else{
						this.$set(this, 'authorizeStatus', 2)
						this.$set(this, 'successVisible', true)
						this.getCode();
					}
				}
			},
			async applyRemoteOpenLockConfirm(subId) {
				let self = this;
				let obj = {
					messageId: this.msgId,
				}
				const {
					data
				} = await applyRemoteOpenLockConfirm(obj);
				if (data.code == 200) {
					if (data.data) {
						this.$set(this, 'authorizeStatus', 1)
						this.$set(this, 'successVisible', true)
						this.laboratoryLabOpenLockLogUpdateLockData(subId,1);
						this.getCode();
					} else {
						this.$set(this, 'authorizeStatus', 2)
						this.$set(this, 'successVisible', true)
						this.laboratoryLabOpenLockLogUpdateLockData(subId,0);
						this.getCode();
					}
					this.$set(this, 'msgId','')
				}
			},
			async laboratoryLabOpenLockLogUpdateLockData(subId,type) {
				let self = this;
				let obj = {
					subjectId: subId,
					userId: uni.getStorageSync('userId'),
					openStatus:type
				}
				const {
					data
				} = await laboratoryLabOpenLockLogUpdateLockData(obj);
				if(data.code == 200) {
					
				}
			},
			
			//滚动加载事件
			scrollGet() {
				let self = this;
				if (self.total / self.getData.pageSize <= self.getData.page) {
					this.$set(this, 'getDataType', true);
				} else {
					this.getData.page += 1;
					this.$nextTick(() => {
						this.systemSubjectGetAppSubjectByLogin();
					})
				}
			},
			//获取实验室
			async systemSubjectGetAppSubjectByLogin() {
				let self = this;
				const {
					data
				} = await systemSubjectGetAppSubjectByLogin(this.getData);
				if (data.code == 200) {
					if (self.getData.page == 1) {
						this.dataList = data.data.records;
						this.total = data.data.total;
						if (data.data.total / self.getData.pageSize <= self.getData.page) {
							this.$set(this, 'getDataType', true);
						}
					} else {
						this.dataList = [...this.dataList, ...data.data.records]
						this.total = data.data.total;
						if (data.data.total / self.getData.pageSize <= self.getData.page) {
							this.$set(this, 'getDataType', true);
						}
					}
				}
			},
			//实验室搜索
			subNameChange() {
				this.$set(this, 'getDataType', false);
				this.$set(this, 'dataList', []);
				this.$set(this, 'total', 0);
				this.$set(this.getData, 'page', 1);
				this.systemSubjectGetAppSubjectByLogin();
			},
			//实验室搜索框清除
			clearSearch() {
				this.$set(this, 'getDataType', false);
				this.$set(this, 'dataList', []);
				this.$set(this, 'total', 0);
				this.$set(this.getData, 'page', 1);
				this.$set(this.getData, 'searchValue', '');
				this.systemSubjectGetAppSubjectByLogin();
			},
			//关闭弹框倒计时
			getMsg(subId) {
				let self = this;
				const TIME_COUNT = 5;
				if (!self.timer) {
					self.count = TIME_COUNT;
					self.show = false;
					self.timer = setInterval(() => {
						if (self.count > 0 && self.count <= TIME_COUNT) {
							self.count -= 1;
						} else {
							self.successVisible = false;
							clearInterval(self.timer);
							self.timer = null;
							self.applyRemoteOpenLockConfirm(subId);
						}
					}, 1000);
				}
			},
			//关闭弹框倒计时
			getCode() {
				let self = this;
				const TIME_COUNT = 3;
				if (!this.timer) {
					this.count = TIME_COUNT;
					this.show = false;
					this.timer = setInterval(() => {
						if (this.count > 0 && this.count <= TIME_COUNT) {
							this.count -= 1;
						} else {
							self.successVisible = false;
							clearInterval(this.timer);
							this.timer = null;
						}
					}, 1000);
				}
			},
			closeTip() {
				this.$set(this, 'successVisible', false)
			},
		}
	}
</script>

<style lang="stylus" scoped>
	#empowerOpen {
		height: 100%;
		display flex;
		flex-direction column;

		.header {
			width: 749rpx;
			height: 120rpx;
			background: #FFFFFF;
			padding: 20rpx 30rpx;
			box-sizing: border-box;
			border-bottom: 1rpx solid #D8D8D8;

			.header-n {
				position: relative;
				width: 690rpx;
				height: 80rpx;
				border-radius: 10rpx 10rpx 10rpx 10rpx;
				border: 1rpx solid #E0E0E0;
				padding-left: 20rpx;
				box-sizing: border-box;

				.header-l {
					width: 590rpx;
					height: 80rpx;
				}

				.header-r {
					position: absolute;
					top: 0rpx;
					right: 0rpx;
					width: 68rpx;
					height: 80rpx;

					>img {
						width: 24rpx;
						height: 24rpx;
						margin-top: 24rpx;
						margin-left: 24rpx;
					}
				}
			}

		}
		.position-button{
			position: absolute;
			bottom:50rpx;
			left:75rpx;
			width:600rpx;
			height:100rpx;
			line-height:100rpx;
			text-align: center;
			background-color: #0183FA;
			font-size:28rpx;
			color:#fff;
			border-radius:20rpx;
		}
		.list {
			flex: 1;
			overflow-y scroll;
			padding: 0 30rpx;
			box-sizing: border-box;
			background: #fff;
			padding-bottom:160rpx;

			.list-li {
				// height: 90rpx;
				display: flex;
				// justify-content: space-between;
				// align-items: center;
				border-bottom: 1rpx solid #D8D8D8;

				.list-li-l {
					flex: 1;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #333333;
					line-height: 40rpx;
					padding: 25rpx 25rpx 25rpx 0;
				}

				.list-li-r {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #0183FA;
					line-height: 90rpx;
					display: flex;
					flex-direction: column;
					text-align: right;
					width: 80rpx;

					.null-p {
						flex: 1;
					}
				}
			}

			.list-li:last-child {
				border-bottom: none;
			}
		}

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

		.get-data-null-p {
			text-align: center;
			line-height: 100rpx;
			padding-bottom: 80px;
			color: #999;
		}

		.success {
			height: 100%;
			width: 100%;
			position: fixed;
			top: 0;
			display: flex;
			flex-direction: column;
			z-index: 10;
			background: rgba(0, 0, 0, 0.2);

			.null-box {
				flex: 1;
			}

			.panel {
				width: 550rpx;
				height: 255rpx;
				background: #FFFFFF;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				position: absolute;
				top: 320rpx;
				left: 100rpx;
				display: flex;
				flex-direction: column;
				align-items: center;

				.panel-t {
					width: 80rpx;
					height: 80rpx;
					margin: 40rpx 0 18rpx 0;
				}

				.panel-m {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #3D3D3D;
					line-height: 42rpx;
					margin-bottom: 8rpx;
				}

				.panel-b {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #999999;
					line-height: 42rpx;
				}
			}
		}
	}
</style>