<template>
	<view class="page">
		<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">【通过】</view>
									<view class="status" v-if="point.checkResult===-1">【不通过】</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">【通过】</view>
									<view class="status" v-if="checklistItem.checkResult===-1">【不通过】</view>
								</view>
							</view>
						</view>				
					</view>					
				</view>
				<view class="handle-container" v-if="handleVisiable">
					<view class="handle" @click="handleSubmit(3)">全部通过</view>
					<view class="handle" @click="handleSubmit(4)">全不通过</view>				
					<view class="handle" @click="handleSubmit(1)">提交</view>
					<view class="handle" @click="handleSubmit(2)">撤销</view>				
				</view>
			</template>
			<template v-else>没有可处理的清单^-_-^</template>
		</view>		
	</view>
</template>
<script>
	import {
		checklistInit,
		getchecklistRecord,
		checklistComplete,
		checklistCancel,
		checklistBatchUpdate,
	} from '@/api/aqpt/checklistPoint.js'
	export default {
		data() {
			return {
				defaultProps: {
					id: 'itemId', 
					children: 'children',
					label: 'itemTitle'
				},
				divider: false,
				edit: true,
				itemList:[],
				recordId:undefined,
				checklistId:undefined,
				handleVisiable:false,
				type:undefined,
				status:undefined
			}
		},
		onLoad({id,recordId,type}) {
			this.checklistId=id	
			this.recordId=recordId	
			this.type=type
			if(type==="app"){
			    this.initCheckList(id)	
				this.handleVisiable=true
			}else{
				this.getchecklistRecord(id,recordId)			
			}		
		},
		// onShow() {
		// 	let point=uni.getStorageSync('point')
		// 	if(!point)return	
		// 	this.handleVisiable=true					
		// 	this.getchecklistRecord(point.checklistId,point.recordId)
		// 	uni.removeStorageSync('point')
		// },
		methods: {
			async initCheckList(checklistId){
				let qrcode=uni.getStorageSync('qrcode')	
				let {data}=await checklistInit({
					ocId:qrcode.ocId,
					targetId:qrcode.targetId,
					targetType:qrcode.targetType,
					targetTitle:qrcode.targetTitle,
					targetGroupId:qrcode.groupId,
					targetGroupName:qrcode.groupName,
					checklistId
				})
				this.itemList=data.itemList
				this.recordId=data.recordId
			},
			getchecklistRecord(checklistId,recordId){
				/* status
					0-未初始化；1-初始化完成 2-完成 -1-巡检完成【异常】
				*/
				getchecklistRecord(checklistId,recordId).then((res)=>{
					this.itemList=res.data.itemList
					 let wxId=uni.getStorageSync('wxId')
					 this.status=res.data.status
					this.handleVisiable=(res.data.status===0||res.data.status===1)&&(res.data.accountId===wxId)
				})
			},
			check(point){		
				uni.setStorageSync('point',point)	
				if(this.type==='history'){	
					if(this.status===2){
						uni.navigateTo({
							url:"/pages/app_views/checkList/detail/detail"
						})						
					}
					if(this.handleVisiable){
						uni.navigateTo({
							url:"/pages/app_views/checkList/form/form"
						})							
					}
				}else{
				if(!this.handleVisiable) return						
					uni.navigateTo({
						url:"/pages/app_views/checkList/form/form"
					})				
				}								
			},
			handleSubmit(type){
				let recordId=this.recordId;
				let checklistId=this.checklistId;
				if(type===1){//完成
					checklistComplete(recordId,checklistId).then(()=>{
						uni.$emit('type',1)						
						uni.switchTab({
							url:'/pages/history/history'
						})
					}).catch(()=>{
						uni.showToast({
							icon:"none",
							title:"操作失败"
						})
					})
				}
				if(type===2){//放弃
					checklistCancel(recordId).then(()=>{
						uni.switchTab({
							url:'/pages/history/history'
						})
					}).catch(()=>{
						uni.showToast({
							icon:"none",
							title:"操作失败"
						})
					})
				}
				if(type===3){
					checklistBatchUpdate(recordId,1).then(()=>{
						this.getchecklistRecord(checklistId,recordId)
					})
				}
				if(type===4){
					checklistBatchUpdate(recordId,-1).then(()=>{
						this.getchecklistRecord(checklistId,recordId)
					})
				}			
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page{
		background-color: #F5F6F8;
		padding: 20rpx;
		min-height: 100vh;
		box-sizing: border-box;
		margin-bottom: 100rpx;
	}	
	.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;
						}
					}
				}
			}
		}
		.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;
			.handle{
				display: inline-block;
				background-color: #3384FF;
				color: #fff;
				padding: 15rpx 20rpx;
				width: 25%;
				box-sizing: border-box;
				border-right: 1rpx solid #eaeaea;
				font-size: 30rpx;
				text-align: center;
				&:nth-child(4){
					border: 0;
				}
			}
		}
	}
</style>
