<!-- 随手拍列表 -->
<template>
	<view class="snapshotList">
		<scroll-view scroll-y @scrolltolower="scrollGet" class="info-max-box">
			<view class="tabTitle" v-if="userType==0">
				<view :class="tabIndex==index?'tab-A':'tab-B'" v-for="(item,index) in tabList" @click="tabClick(index)">
					{{item}}
				</view>
			</view>
			<view class="tabTitleTow" v-if="userType==1">
				<view :class="tabIndexTow==index?'tab-C':'tab-D'" v-for="(item,index) in tabListTow"
					@click="tabClickTow(index)">{{item}}</view>
			</view>
			<view v-if="pageType==0">
				<view class="srearch">
					<view class="srearch-n">
						<view class="srearch-l" @click="popupClick(1)">
							<img src="@/pages_safetyCheck/images/icon_aqjc_ss.png">
						</view>
						<input class="srearch-c" type="text" v-model="form.searchValue" placeholder="实验室/房间号" />
						<view class="srearch-r" @click="saoCode()">
							<img src="@/pages_safetyCheck/images/icon_aqjc_sm.png">
						</view>
					</view>
				</view>
				<view class="list">
					<view class="list-li">
						<view>实验室名称（房间号）</view>
						<view></view>
					</view>
					<view class="list-li">
						<view>学院单位名称</view>
						<view>化工学院</view>
					</view>
					<view class="list-li">
						<view>检查项目</view>
						<uni-data-picker :ellipsis="false" :localdata="checkOptionList[0]"
							:map="{text:'name',value:'id'}" popup-title="请选择所属目录" @change="(e)=>onchange('',e,index)"
							@nodeclick="onnodeclick()" ref="uniDataPicker" :addType="true" :addIndex="index">
							<view class="project_r">{{form.hazardCheckName?form.hazardCheckName:'请选择检查项 >'}}</view>
						</uni-data-picker>
					</view>
					<view class="list-li">
						<view>隐患描述</view>
						<view></view>
					</view>
					<textarea class="describe" type="text" v-model="form.rectifyDescribe" maxlength="200"
						placeholder="请填写隐患描述" placeholder-style="font-size:24rpx;color:#999;"></textarea>
					<view class="check-for-img-max-box">
						<view class="left-title-p"></view>
						<view class="right-img-box">
							<view class="img-box" v-for="(imgUrl,imgIndex) in form.imgDtoList" :key="imgIndex">
								<img class="img-data" :src="baseUrl+imgUrl.fileUrl">
								<img class="position-img" src="@/pages_safetyCheck/images/icon_ssp_closure.png"
									@click="delImg(imgIndex)">
							</view>
							<img class="add-button" src="@/pages_safetyCheck/images/img_ssp_pz@1x.png"
								@click="selectImage()" v-if="form.imgDtoList.length<5">
						</view>
					</view>
				</view>
				<view class="subBtn">提交</view>
			</view>
			<!-- 上报记录 -->
			<view class="escalation" v-if="pageType==1">
				<view class="escalation-li" v-for="(item,index) in escalationList">
					<view class="escalation-li-t">
						<view>{{item.subName}}</view>
						<view :id="item.status==0?'color-A':(item.status==1?'color-B':(item.status==2?'color-C':''))">
							待整改
							<img  src="@/pages_safetyCheck/images/icon_wd_gd@1x.png">
						</view>
					</view>
					<view class="escalation-li-b">
						<view>上报时间</view>
						<view>{{item.time}}</view>
					</view>
				</view>
			</view>
			<!-- 隐患整改 -->
			<view class="hidden" v-if="pageType==2">
				<view class="hidden-li" v-for="(item,index) in hiddenList">
					<img class="hidden-li-l"  src="@/pages_safetyCheck/images/icon_sys_xz@1x.png">
					<view class="hidden-li-c">{{item.subName}}</view>
					<view class="hidden-li-r">待检查{{item.num}}项
						<img  src="@/pages_safetyCheck/images/icon_wd_gd@1x.png">
					</view>
				</view>
			</view>

		</scroll-view>
		<view class="popup-max-box" v-if="popupType">
			<view class="popup-null" @click="popupClick(2)"></view>
			<view class="popup-big-box">
				<view class="popup-input-box">
					<input type="text" maxlength="10" v-model="room" placeholder="请输入关键字">
					<view @click="buildBySub">搜索</view>
				</view>
				<view class="popup-for-max-box">
					<view class="popup-for-null" v-if="!roomList[0]">暂无数据</view>
					<view class="popup-for-box" v-for="(item,index) in roomList" :key="index">
						<view class="name-p">{{item.subName}} ({{item.roomNum?item.roomNum:'-'}})</view>
						<view class="button-p" @click="popupClickItem(item)">确定</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		config
	} from '@/api/request/config.js'
	import {} from '@/pages/api/index.js'
	import {
		laboratorySubRelInfoGetRelList,
		checkOptionListNew,
	} from '@/pages_safetyCheck/api/index.js'
	export default {
		name: "snapshotList",
		components: {

		},
		data() {
			return {
				baseUrl: config.base_url,
				pageType: 0,
				userType: 1,
				tabList: ['随手拍上报', '上报记录'],
				tabIndex: 0,
				tabListTow: ['随手拍上报', '上报记录', '隐患整改'],
				tabIndexTow: 0,
				newData: {

				},
				form: {
					rectifyDescribe: '',
					imgDtoList: [],
					hazardCheckName:'',
				},
				popupType: false,
				roomList: [],
				checkOptionList: [],
				escalationList:[
					{
						subName:'实验室名称（房间号）',
						status:0,
						time:'2024-01-01',
					},
					{
						subName:'实验室名称（房间号）',
						status:1,
						time:'2024-01-01',
					},
					{
						subName:'实验室名称（房间号）',
						status:2,
						time:'2024-01-01',
					},
				],
				hiddenList:[
					{
						subName:'实验室名称实验室名称实验室名称实验室名称',
						num:3,
					},
					{
						subName:'实验室名称实验室名称实验室名称实验室名称',
						num:3,
					},
					{
						subName:'实验室名称实验室名称实验室名称实验室名称',
						num:3,
					},
				],
			}
		},
		onLoad(option) {


		},
		onShow() {

		},
		mounted() {

		},
		methods: {
			//滚动事件
			scrollGet() {},
			tabClick(index) {
				this.tabIndex = index;
				this.pageType = index;
			},
			tabClickTow(index) {
				this.tabIndexTow = index;
				this.pageType = index;
			},
			/******图片上传******/
			selectImage() {
				let self = this;
				if (this.form.imgDtoList.length > 4) {
					uni.showToast({
						title: '最多上传5张图片',
						icon: "none",
						mask: true,
						duration: 2000
					});
					return
				}
				wx.chooseImage({
					count: 1,
					sizeType: ["original", "compressed"],
					sourceType: ["album", "camera"],
					success: function(res) {
						let tempFilePaths = res.tempFilePaths[0];
						self.uploadImg(tempFilePaths);
					}
				});
			},
			async uploadImg(tempFilePaths) {
				var self = this;
				uni.showLoading({
					title: '上传中',
					mask: true
				});
				uni.uploadFile({
					url: config.base_url + '/system/file/upload', //仅为示例，非真实的接口地址
					header: {
						'Authorization': uni.getStorageSync('token')
					},
					filePath: tempFilePaths,
					name: 'file',
					formData: {
						'user': 'test'
					},
					success: (uploadFileRes) => {
						let res = JSON.parse(uploadFileRes.data);
						if (res.code == 200) {
							this.form.imgDtoList.push({
								'fileUrl': res.data.url,
								'fileName': res.data.name
							});
							console.log(res.data.url)
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none",
								mask: true,
								duration: 2000
							});
						}
					},
					fail: err => {},
					complete: () => {
						uni.hideLoading()
					}
				});
			},
			//删除图片
			delImg(minIndex) {
				this.form.imgDtoList.splice(minIndex, 1);
				this.$forceUpdate();
			},
			/******搜索房间弹层开关******/
			popupClick(type) {
				if (type == 1) {
					this.room = "";
					this.roomList = [];
					this.popupType = true;
					this.buildBySub();
				} else if (type == 2) {
					this.popupType = false;
				}
			},
			//搜索房间接口
			async buildBySub() {
				let self = this;
				const {
					data
				} = await laboratorySubRelInfoGetRelList({
					'searchValue': this.form.searchValue,
				});
				if (data.code == 200) {
					if (data.data[0]) {
						this.roomList = data.data;
					} else {
						uni.showToast({
							title: '未找到相关实验室',
							icon: "none",
							mask: true,
							duration: 2000
						});
					}
				}
			},
			//确认搜索房间
			popupClickItem(item) {
				console.log(item)
				this.$set(this.form, "subId", item.subId)
				this.$set(this.form, "subName", item.subName)
				this.$set(this.form, "deptId", item.deptId)
				for (let i = 0; i < this.collegeList.length; i++) {
					if (item.deptId == this.collegeList[i].deptId) {
						this.$set(this.form, "deptName", this.collegeList[i].deptName)
					}
				}
				this.popupType = false;
			},
			/******调用摄像头******/
			saoCode() {
				let self = this;
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						let list = res.result.split("?")[1].split("&");
						let codeData = {};
						list.forEach((item) => {
							codeData[item.split("=")[0]] = item.split("=")[1];
						})
						console.log('二维码', codeData)
						if (codeData.code) {
							self.laboratoryAppletGetSubDetailInfo(codeData.code);
						} else {
							uni.showToast({
								title: '请扫描正确的二维码',
								icon: "none",
								mask: true,
								duration: 2000
							});
						}
					}
				});
			},
			/**************************搜索项***************************/
			//获取检查项
			async getCheckOptionList() {
				const {
					data
				} = await checkOptionListNew();
				if (data.code == 200) {
					let list = this.getTreeData(data.data);
					console.log(list)
					this.checkOptionList.push(JSON.parse(JSON.stringify(list)));

				}
			},
			getTreeData(data) {
				for (var i = 0; i < data.length > 0; i++) {
					if (data[i].children == null || data[i].children.length <= 0) {
						// children若为空数组，则将children设为undefined
						data[i].children = undefined;
					} else {
						if (this.form.checkCategory == 1 && data[i].level == 3) {
							if (data[i].children[0]) {
								let mainPoint = []
								for (let b = 0; b < data[i].children.length; b++) {
									mainPoint.push(data[i].children[b].name)
									data[i].mainPoint = mainPoint.join(',')
								}
							} else {
								data[i].mainPoint = '';
							}
							data[i].children = [];
						}
						// children若不为空数组，则继续 递归调用 本方法
						this.getTreeData(data[i].children);
					}
				}
				return data;
			},
			/**************************搜索项***************************/
			//搜索特定检查项
			async searchClick(text, index) {
				console.log("搜索text", text);
				console.log("搜索index", index);
				const {
					data
				} = await checkOptionListNew({
					'searchValue': text
				});
				if (data.code == 200) {
					if (data.data[0]) {
						let list = [];
						list[0] = this.getTreeData(data.data)
						this.$set(this, 'checkOptionList', JSON.parse(JSON.stringify(list)))

						console.log(this.checkOptionList)

						this.$refs.uniDataPicker[index].show();
					} else {
						uni.showToast({
							title: '未找到相关检查项',
							icon: "none",
							mask: true,
							duration: 2000
						});
					}
				}
			},
			//重置检查项
			async resetClick(index) {
				console.log("重置index", index);
				const {
					data
				} = await checkOptionListNew();
				if (data.code == 200) {
					let list = [];
					list[0] = this.getTreeData(data.data);
					this.$set(this, 'checkOptionList', JSON.parse(JSON.stringify(list)))
					this.$refs.uniDataPicker[index].show();
				}
			},
			//选中固有检查项(新)
			onchange(indexAll, e, index) {
				let self = this;
				//hazardCheckCode:'', //检查项目code序号
				//hazardCheckName:'', //检查项目名称
				//hazardCheckPro:'', //最后一级的id
				if (this.getTreeName(this.checkOptionList[0], e.detail.value[e.detail.value.length - 1].value).level ==
					3) {
					let jcxSstkNum = this.getTreeName(this.checkOptionList[0], e.detail.value[e.detail.value.length -
						1].value).code
					this.$set(this.form.checkHazardDtoList[index], 'hazardCheckPro', e.detail.value[e.detail.value
						.length - 1].value);
					this.$set(this.form.checkHazardDtoList[index], 'hazardCheckCode', jcxSstkNum);
					this.$set(this.form.checkHazardDtoList[index], 'hazardCheckName', e.detail.value[e.detail.value
						.length - 1].text);
					this.$set(this.form.checkHazardDtoList[index], 'hazardCheckPoint', this.getTreeName(this
						.checkOptionList[0], e.detail.value[e.detail.value.length - 1].value).mainPoint);
							
				} else {
					uni.showToast({
						title: '暂无数据',
						icon: "none",
						mask: true,
						duration: 2000
					});
				}
			},

		}
	}
