<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 {
		getchecklistRecord,
		checklistComplete,
		checklistCancel,
		checklistBatchUpdate,
	} from '@/api/aqpt/checklistPoint.js'
	export default {
		data() {
			return {
				defaultProps: {
					id: 'itemId', 
					children: 'children',
					label: 'itemTitle'
				},
				accountInfo:{},
				divider: false,
				edit: true,
				itemList:[],
				recordId:undefined,
				checklistId:undefined,
				handleVisiable:false,
				type:undefined,
				status:undefined
			}
		},
		onLoad({rid,cid}) {
			this.checklistId=cid	
			this.recordId=rid	
			let accountInfo=uni.getStorageSync('accountInfo')
			this.accountInfo=accountInfo
			this.getchecklistRecord(cid,rid)
		},
		methods: {
			getchecklistRecord(checklistId,recordId){
				getchecklistRecord(checklistId,recordId).then((res)=>{
					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)
				})
			},
			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;
				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>
