<template>
	<view class="page">
<!-- 		<view class="page-head">
			<view class="handle" @click="handleSubmit(1)">全部通过</view>
			<view class="handle" @click="handleSubmit(2)">全不通过</view>				
		</view> -->
		<view class="wrap">
			<template v-if="itemList.length>0">
				<view class="checklist" v-for="(checklist,idx) in itemList" :key="idx">
					<view class="title more">{{checklist.itemTitle}}</view>
					<view class="item-cont" v-if="checklist.children&&checklist.children.length>0">
						<view class="checklistItem" v-for="(checklistItem,itemIdx) in checklist.children" :key="itemIdx">
							<view class="itemTitle more">{{checklistItem.itemTitle}}</view>
							<view  class="point-cont" v-if="checklistItem.recordList&&checklistItem.recordList.length>0">
								<view class="point" v-for="(point,pointIdx) in checklistItem.recordList" :key="pointIdx" @click="check(point)">
									<view class="pointContent">{{point.pointContent}}</view>
									<view class="status" v-if="point.checkResult===1">【<span class="text">通过</span>】</view>
									<view class="status" v-if="point.checkResult===-1">【<span class="text">不通过</span>】</view>
								</view>
							</view>
						</view>				
					</view>
					<view class="item-cont" v-if="checklist.recordList&&checklist.recordList.length>0">
						<view class="checklistItem" v-for="(checklistItem,itemIdx) in checklist.recordList" :key="itemIdx">
							<view  class="point-cont">
								<view class="point" @click="check(checklistItem)">
									<view class="pointContent">{{checklistItem.pointContent}}</view>
									<view class="status" v-if="checklistItem.checkResult===1">【<span class="text">通过</span>】</view>
									<view class="status" v-if="checklistItem.checkResult===-1">【<span class="text">不通过</span>】</view>
								</view>
							</view>
						</view>				
					</view>					
				</view>
				<view class="handle-container" v-if="handleVisiable">
					<view class="handle" @click="handleSubmit(1)">全部通过</view>
					<view class="handle" @click="handleSubmit(2)">全不通过</view>	
					<view class="handle submit" @click="handleSubmit(3)" v-if="submit_bt_state">提交</view>
<!-- 					<view class="handle danger" @click="handleSubmit('danger')">登记隐患</view> -->
					<view class="more" @mouseenter="hover(1)" @mouseleave="hover">
						<text class="text">更多</text>
						<view class="list" v-if="showMore">
							<view class="hand-item-box">
								<view class="handle-item" @click="handleSubmit(4)">撤销</view>
								<view class="handle-item" @click="handleSubmit(5)">转交</view>																	
							</view>						
						</view>						
					</view>
				</view>
			</template>
			<template v-else>没有可处理的清单^-_-^</template>
		</view>		
	</view>