</script>

<style lang="stylus" scoped>
	.snapshotList {
		height: 100%;
		display flex;

		.tabTitle {
			width: 600rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			margin: 20rpx 0 0 74rpx;

			>view:nth-of-type(1) {
				width: 300rpx;
				height: 80rpx;
				border-radius: 50rpx 0rpx 0rpx 50rpx;
			}

			>view:nth-of-type(2) {
				width: 300rpx;
				height: 80rpx;
				border-radius: 0rpx 50rpx 50rpx 0rpx;
			}

			.tab-A {
				font-size: 30rpx;
				color: #FFFFFF;
				line-height: 80rpx;
				text-align: center;
				background: #0183FA;
			}

			.tab-B {
				font-size: 30rpx;
				color: #333333;
				line-height: 80rpx;
				text-align: center;
				background: #FFFFFF;
			}
		}

		.tabTitleTow {
			width: 690rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			margin: 20rpx 0 0 30rpx;

			>view:nth-of-type(1) {
				width: 230rpx;
				height: 80rpx;
				border-radius: 50rpx 0rpx 0rpx 50rpx;
			}

			>view:nth-of-type(2) {
				width: 230rpx;
				height: 80rpx;
				border-radius: 0rpx 0rpx 0rpx 0rpx;
			}

			>view:nth-of-type(3) {
				width: 230rpx;
				height: 80rpx;
				border-radius: 0rpx 50rpx 50rpx 0rpx;
			}

			.tab-C {
				font-size: 30rpx;
				color: #FFFFFF;
				line-height: 80rpx;
				text-align: center;
				background: #0183FA;
			}

			.tab-D {
				font-size: 30rpx;
				color: #333333;
				line-height: 80rpx;
				text-align: center;
				background: #FFFFFF;
			}
		}

		.srearch {
			width: 750rpx;
			height: 100rpx;
			background: #FFFFFF;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			overflow: hidden;
			margin-top: 20rpx;

			.srearch-n {
				width: 690rpx;
				height: 60rpx;
				border-radius: 50rpx 50rpx 50rpx 50rpx;
				border: 1rpx solid #E0E0E0;
				margin: 20rpx 0 0 30rpx;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				.srearch-l {
					width: 60rpx;
					height: 60rpx;

					>img {
						width: 20rpx;
						height: 20rpx;
						margin: 20rpx 0 0 26rpx;
					}
				}

				.srearch-c {
					flex: 1;
					height: 58rpx;
					font-size: 24rpx;
					color: #999999;
					line-height: 30rpx;
					text-align: left;
				}

				.srearch-r {
					width: 60rpx;
					height: 60rpx;

					>img {
						width: 30rpx;
						height: 30rpx;
						margin: 14rpx 22rpx 0 0;
					}


				}
			}
		}

		.list {
			width: 690rpx;
			min-height: 765rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			margin: 20rpx 0 0 30rpx;

			.list-li {
				display: flex;
				justify-content: space-between;
				border-bottom: 1rpx solid #E0E0E0;
				padding: 0 22rpx 0 28rpx;
				box-sizing: border-box;

				>view:nth-of-type(1) {
					font-size: 30rpx;
					color: #333333;
					line-height: 80rpx;
					text-align: left;
				}

				>view:nth-of-type(2) {
					font-size: 28rpx;
					color: #666666;
					line-height: 80rpx;
					text-align: left;
					display: flex;
					justify-content: flex-start;
					align-items: center;

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

				.project_r {
					width: 510rpx;
					min-height: 80rpx;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					padding-left: 20rpx;
					box-sizing: border-box;
					color: #666;
				}

			}

			.describe {
				width: 636rpx;
				height: 120rpx;
				border-radius: 10rpx 10rpx 10rpx 10rpx;
				border: 1rpx solid #E0E0E0;
				margin: 20rpx 0 0 28rpx;
				padding: 12rpx 16rpx;
				box-sizing: border-box;
			}

			.check-for-img-max-box {
				margin: 20px 28rpx;

				.left-title-p {
					width: 100%;
					text-align: left;
					font-size: 30rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 400;
					color: #333333;
					line-height: 80rpx;
				}

				.right-img-box {
					.img-box {
						display inline-block;
						height: 150rpx;
						width: 150rpx;
						position relative;
						margin: 0 14rpx 20rpx 0;
						border-radius 10rpx;
						overflow hidden;

						.img-data {
							height: 150rpx;
							width: 150rpx;
						}

						.position-img {
							position absolute;
							right: 0;
							top: 0;
							width: 36rpx;
							height: 36rpx;
						}
					}

					.img-box:nth-of-type(3n+3) {
						margin-right: 0rpx;
					}

					.add-button {
						margin: 0 0rpx 20rpx 0;
						border-radius 10rpx;
						overflow hidden;
						display inline-block;
						height: 150rpx;
						width: 150rpx;
					}
				}
			}
		}

		/* 实验室弹框 */
		.popup-max-box {
			z-index: 10;
			height: 100%;
			width: 100%;
			position fixed;
			background rgba(0, 0, 0, 0.2);
			display flex;

			flex-direction column;

			.popup-null {
				flex: 1;
			}

			.popup-big-box {
				border-top-left-radius: 20rpx;
				border-top-right-radius: 20rpx;

				background #fff;

				.popup-input-box {
					padding: 30rpx 20rpx;
					display: flex;

					input {
						flex: 1;
						height: 70rpx;
						border: 1rpx solid #e0e0e0;
						border-radius: 10rpx;
						margin-right: 20rpx;
						padding: 0 20rpx;
					}

					view {
						background #0183FA;
						color: #fff;
						border-radius: 10rpx;
						width: 140rpx;
						line-height: 70rpx;
						text-align center;
						font-size: 28rpx;
					}
				}

				.popup-for-max-box {
					margin: 0 20rpx 30rpx;
					height: 600rpx;
					overflow-y scroll;

					.popup-for-null {
						line-height: 100rpx;
						text-align center;
						color: #999;
					}

					.popup-for-box {
						display: flex;
						padding: 10px 0;

						.name-p {
							flex: 1;
							line-height: 60rpx;
						}

						.button-p {
							background #0183FA;
							color: #fff;
							text-align center;
							width: 100rpx;
							line-height: 60rpx;
							height: 60rpx;
							border-radius: 10rpx;
						}
					}
				}
			}
		}

		.subBtn {
			width: 690rpx;
			height: 100rpx;
			background: #0183FA;
			border-radius: 50rpx 50rpx 50rpx 50rpx;
			font-weight: 400;
			font-size: 30rpx;
			color: #FFFFFF;
			line-height: 100rpx;
			text-align: center;
			position: fixed;
			left: 30rpx;
			bottom: 10rpx;
		}
		/* 上报记录 */
		.escalation{
			
			.escalation-li{
				width: 690rpx;
				height: 162rpx;
				background: #FFFFFF;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				margin: 20rpx 30rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
				.escalation-li-t{
					display: flex;
					justify-content: space-between;
					align-items: center;
					border-bottom: 1rpx solid #E0E0E0;
					>view:nth-of-type(1) {
						font-weight: 400;
						font-size: 30rpx;
						color: #333333;
						line-height: 80rpx;
						text-align: left;
					}
					>view:nth-of-type(2) {
						font-weight: 400;
						font-size: 28rpx;
						color: #0183FA;
						line-height: 80rpx;
						text-align: left;
						display: flex;
						justify-content: flex-start;
						align-items: center;
						>img{
							width: 24rpx;
							height: 24rpx;
						}
					}
				}
				.escalation-li-b{
					display: flex;
					justify-content: space-between;
					align-items: center;
					>view:nth-of-type(1) {
						font-weight: 400;
						font-size: 30rpx;
						color: #333333;
						line-height: 80rpx;	
						text-align: left;
					}
					>view:nth-of-type(2) {
						font-weight: 400;
						font-size: 30rpx;
						color: #666666;
						line-height: 80rpx;
						text-align: left;
					}
				}
				
			}
		}
		/* 隐患整改 */
		.hidden{
			width: 690rpx;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			background: #FFFFFF;
			padding: 0 30rpx;
			box-sizing: border-box;
			margin: 20rpx 30rpx;
			.hidden-li{
				height: 80rpx;
				border-bottom: 1rpx solid #E0E0E0;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				.hidden-li-l{
					width: 34rpx;
					height: 34rpx;
				}
				.hidden-li-c{
					font-size: 28rpx;
					color: #333333;
					line-height: 30rpx;
					text-align: left;
					flex: 1;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					margin: 0 84rpx 0 12rpx;
				}
				.hidden-li-r{
					font-size: 28rpx;
					color: #0183FA;
					line-height: 80rpx;
					text-align: left;
					display: flex;
					justify-content: flex-start;
					align-items: center;
					>img{
						width: 24rpx;
						height: 24rpx;
					}
				}
			}
			.hidden-li:last-of-type{
				border: none;
			}
		}
		#color-A{
			color: #0183FA;
		}
		#color-B{
			color: #FF8C00;
		}
		#color-C{
			color: #16B531;
		}
	}
</style>
