<template>
	<view class="shade">
		<view class="null-box" @click="dialogClose()"></view>
		<view class="shade_n">
			<view class="title">
				<view>{{dialogTtile}}</view>
				<!-- <img :src="imagesUrl('commonality/icon_06.png')"> -->
			</view>
			<view class="batch">
				<view class="batch-li" @click="batchClick(item)" v-for="(item,index) in batchList">
					<view class="batch-li-l">
						<text v-if="dialogStatus==0">{{item.planTitle}}</text>
						<text v-if="dialogStatus==1">{{item.checkName}}</text>
					</view>
					<view class="batch-li-r">
						<img :src="imagesUrl('commonality/icon_wd_gd@1x.png')">
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		securityAppCheckPlanAppTitleList,
		securityAppCheckManageList,
		securityAppCheckSetOptionList,
	} from '@/pages_safetyCheck/api/index.js'
	import {
		config
	} from '@/api/request/config.js'
	export default {
		name: "iotControl",
		props: {
			subjectData: {},
		},
		data() {
			return {
				baseUrl: config.base_url,
				newData:{},
				dialogTtile: '',
				dialogStatus: 0, //0计划 1批次 2实验室
				batchList: [],
				queryParams: {
					page: 1,
					pageSize: 100,
					checkCategory: '', //1校院巡查 2学院自查
					subId: '',
				},
				checkPlanSetVoList: null,
				form: {},
				OptionListArr:[],//临时存储检查项
				planId:'',
				planTitle:'',
				checkTypeName:'',
				crossCheck:'',
			}
		},
		created() {
			
		},
		mounted() {
			if(uni.getStorageSync('identityData')){
				if(uni.getStorageSync('identityData').type=='collegeAdmin'){
					this.queryParams.checkCategory=2
				}else if(uni.getStorageSync('identityData').type=='schoolAdmin'){
					this.queryParams.checkCategory=1
				}
			}
			this.$set(this, 'newData', this.subjectData);
			console.log(this.subjectData)
			this.$set(this.queryParams, 'subId', this.subjectData.subId);
			this.securityAppCheckPlanAppTitleList();
		},
		methods: {
			dialogClose() {
				this.dialogStatus = 0;
				// #ifdef WEB
				this.$parent.$parent.goPage('securityCheck',false);
				// #endif
				// #ifdef MP-WEIXIN
				this.$parent.goPage('securityCheck',false);
				// #endif
			},
			//十六进制颜色值和RGB格式转换
			hexToRgb(hex, opacity = 1) {
				// 去除#号
				var color = hex.replace("#", "");
				// 分割成红、绿、蓝三部分的16进制字符串
				var red = parseInt(color.substring(0, 2), 16);
				var green = parseInt(color.substring(2, 4), 16);
				var blue = parseInt(color.substring(4, 6), 16);
				return `RGB(${red}, ${green}, ${blue},${opacity})`;
			},
			batchClick(row) {
				let self = this;
				if (this.dialogStatus == 0) { //计划点击
					this.$set(this, 'planId', row.planId);
					this.$set(this, 'planTitle', row.planTitle);
					this.$set(this, 'checkTypeName', row.checkTypeName);
					this.$set(this, 'crossCheck', row.crossCheck);
					if (row.checkPlanSetVoList.length == 1) {
						//如果只有1个检查批次不弹窗
						let planSetId = row.checkPlanSetVoList[0].planSetId
						self.checkPlanSetVoList = row.checkPlanSetVoList[0];
						self.securityAppCheckManageList(planSetId)
					} else if (row.checkPlanSetVoList.length == 0) {
						this.dialogStatus = 0;
						// #ifdef WEB
						this.$parent.$parent.goPage('securityCheck',false);
						// #endif
						// #ifdef MP-WEIXIN
						this.$parent.goPage('securityCheck',false);
						// #endif
						uni.showToast({
							title: '该实验室暂无检查计划!',
							icon: "none",
							mask: true,
							duration: 2000
						});
						return
					} else {
						this.$set(this, 'batchList', row.checkPlanSetVoList);
						this.dialogStatus = 1;
						this.dialogTtile = '批次';
						// #ifdef WEB
						this.$parent.$parent.goPage('securityCheck',true);
						// #endif
						// #ifdef MP-WEIXIN
						this.$parent.goPage('securityCheck',true);
						// #endif
					}
				} else if (this.dialogStatus == 1) { //批次点击
					/* this.dialogStatus = 2;
					this.dialogTtile = '实验室'; */
					let planSetId = row.planSetId
					self.checkPlanSetVoList = row;
					this.securityAppCheckManageList(planSetId)
				}
			},
			//查询计划
			async securityAppCheckPlanAppTitleList() {
				this.queryParams.subId = this.newData.subId;
				this.queryParams.checkStatus = 0;
				let self = this;
				const {
					data
				} = await securityAppCheckPlanAppTitleList(this.queryParams);
				if (data.code == 200) {
					//循环过滤掉当前计划中批次中已经检查完成的
					data.data.records.forEach(function(item, index) {
						let list = [];
						item.checkPlanSetVoList.forEach(function(item2) {
							if (item2.checkSetProgress != 100) {
								list.push(item2)
							}
						})
						item.checkPlanSetVoList = list
					})
					let list2 = []
					list2 = data.data.records.filter(function(item) {
						return item.checkPlanSetVoList.length != 0
					});
					data.data.records = list2
			
					if (data.data.records.length == 1) { //一个计划
						this.$set(this, 'planId', data.data.records[0].planId);
						this.$set(this, 'planTitle', data.data.records[0].planTitle);
						this.$set(this, 'checkTypeName', data.data.records[0].checkTypeName);
						this.$set(this, 'crossCheck', data.data.records[0].crossCheck);
						if (data.data.records[0].checkPlanSetVoList.length == 1) { //1个批次
							let planSetId = data.data.records[0]
							if (data.data.records[0].checkPlanSetVoList[0]) { //一个批次
								let planSetId = data.data.records[0].checkPlanSetVoList[0].planSetId
								self.checkPlanSetVoList = data.data.records[0].checkPlanSetVoList[0];
								self.securityAppCheckManageList(planSetId)
							}
						} else if (data.data.records[0].checkPlanSetVoList.length == 0) {
							this.dialogStatus = 0;
						// #ifdef WEB
						this.$parent.$parent.goPage('securityCheck',false);
						// #endif
						// #ifdef MP-WEIXIN
						this.$parent.goPage('securityCheck',false);
						// #endif
							uni.showToast({
								title: '该实验室暂无检查计划!',
								icon: "none",
								mask: true,
								duration: 2000
							});
							return
						} else { //多个批次
							this.dialogStatus = 1;
							this.batchList = data.data.records[0].checkPlanSetVoList;
						// #ifdef WEB
							this.$parent.$parent.goPage('securityCheck',true);
						// #endif
						// #ifdef MP-WEIXIN
							this.$parent.goPage('securityCheck',true);
						// #endif
							this.dialogTtile = '批次';
						}
			
					} else if (data.data.records.length==0) {
						this.dialogStatus = 0;
						// #ifdef WEB
						this.$parent.$parent.goPage('securityCheck',false);
						// #endif
						// #ifdef MP-WEIXIN
						this.$parent.goPage('securityCheck',false);
						// #endif
						uni.showToast({
							title: '该实验室暂无检查计划!',
							icon: "none",
							mask: true,
							duration: 2000
						});
						return
					} else { //多个计划
						this.batchList = data.data.records;
						// #ifdef WEB
						this.$parent.$parent.goPage('securityCheck',true);
						// #endif
						// #ifdef MP-WEIXIN
						this.$parent.goPage('securityCheck',true);
						// #endif
						this.dialogTtile = '计划标题';
					}
				}
			},
			//检查任务-在此结果里对比实验室Id判断是否有实验室
			async securityAppCheckManageList(planSetId) {
				let self = this;
				let obj = {
					page: 1,
					pageSize: 20,
					planSetId: planSetId,
					subId: self.newData.subId,
				}
				const {
					data
				} = await securityAppCheckManageList(obj);
				if (data.code == 200) {
					//this.shadeList = data.data.records;
					if (data.data.records[0]) {
						let manageId = data.data.records[0].manageId;
						self.securityAppCheckSetOptionList(data.data.records[0],0);
					} else {
						self.dialogStatus = 0;
						// #ifdef WEB
						this.$parent.$parent.goPage('securityCheck',false);
						// #endif
						// #ifdef MP-WEIXIN
						this.$parent.goPage('securityCheck',false);
						// #endif
						uni.showToast({
							title: '该实验室暂无检查计划!',
							icon: "none",
							mask: true,
							duration: 2000
						});
						return
					}
			
				}
			},
			//查询检查项
			async securityAppCheckSetOptionList(item,checkStatus) {
				let self = this;
				let upData = item;
				let obj = {
					page: 1,
					pageSize: 20,
					checkStatus: checkStatus,
					manageId: upData.manageId,
				}
				const {
					data
				} = await securityAppCheckSetOptionList(obj);
				if (data.code == 200 && checkStatus==0) {//先查询待检查数据
					if (data.data.records[0]) {
						let infoData = {};
						infoData.pageType = 0;
						infoData.itemsStatus = 0;
						infoData.subName = upData.subName;
						infoData.checkPlanSetVoList = self.checkPlanSetVoList;
						infoData.planId = self.planId;
						infoData.planTitle = self.planTitle;
						infoData.checkTypeName = self.checkTypeName;
						infoData.crossCheck = self.crossCheck;
						
						infoData.roomNum = upData.roomNum;
						infoData.subId = data.data.records[0].subId;
						infoData.manageId = data.data.records[0].manageId;
						infoData.setOptionId = data.data.records[0].setOptionId;
						infoData.hazardCheckPro = data.data.records[0].hazardCheckPro;
						infoData.checkCategory = data.data.records[0].checkCategory;
						uni.navigateTo({
							url: '/pages_safetyCheck/views/inspectManage/conductInspections?infoData=' +
								encodeURIComponent(
									JSON
									.stringify(infoData))
						});
					} else {
						self.securityAppCheckSetOptionList(item,2);
						
					}
				}else if (data.code == 200 && checkStatus==2) {//查询检查中数据
					if (data.data.records[0]) {
						let infoData = {};
						infoData.pageType = 0;
						infoData.itemsStatus = 0;
						infoData.subName = upData.subName;
						infoData.checkPlanSetVoList = self.checkPlanSetVoList;
						infoData.planId = self.planId;
						infoData.planTitle = self.planTitle;
						infoData.checkTypeName = self.checkTypeName;
						infoData.crossCheck = self.crossCheck;
						infoData.roomNum = upData.roomNum;
						infoData.subId = data.data.records[0].subId;
						infoData.manageId = data.data.records[0].manageId;
						infoData.setOptionId = data.data.records[0].setOptionId;
						infoData.hazardCheckPro = data.data.records[0].hazardCheckPro;
						infoData.checkCategory = data.data.records[0].checkCategory;
						uni.navigateTo({
							url: '/pages_safetyCheck/views/inspectManage/conductInspections?infoData=' +
								encodeURIComponent(
									JSON
									.stringify(infoData))
						});
					} else {
						self.dialogStatus = 0;
						// #ifdef WEB
						this.$parent.$parent.goPage('securityCheck',false);
						// #endif
						// #ifdef MP-WEIXIN
						this.$parent.goPage('securityCheck',false);
						// #endif
						uni.showToast({
							title: '该实验室暂无检查工作!',
							icon: "none",
							mask: true,
							duration: 2000
						});
						return;
					}
				}
			},


		},
		onHide() {
		
		},
		beforeDestroy() {
			
		},
	}
