<!-- 授权开门 -->
<template>
	<view id="empowerOpen">
		<view class="page-one" v-if="pageType==1">
			<view class="header">
				<view class="header-n">
					<input class="header-l" type="text" @change="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}}）</view>
					<view class="list-li-r" @click="authorizedClick(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>
		<view class="page-tow" v-if="pageType==2">
			<view class="list-tow">
				<view class="list-tow-li">
					<view class="list-tow-li-t">被授权人</view>
					<view class="list-user-name-box" v-if="addForm.userId">
						<view>{{addForm.userName}}{{account?'-'+account:''}}</view>
						<view @click="delUserData()">更改</view>
					</view>
					<input class="list-tow-li-b" v-if="!addForm.userId" type="text" @change="authorizedChange()"
						v-model="getDataTow.searchValue" placeholder="被授权人搜索,最少两个字">
					<uni-data-picker @change="bindPickerChange":localdata="dataListTow" ref='picker'>
					</uni-data-picker>
				</view>
				<view class="list-tow-li">
					<view class="list-tow-li-t">生效时间</view>
					<view class="time">
						<view class="example-body">

						</view>
						<view class="time-l">
							<uni-datetime-picker type="datetime" hide-second="true" :border="true"
								v-model="addForm.validBeginTime" @change="changeLog" placeholder="开始时间" />
						</view>
						<view class="time-c">-</view>
						<view class="time-r">
							<uni-datetime-picker type="datetime" hide-second="true" :border="true"
								v-model="addForm.validEndTime" @change="changeLog" placeholder="结束时间" />
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="bottom" v-if="pageType==2">
			<view class="bottom-l" @click="cancelClick()">取消</view>
			<view class="bottom-r" @click="submitForm()">确定</view>
		</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" :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?'授权成功':'授权失败'}}</view>
				<view class="panel-b">({{count}}S)</view>
			</view>
		</view>
	</view>

</template>

