<template>
	<view class="fullHeight dangerLibrary">
		<headers class="fixedheader" :themeType="true" :showBack="true">
			<view class="headerName">
				隐患库
			</view>
		</headers>
		<view style="height: 100%; box-sizing: border-box;" :style="{ 'padding-top': statusBarHeight + 45 + 'px' }">
		<!-- <view class="dangerLibrary-crumbs flex b-bottom">
			<view class="crumbs-title">当前标准：<text>房建（JGJ59-2011）</text></view>
			<view class="add-btn" @tap="addDanger">
				自定义隐患
			</view>
		</view> -->
		
		<view class="dangerLibrary-content flex" id="scroll-panel">
			<view class="dangerLibrary-nav">
				<scroll-view class="smallHeight" :scroll-into-view="leftIntoView" scroll-y>
					<view class="nav-item"
						:id="'left-' + item"
						:class="{'active-nav': leftIndex == index}"
						v-for="(item,index) in navList" :key="index"
						@tap="checkNav(index)">
						{{item.hiddenDangerName}}
						</view>
				</scroll-view>
			</view>
			<view class="dangerLibrary-detail">
				<scroll-view class="smallHeight" scroll-y>
					<view class="detail-title">
						检查分项<text>(包含{{rightNavList.length}}个分项)</text>
					</view>
					<!-- <view class="detail-title">
						<text>共22个隐患点</text>
					</view> -->
					<view>
					    <view :class="{'b-bottom': rightNavList.length && index != (rightNavList.length-1)}" v-for="(item,index) in rightNavList" :key="index" >
					        <!-- <template v-slot:title> -->
					            <view>
									<view class="card-title flex">
										<view  @tap="checkNavChildren(item)">{{item.hiddenDangerName}}<!-- <p>(共3个隐患点)</p> --></view>
										<view class="flex" style="margin-right: 10px;">
											<view class="pre-btn" @tap="previewRule(item)">
												检查依据
											</view>
								
											<uni-icons type="arrowdown" style="transition: all 0.3s linear;" :class="{'down': !item.isShow, 'up': item.isShow}" color="#6C7178" @tap="showCard(item)"/>
										</view>
									</view>
					            </view>
					        <!-- </template> -->
					        <view class="content" style="overflow: hidden;">
								<view style="transition: all 0.3s linear; overflow: hidden;" :class="{'hidden': !item.isShow, 'show': item.isShow}">
									<view class="card-detail flex" :class="{'b-bottom': idx !== (item.children.length-1)}" v-for="(itm,idx) in item.children" :key="idx">
										<text class="text">{{itm.checkContent}}</text>
										<view class="add-detail" @click="checkDetail(itm)">生成整改单</view>
									</view>
								</view>
					        </view>
					    </view>
					</view>
					
					<!-- <view class="others-list">
						<view class="others-item" :class="{'b-bottom': item !== 10}" v-for="item in 10" :key="item">
							<view class="others-detail flex">
								<text class="text">折叠内容主体，可自定义内容及样式折叠内容主体，可自定义内容及样式</text>
								<view class="add-detail">生成整改单</view>
							</view>
						</view>
					</view> -->
				</scroll-view>
			</view>
		</view>
		</view>
		<dialogs ref="dialogs">
			<template v-slot:content>
				<view class="dialog-title">
					检查依据
				</view>
				<view class="dialog-content">
					<scroll-view class="dialog-scroll" scroll-y>
						<p v-for="(item,index) in childrenData" :key="index" @tap="checkNavChildren(item)">{{item.checkContent}}</p>
					</scroll-view>
				</view>
			</template>
		</dialogs>
		
		<dialogs ref="dialogs2">
			<template v-slot:content>
				<view class="dialog-title">
					新建隐患
				</view>
				<view class="dialog-content">
					<view class="add-form">
						<view class="form-item">
							<picker class="form-content" :value="addForm.type" range-key="name" :range="typeList" @change="bindType">
								<view class="uni-input" :class="{'noCheck': !addForm.type.toString()}">
									{{addForm.type.toString() ? typeList[addForm.type].name : ""}}
								</view>
							</picker>
						</view>
						<view class="form-item">
							<picker class="form-content" :value="addForm.lv" range-key="name" :range="lvList" @change="bindLv">
								<view class="uni-input" :class="{'noCheck': !addForm.lv.toString()}">
									{{addForm.lv.toString() ? lvList[addForm.lv].name : "--请选择隐患等级--"}}
								</view>
							</picker>
						</view>
						<view class="form-item">
							<view class="form-textarea">
								<textarea placeholder="隐患点" @blur="bindTextAreaBlur"/>
							</view>
						</view>
					</view>
					<view class="submit-btn">提交</view>
				</view>
			</template>
		</dialogs>
	</view>