</template>
<script>
	import {
		getchecklistRecord,
		checklistComplete,
		checklistCancel,
		checklistBatchUpdate,
	} from '@/api/aqpt/checklistPoint.js'
	import {cancelCheckTask,completeCheckTask} from '@/api/aqpt/task.js'	
	export default {
		data() {
			return {
				defaultProps: {
					id: 'itemId', 
					children: 'children',
					label: 'itemTitle'
				},
				danger_bt_state:false,
				submit_bt_state:false,
				accountInfo:{},
				divider: false,
				edit: true,
				itemList:[],
				recordId:undefined,
				checklistId:undefined,
				handleVisiable:false,
				type:undefined,
				status:undefined,
				taskTitle:"",
				showMore:false
			}
		},
		onLoad({rid,cid,title}) {
			this.checklistId=cid	
			this.recordId=rid	
			let accountInfo=uni.getStorageSync('accountInfo')
			this.accountInfo=accountInfo
			this.taskTitle=title
			this.getchecklistRecord(cid,rid)
		},
		methods: {
			getchecklistRecord(checklistId,recordId){
				getchecklistRecord(checklistId,recordId).then((res)=>{
					if(!res.data.itemList)return
					this.itemList=res.data.itemList
					this.status=res.data.status
					this.handleVisiable=(res.data.status===0||res.data.status===1)&&(res.data.accountId===this.accountInfo.userId)
					let items=[]
					for(let i=0;i<itemList.length;i++){
						if(itemList[i].children){
							itemList[i].children.map(child=>{
								child.recordList.map(pointitem=>{
									items.push(point)
								})
							})
						}else{
							itemList[i].recordList.map(pointitem=>{
								items.push(pointitem)
							})							
						}						
					}
					let index=items.findIndex(item=>item.checkResult===-1)
					let _index=items.findIndex(item=>item.checkResult===0)
					this.submit_bt_state=_index<0
					this.danger_bt_state=index>-1?true:false
				})
			},
			check(point){		
				if(!this.handleVisiable) return	
				uni.setStorageSync("point",point)
				uni.navigateTo({
					url:"/pages/index/handle/task/checkList/form/form"
				})								
			},
			handleSubmit(type){
				let recordId=this.recordId;
				let checklistId=this.checklistId;
				const self=this;
				if(type===1){
					checklistBatchUpdate(recordId,1).then(()=>{
						this.getchecklistRecord(checklistId,recordId)
					})
				}
				if(type===2){
					checklistBatchUpdate(recordId,-1).then(()=>{
						this.getchecklistRecord(checklistId,recordId)
					})
				}				
				if(type===3){//完成
					if(this.danger_bt_state){
						uni.showModal({
							title: '提示',
							content: '本次检查有未通过项，是否需要登记隐患？',
							success: function (res) {
								if (res.confirm) {
									self.submitDanger(recordId)
								} else if (res.cancel) {
									self.completeTask(recordId)
								}
							}
						});
					}else{
						this.completeTask(recordId)
					}
				}
				if(type===4){//放弃
					cancelCheckTask(recordId).then(()=>{
						uni.switchTab({
							url:'/pages/index/index'
						})
					}).catch(()=>{
						uni.showToast({
							icon:"none",
							title:"操作失败"
						})
					})
				}	
				if(type===5){//转交
					let taskTitle=this.taskTitle;
					uni.navigateTo({
						url:`/pages/index/handle/task/checkList/form/transferCheckTask?tid=${recordId}&title=${taskTitle}`
					})
				}
				if(type==="danger"){
					this.submitDanger(recordId)
				}
			},
			completeTask(recordId){
				completeCheckTask(recordId).then(()=>{				
					uni.reLaunch({
						url:'/pages/index/index'
					})
				}).catch(()=>{
					uni.reLaunch({
						url:`/pages/webview/webview?href=${href}&name=${name}`
					})
					uni.showToast({
						icon:"none",
						title:"操作失败"
					})
				})					
			},
			submitDanger(recordId){
				let href=`${this.$h5}/danger/views/create.html`;
				let name= '登记隐患'	
				uni.reLaunch({
					url:`/pages/webview/webview?href=${href}&name=${name}`
				})
				completeCheckTask(recordId)							
			},
			hover(state){
				this.showMore=state===1
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page{
		background-color: #F5F6F8;
		padding: 20rpx;
		min-height: 100vh;
		box-sizing: border-box;
		margin-bottom: 100rpx;
		.page-head{
			text-align: right;
			padding-bottom: 10rpx;
			.handle{
				display: inline-block;
				background-color: #3384FF;
				color: #fff;
				padding: 10rpx 15rpx;
				box-sizing: border-box;
				border-right: 1rpx solid #eaeaea;
				font-size: 30rpx;
				text-align: center;
				&:nth-child(4){
					border: 0;
				}
			}
		}
	}	
	.wrap{
		padding: 20rpx;
		background-color: #fff;
		.checklist{
			.title{
				color: #222222;
				font-size: 32rpx;
				height: 98rpx;
				line-height: 98rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				border-bottom: 1px dashed #E8E8E8;				
			}
			.more{
				background-image: url('/static/tree_more.png');
				background-size: 30rpx 30rpx;
				padding-left: 40rpx;
				background-position: center left;
				background-repeat: no-repeat;
			}
			.item-cont{
				padding-left: 20rpx;
				border-bottom: 1px dashed #E8E8E8;	
				.checklistItem{
					.itemTitle{
						color: #222222;
						font-size: 32rpx;	
						height: 98rpx;
						line-height: 98rpx;
					}
				}
				.point-cont{
					padding-left: 30rpx;
					.point{
						display: flex;						
						color: #666;
						font-size: 28rpx;	
						height: 98rpx;
						line-height: 98rpx;
						.pointContent{
							flex: 1;
						}
						.status{
							.text{
								color: #3384FF;
								font-weight: 800;
							}
						}
					}
				}
			}
		}
			
		.handle-container{
			position: fixed;
			width: 100%;
			left: 0;
			bottom: 0;
			background-color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
			border-top: 1rpx solid #eaeaea;
			padding: 15rpx 0;
			.handle{
				display:inline-block;
				color: #222;
				padding: 15rpx 20rpx;
				box-sizing: border-box;
				border: 1rpx solid #eaeaea;
				font-size: 26rpx;
				text-align: center;
				border-radius: 50rpx;
				margin: 0 10rpx;
				&.danger{
					background-color: #FA5B41;	
					color: #fff;						
				}
				&.submit{
					background-color: #3384FF;	
					color: #fff;
				}
			}
			.more{
				position: relative;
				padding: 15rpx 20rpx;
				.text:hover{
					&+.list>.hand-item-box{
						display: block;
					}
				}
				.list{
					position: absolute;
					bottom: 100rpx;
					right: 0;
					box-shadow: 0px 2px 12px 0px #0000001F;
					padding:0 20rpx;
					background-color: #fff;
					.hand-item-box{
						position: relative;
						display: none;
						&.show{
							display: block;
						}
						&::after{
							width: 0;
							height: 0;
							content: "";
							display: block;
							border-width: 26rpx 26rpx 0 0;
							border-color: #fff transparent;
							border-style: solid;
							position: absolute;
							bottom: -20rpx;
							right: -20rpx;
						}
						.handle-item{
							width: 180rpx;
							height: 50rpx;
							font-size: 26rpx;
							line-height: 50rpx;
							background-color: #fff;
							text-align: center;
							border-bottom: 1px solid #ccc;
							padding:10rpx 0;
							&:last-child{
								border: 0;
							}
						}						
					}

				}
			}
		}
	}
</style>