<script>
	import {
		config
	} from '@/api/request/config.js'
	import {
		systemSubjectGetAppSubject,
    laboratoryApplyGetUserInfo,
		laboratoryApplyAddAddApply
	} from '@/pages_manage/api/index.js'
	export default {
		components: {

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

				},
				currentDate: '',
				addForm: {
					userId: '',
					subId: '',
					validBeginTime: '',
					validEndTime: '',
				},
				total: 0,
				getData: {
					page: 1,
					pageSize: 20,
					searchValue: '',
					adminId: uni.getStorageSync('userId'),
				},
				getDataType: false,
				getDataTow: {
					searchValue: '',
					labId: '',
				},
				dataList: [],
				dataListTow: [], //被授权人
				authorizeStatus: 0, //1成功 2失败
				successVisible: false,
				timer: null,
				count: '',
				//展示用工号
				account:false,
			}
		},
		onLoad(option) {

		},
		onShow() {

		},
		mounted() {
			this.currentDate = this.getdate();
			this.systemSubjectGetAppSubject();
		},
		methods: {
			changeLog(e) {
				console.log('change事件:', e);
			},
			getdate() {
				var date = new Date();
				var seperator1 = "-";
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var strDate = date.getDate();

				if (month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if (strDate >= 0 && strDate <= 9) {
					strDate = "0" + strDate;
				}
				var currentdate = year + "-" + month + "-" + strDate;
				return currentdate;
			},
			//清空选项
			delUserData(){
				this.$set(this.addForm,'userName',null);
				this.$set(this.addForm,'userId',null);
				this.$set(this,'account',false);
			},
			//被授权人搜索
			async authorizedChange() {
				let self = this
				self.dataListTow = []
				const {
					data
				} = await laboratoryApplyGetUserInfo(this.getDataTow)
				if (data.code == 200) {
					data.data.forEach(function(item) {
						self.dataListTow.push({
							text: item.userName+(item.account?'-'+item.account:''),
							value: item.userId,
							userName: item.userName,
							account: item.account,
						})
					})
					self.$refs.picker.show()
				}
			},
			bindPickerChange: function(e) {
				let self = this;
				for(let i=0;i<self.dataListTow.length;i++){
					if(e.detail.value[0].value == self.dataListTow[i].value){
						this.$set(self.addForm,'userName',self.dataListTow[i].userName);
						this.$set(self.addForm,'userId',self.dataListTow[i].value);
						this.$set(self,'account',self.dataListTow[i].account?self.dataListTow[i].account:false);
						return
					}
				}
			},
			//授权点击
			authorizedClick(row) {
				this.$set(this, 'pageType', 2)
				this.$set(this.getDataTow, 'labId', row.subId)
				this.$set(this.addForm, 'subId', row.subId)

			},
			//滚动加载事件
			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.systemSubjectGetAppSubject();
					})
				}
			},
			//获取实验室
			async systemSubjectGetAppSubject() {
				let self = this;
				const {
					data
				} = await systemSubjectGetAppSubject(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.systemSubjectGetAppSubject();
			},
			//实验室搜索框清除
			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.systemSubjectGetAppSubject();
			},
			cancelClick() {
				this.$set(this, 'pageType', 1)
			},
			//授权提交
			async submitForm() {
				let self = this;
				if(!self.addForm.userId){
						uni.showToast({
							title: '请选择被授权人',
							icon: "none",
							duration: 2000
						});
						return
				}
				if(!self.addForm.validBeginTime || !self.addForm.validEndTime){
						uni.showToast({
							title: '请选择授权时间',
							icon: "none",
							duration: 2000
						});
						return
				}
				let obj={
					userId:this.addForm.userId,
					subId: this.addForm.subId,
					validBeginTime:this.addForm.validBeginTime.replace(' ','T')+':00',
					validEndTime:this.addForm.validEndTime.replace(' ','T')+':59',
				}
				const {
					data
				} = await laboratoryApplyAddAddApply(obj);
				if (data.code == 200) {
					this.$set(this, 'authorizeStatus', 1)
					this.$set(this, 'successVisible', true)
					this.getCode();
				}else{
					this.$set(this, 'authorizeStatus', 2)
					this.$set(this, 'successVisible', true)
					this.getCode();
				}


			},
			//关闭弹框倒计时
			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;
		.page-one {
			flex:1;
			display flex;
			flex-direction column;
			overflow: hidden;
			.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;
						}
					}
				}

			}

			.list {
				flex: 1;
				overflow-y scroll;
				padding: 0 30rpx;
				box-sizing: border-box;
				background: #fff;

				.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;
				}
			}
			.get-data-null-p {
				text-align: center;
				line-height: 100rpx;
				padding-bottom: 80px;
				color: #999;
			}
		}

		.page-tow {
			.list-tow {
				padding: 0 30rpx 50rpx;
				box-sizing: border-box;
				background: #fff;

				.list-tow-li {
					overflow: hidden;

					.list-tow-li-t {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 30rpx;
						color: #333333;
						line-height: 42rpx;
						text-align: left;
						margin: 24rpx 0;
					}
					.list-user-name-box{
						display: flex;
						width: 690rpx;
						height: 80rpx;
						border-radius: 10rpx 10rpx 10rpx 10rpx;
						border: 1rpx solid #e0e0e0;
						padding-left: 20rpx;
						box-sizing: border-box;
						view{
							line-height:78rpx;
						}
						view:nth-child(1){
							paddin:0 10rpx;
							flex:1;
						}
						view:nth-child(2){
							width:100rpx;
							text-align: center;
							border-left: 1rpx solid #e0e0e0;
						}
					}
					.list-tow-li-b {
						width: 690rpx;
						height: 80rpx;
						border-radius: 10rpx 10rpx 10rpx 10rpx;
						border: 1rpx solid #E0E0E0;
						padding-left: 20rpx;
						box-sizing: border-box;
					}

					.time {
						display: flex;
						justify-content: flex-start;
						align-items: center;
						width: 690rpx;
						height: 80rpx;
						border-radius: 10rpx 10rpx 10rpx 10rpx;
						//border: 1rpx solid #E0E0E0;

						.time-l {
							height: 80rpx;
							width: 324rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 28rpx;
							color: #333333;
							line-height: 80rpx;
							text-align: center;

						}

						.time-c {
							height: 80rpx;
							width: 40rpx;
							text-align: center;
							line-height: 80rpx;
							//border-top: 1rpx solid #E0E0E0;
							//border-bottom: 1rpx solid #E0E0E0;
						}

						.time-r {
							height: 80rpx;
							width: 324rpx;
							//background: #E0E0E0;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 28rpx;
							color: #333333;
							line-height: 80rpx;
							text-align: center;
						}
					}
				}
			}
		}

		.bottom {
			display: flex;
			justify-content: space-between;
			position: fixed;
			bottom: 0;

			.bottom-l {
				width: 375rpx;
				height: 90rpx;
				background: #FFFFFF;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #333333;
				line-height: 90rpx;
				text-align: center;
			}

			.bottom-r {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #FFFFFF;
				line-height: 90rpx;
				text-align: center;
				width: 375rpx;
				height: 90rpx;
				background: #0183FA;
			}
		}

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

	/deep/.input-value-border {
		display: none !important;
	}
</style>
<style>
	.uni-date-x .icon-calendar {
		padding-left: 3px;
		display: none !important;
	}

	.uni-date__x-input {
		height: 74rpx !important;
		line-height: 74rpx !important;
		font-size: 28rpx !important;
	}

	.uni-date__icon-clear{
		height: 74rpx !important;
		font-size: 28rpx !important;
	}
	.uni-date__icon-clear text{
		font-size: 32rpx !important;
	}
</style>