</template>

<script>
	import headers from '../../../components/headers/headers.vue'
	import dialogs from '../../../components/dialog/dialog.vue'
	export default {
		data(){
			return{
				childrenData:[],
				type:"",
				taskId:"",
				itemId:"",
				engineeringId: "",
				statusBarHeight: 0,
				navList: [],
				leftIndex: 0,
				scrollHeight: 400,
				scrollTopSize: 0,
				addForm:{
					type: 0,
					lv: "",
					content: ""
				},
				typeList:[{
					name: "其他",
					id: 0
				}],
				lvList:[{
					id: 0,
					name: "一般隐患"
				},{
					id: 1,
					name: "较大隐患"
				},{
					id: 2,
					name: "重大隐患"
				}],
				headquartersSn: "",
				rightNavList:[]
			}
		},
		computed: {
			/* 计算左侧滚动位置定位 */
			leftIntoView() {
				return `left-${this.leftIndex > 1 ? this.leftIndex - 1 : 0}`;
		    }
		},
		components:{
			headers,
			dialogs
		},
		onLoad(val) {
			console.log(val, '-----隐患级别 数据')
			this.type = val.type
			this.taskId = val.taskId
			this.itemId = val.itemId
			this.engineeringId = val.engineeringId
		},
		onShow(val){
			
			this.statusBarHeight=uni.getStorageSync('systemInfo').statusBarHeight;
			this.headquartersSn = JSON.parse(uni.getStorageSync('userInfo')).headquartersSn
			this.initData()
			console.log(JSON.parse(uni.getStorageSync('userInfo')))
		},
		mounted(){
			// this.$nextTick(() => {
			//                 /* 在非H5平台，nextTick回调后有概率获取到错误的元素高度，则添加200ms的延迟来减少BUG的产生 */
			//                 setTimeout(() => {
			//                     /* 等待滚动区域初始化完成 */
			//                     this.initScrollView().then(() => {
			//                         /* 获取列表数据，你的代码从此处开始 */
			//                         // this.getListData();
			//                     });
			//                 }, 200);
			//             });
		},
		methods:{
			initData(){
				let _this = this
				let sn = this.headquartersSn
				this.sendRequest({
					url: 'xmgl/inspectHiddenDangerLibrary/selectLibraryList',
					method: 'post',
					data: {
						sn: sn
					},
					success: res => {
						_this.navList = res.result
						if(_this.navList.length>0){
							_this.checkNav(0)
						}
						console.log(res.result, '-----分类列表数据')
					}
				})
			},
			initScrollView() {
			                return new Promise((resolve, reject) => {
			                    let view = uni.createSelectorQuery().select('#scroll-panel');
			                    view.boundingClientRect(res => {
			                        this.scrollTopSize = res.top;
			                        this.scrollHeight = res.height;
			                        this.$nextTick(() => {
			                            resolve();
			                        });
			                    }).exec();
			                });
			            },
			checkNav(val){
				console.log('点击事件触发了吗',val);
				this.leftIndex = val
				
				if(this.navList[val].children && this.navList[val].children.length >= 1) {
					this.rightNavList = this.navList[val].children
					console.log('列表数据',this.rightNavLis);
						
					console.log(this.navList[val].children, '-----分类列表数据')
					this.navList[val].children.forEach(item=>{
						item.isShow = false
					})
				}else{
					this.rightNavList = []
				}

				
			},
			checkNavChildren(val) {
				console.log('子类调用参数',val);
				val.title =  this.navList[this.leftIndex].hiddenDangerName
				uni.setStorageSync('dangerData', val);
				uni.navigateTo({
					url: './addExamine?type='+this.type+'&taskId=' + this.taskId + '&itemId=' + this.itemId + '&engineeringId=' + this.engineeringId
				})
			},
			previewRule(e){ 
				console.log('列表数据 点击数据：',e);
				this.sendRequest({
					url: 'xmgl/inspectHiddenDangerItemRecord/selectHiddenDangerItemRecordList',
					method: 'post',
					data: {
						libraryId: e.id
					},
					success: res => {
						this.childrenData=res.result
						this.$refs.dialogs.showFn()
						console.log(res.result, '-----分类子项详情')
					}
				})
				
			},
			addDanger(e){
				this.$refs.dialogs2.showFn()
			},
			bindType(e){
				this.addForm.type = e.detail.value
			},
			bindLv(e){
				this.addForm.lv = e.detail.value
			},
			bindTextAreaBlur(e){
				this.addForm.content = e.detail.value
			},
			showCard(val){
				if(val.isShow&&val.children.length>0){
					val.isShow = !val.isShow
					this.$forceUpdate()
				} else{
					val.children = []
					let _this = this
					this.sendRequest({
						url: 'xmgl/inspectHiddenDangerItemRecord/selectHiddenDangerItemRecordList',
						method: 'post',
						data: {
							libraryId: val.id
						},
						success: res => {
							val.children=res.result
							val.isShow = !val.isShow
							_this.$forceUpdate()
							console.log(res.result, '-----分类子项详情')
						}
					})
				}
				// console.log(val)
			},
			checkDetail(val){
				console.log('val的数据',val);
				val.title =  this.navList[this.leftIndex].hiddenDangerName
				uni.setStorageSync('dangerData', val);
				uni.navigateBack({})
				// console.log(val)
			}
		}
	}