</script>

<style lang="stylus" scoped>
	.shade {
		height: 100%;
		width: 100%;
		position: fixed;
		display: flex;
		flex-direction: column;
		z-index: 200;
		background: rgba(0, 0, 0, 0.2);
		top: 0;
	
		.null-box {
			flex: 1;
		}
	
		.shade_n {
			position: absolute;
			bottom: 0;
			left: 0;
			width: 750rpx;
			height: 466rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 0rpx 0rpx;
			overflow-y: auto;
	
			.title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 50rpx 0 284rpx;
				box-sizing: border-box;
				border-bottom: 1rpx solid #E0E0E0;
	
				>view {
					font-size: 30rpx;
					color: #3D3D3D;
					line-height: 90rpx;
					text-align: left;
				}
	
				>img {
					width: 24rpx;
					height: 12rpx;
				}
			}
	
			.batch {
				padding: 0 30rpx;
				box-sizing: border-box;
	
				.batch-li {
					display: flex;
					justify-content: space-between;
					align-items: center;
					border-bottom: 1rpx solid #E0E0E0;
					padding: 20rpx 0 16rpx 0rpx;
					box-sizing: border-box;
	
					.batch-li-l {
						>text:nth-of-type(1) {
							display: block;
							font-size: 30rpx;
							color: #333333;
							line-height: 42rpx;
							text-align: left;
						}
	
						>text:nth-of-type(2) {
							display: block;
							font-size: 28rpx;
							color: #666666;
							line-height: 39rpx;
							text-align: left;
							margin-top: 6rpx;
						}
					}
	
					.batch-li-r {
						font-size: 28rpx;
						color: #666666;
						line-height: 39rpx;
						text-align: left;
						display: flex;
						justify-content: flex-start;
						align-items: center;
	
						>img {
							width: 24rpx;
							height: 24rpx;
							margin-left: 9rpx;
						}
					}
				}
			}
		}
	}
</style>