</script>

<style lang="scss" scoped>
.dangerLibrary{
	.fixedheader{
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 2;
	}
	.flex{
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.b-bottom{
		border-bottom: 2rpx solid #DCDFE6;
	}
	.dangerLibrary-crumbs{
		height: 120rpx;
		width: 100%;
		padding: 0 20rpx;
		background: #fff;
		box-sizing: border-box;
		font-size: 26rpx;
		.crumbs-title{
			/deep/span{
				font-weight: 600;
			}
		}
		.add-btn{
			padding: 6rpx 30rpx;
			border: 1px solid #409EFF;
			border-radius: 24rpx;
			color: #409EFF;
		}
	}
	.dangerLibrary-content{
		height: 100%;
		.smallHeight{
			height: 100%;
			box-sizing: border-box;
		}
		.dangerLibrary-nav{
			height: 100%;
			width: 30%;
			background: #E4E7ED;
			.nav-item{
				width: 100%;
				padding: 20rpx 14rpx;
				box-sizing: border-box;
				text-align: center;
				font-size: 28rpx;
			}
			.active-nav{
				background: #fff;
				color: #409EFF;
			}
		}
		.dangerLibrary-detail{
			height: 100%;
			width: 70%;
			padding: 10rpx 0;
			padding-left: 20rpx;
			box-sizing: border-box;
			.detail-title{
				font-size: 24rpx;
				padding-bottom: 10rpx;
				/deep/span{
					color: #C0C4CC;
				}
			}
			.card-title{
				font-size: 32rpx;
				padding-bottom: 20rpx;
				padding-top: 26rpx;
				p{
					color: #C0C4CC;
					font-size: 24rpx;
					padding-top: 12rpx;
				}
				.pre-btn{
					font-size: 24rpx;
					color: #409EFF;
					white-space: nowrap;
					margin-right: 10rpx;
				}
			}
			.card-detail{
				color: #409EFF;
				font-size: 24rpx;
				margin-left: 20%;
				padding: 20rpx 0;
				line-height: 40rpx;
				padding-right: 20rpx;
				.text{
					flex: 1;
				}
				.add-detail{
					margin-left: 20rpx;
					width: 140rpx;
				}
			}
			
		}
	}
	.others-list{
		.others-detail{
			color: #409EFF;
			font-size: 24rpx;
			padding: 20rpx 0;
			line-height: 40rpx;
			padding-right: 20rpx;
			.add-detail{
				margin-left: 20rpx;
				width: 280rpx;
			}
		}
	}
	.dialog-title{
		text-align: center;
		padding: 30rpx 0;
	}
	.dialog-content{
		padding: 0 40rpx;
		padding-bottom: 40rpx;
		font-size: 28rpx;
		p{
			line-height: 40px;
			border-bottom: 1px solid #ccc;
		}
		
		.add-form{
			margin-bottom: 48rpx;
			.form-item{
				background: #eee;
				margin-bottom: 20rpx;
				padding:10rpx 0;
				padding-left: 20rpx;
				border-radius: 6rpx;
			}
		}
		.form-textarea{
			flex: 1;
			height: 160rpx;
			border: 1px solid #f0f0f0;
			overflow: auto;
			background: #eee;
			/deep/uni-textarea{
				width: 100%;
				height: 100%;
				font-size: 28rpx;
				.uni-textarea-compute{
					width: 100%;
					height: 100%;
					overflow: auto;
				}
			}
		}
		.submit-btn{
			margin: 0 auto;
			text-align: center;
			width: 100%;
			height: 80rpx;
			color: #fff;
			background: #409EFF;
			line-height: 80rpx;
			border-radius: 6px;
		}
	}
	.down{
		transform: rotate(0deg);
	}
	.up{
		transform: rotate(180deg);
	}
	.hidden{
		height: 0;
		transform: translateY(-100%);
		// opacity: 0;
	}
	.show{
		height: 100%;
		transform: translateY(0);
		// opacity: 1;
	}
	.dialog-scroll{
		height: 600rpx;
	}
}
</style>
