<template>
	<view class="history-container">
		<view class="tabs">
			<template v-if="isAccount!==0">
				<view class="tab" :class="tabIdx===1?'active':''" @click="tabclick(1)">安全巡检</view>
				<view class="tab" :class="tabIdx===2?'active':''" @click="tabclick(2)">隐患记录</view>						
<!-- 				<view class="tab" :class="tabIdx===4?'active':''" @click="tabclick(4)">危险源</view>	 -->
			</template>
			<view class="tab" :class="tabIdx===5?'active':''" @click="tabclick(5)">问题反馈</view>				
			<view class="tab" :class="tabIdx===3?'active':''" @click="tabclick(3)">满意度评价</view>			
		</view>
		<view class="back" @click="back" v-if="showback">返回</view>
		<view class="item-list-wrap" v-if="total>0">
			<view class="item-list basics" v-if="tabIdx===1||tabIdx===4">
				<view class="basics-item" v-for="(item,idx) in items" :key="idx" @click="showDetail(item)">
					<view class="basics-title">{{item.checklistTitle}}</view>
					<view class="basics-body">
						<view class="lf">
							<text class="time">{{item.recordTime}}</text>
							<text class="user">{{item.accountName||'微信用户'}}</text>							
						</view>
						<view class="status">{{checklistStatus(item.status)}}</view>
					</view>						
				</view>				
			</view>
			<view class="item-list basics" v-if="tabIdx===5">
				<view class="basics-item" v-for="(item,idx) in items" :key="idx" @click="showDetail(item)">
					<view class="basics-title">{{item.snapshotContent}}</view>
					<view class="basics-body">
						<view class="lf">
							<text class="time">{{item.submitTime}}</text>
							<template>
								<text class="user" v-if="item.handleAccountId > 0">{{ item.handleAccountName }}</text>
								<text class="user" v-else>{{item.wxName||'--'}}</text>
							</template>
						</view>
						<view class="status">{{snapshotStatus(item.status)}}</view>
					</view>						
				</view>				
			</view>
			<view class="item-list basics danger" v-if="tabIdx===2">
				<view class="basics-item" v-for="(danger,dangerIdx) in items" :key="dangerIdx">
					<view class="basics-item-sceneIcon">
						<img @click="showImg([danger.scenePhoto])" class="icon" :src="danger.sceneIcon" v-if="danger.sceneIcon" />
					</view>
					<view class="basics-item-info" @click="linkTo(danger)">
						<view class="basics-title">{{danger.dangerTitle}}</view>
						<view class="basics-body">
							<view class="lf">
								<view class="user">{{danger.curAccountName}}</view>							
								<view class="time">{{danger.dangerDeadLine}}</view>						
							</view>
							<view class="status">{{dangerStatus(danger.status)}}</view>
						</view>							
					</view>
				</view>				
			</view>		
			<view class="item-list satisfaction" v-if="tabIdx===3">
				<view class="uni-table">
					<view class="uni-table-tr">
						<view class="uni-table-th">姓名</view>
						<view class="uni-table-th" style="80px;text-align:right;">评分</view>	
					</view>
					<view class="uni-table-tr" v-for="(item,index) in items" :key="index"  @click="showDetail(item)">
						<view class="uni-table-td">{{item.accountName||'微信用户'}}</view>
						<view class="uni-table-td" style="80px;text-align:right;">{{item.score===null?'--':item.score}}</view>	
					</view>
				</view>
			</view>								
		</view>
		<template v-else>
			<view class="isEmpty" >
				<p class="word">没有数据 ^_^ </p>
			</view>
		</template>
	</view>
</template>
<script>
	import {getDangerByPage} from '@/api/aqpt/dangerApi.js'
	import { getchecklistByPage} from '@/api/aqpt/checklistPoint.js'
	import { getSnapshotByPage} from '@/api/aqpt/snapshotApi.js'
	import { getcompletedchecklistByPage} from '@/api/aqpt/checklist.js'	
	export default { 
		data() {
			return {
				conditions:{
					page:1,
					limit:15,					
				},
				total:0,
				items:[],
				tabIdx:1,
				isAccount:undefined,
				showback:window.android?true:false
			}
		},
		onShow() {
			this.init()
		},
		onLoad() {
			uni.$on('type',(tabIdx)=>{
				if(tabIdx){
					this.tabIdx=tabIdx					
					// this.tabclick(tabIdx)
				}
			})
		},
		methods: {	
			dangerStatus(i) {
			  if (i >= 0) {
			    const strs = ['待提交','待评审','待整改','待验收','已完成']
			    return strs[i]
			  } else {
			    return '已撤销'
			  }
			},
			checklistStatus(val){
				if(val===-1)return "巡检异常"
				const strs = ['未开始','未完成','完成']
				return strs[val]
			},
			dangerLevelFilter(val){
				let levels=['未知','一般隐患','重大隐患'];
				return levels[val]
			},
			snapshotStatus(val){
			  if (val >= 0) {
				const strs = [
				  '待处理',
				  '已处理'
				]
				return strs[val]
			  } else {
				return '已撤销'
			  }
			},
			init(){
				this.items=[]
				this.total=0
				this.conditions={
					limit:15,
					page:1
				}
				this.getData()
				this.clearDetail()
				this.isAccount=uni.getStorageSync('isAccount')
			},	
			tabclick(tabIdx){
				this.tabIdx=tabIdx
				this.init()
			},
			back(){
				if(window.android){
					window.android.webExit()					
				}
			},
			getData(){
				let qrcode=uni.getStorageSync('qrcode')
				let targetType=qrcode.targetType
				let targetId=qrcode.targetId
				if(this.tabIdx===2){
					getDangerByPage({
						...this.conditions,
						riskPointId:targetId
					}).then((res)=>{
						let items=JSON.parse(JSON.stringify(this.items))
						this.items=items.concat(res.data)
						this.total=res.total
					})				
				}
				if(this.tabIdx===1||this.tabIdx===3||this.tabIdx===4){
					let checklistTypeId=undefined;
					let enums={1:2,3:3,4:1}
					let params={
						...this.conditions,
						targetType,
						targetId,						
					}
					if(this.tabIdx!==1){params.checklistTypeId=enums[this.tabIdx]}
					let getchecklist=this.tabIdx===1?getcompletedchecklistByPage:getchecklistByPage
					getchecklist(params).then((res)=>{
						let items=JSON.parse(JSON.stringify(this.items))
						this.items=items.concat(res.data)
						this.total=res.total
					})
				}
				if(this.tabIdx===5){
					getSnapshotByPage({
						...this.conditions,
						targetType,
						targetId
					}).then((res)=>{
						let items=JSON.parse(JSON.stringify(this.items))
						this.items=items.concat(res.data)
						this.total=res.total
					})	
				}
			},
			changePageNumber({type,current}){
				this.conditions.page=current
				this.getData()
			},
			clearDetail(){
				if(uni.getStorageSync('detail-info')){
				  uni.removeStorageSync('detail-info')
				}				
			},
			linkTo(item){
				uni.setStorageSync('detail-info',item)
				uni.navigateTo({
					url:'/pages/history/detail/detail'
				})
			},
			showDetail(item){
				let pageUrl="";
				if(this.tabIdx===1){
					if(item.checklistTypeId===1){
						pageUrl="/pages/app_views/hazard/index/index"						
					}else{
						pageUrl="/pages/app_views/checkList/index/index"					
					}
				
				}
				if(this.tabIdx===3){
					pageUrl="/pages/app_views/satisfaction_evaluation/satisfaction_evaluation"
				}
				if(this.tabIdx===4){
					pageUrl="/pages/app_views/hazard/index/index"
				}
				if(this.tabIdx===5){
					pageUrl="/pages/app_views/problem_feedback/record/record"
					uni.navigateTo({
						url:`${pageUrl}?id=${item.snapshotId}`
					})	
					return
				}
				uni.navigateTo({
					url:`${pageUrl}?type=history&id=${item.checklistId}&recordId=${item.recordId}`
				})	
			},
			showImg(urls){
				uni.previewImage({
					urls
				})
			},
			isEmpty(val){
				if(val!=="undefined"&&val!==undefined&&val!==""&&val!==null){
					return false
				}
				return true
			}			
		},
		onUnload() {
			uni.$off('type')
		},
		onPullDownRefresh() {
			this.init()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if(this.total>=this.conditions.page*this.conditions.limit){
				this.conditions.page++
				this.getData()
			}
		}
	}
</script>

<style lang="scss" scoped>
.history-container{
	padding-bottom: 100rpx;
	min-height: 100vh;
	box-sizing: border-box;
	background-color: #F5F6F8;	
	.tabs{
		overflow-x: auto;
		display: flex;
	    background-color: #fff;	
		border-radius: 16rpx;
		.tab{
			height: 88rpx;
			line-height: 88rpx;
			display: inline-block;
			padding: 0 32rpx;
			flex-shrink: 0;
			font-size: 28rpx;
			color: #35364F;
			&.active{
				font-size: 34rpx;
				color: #4D73FF;
				font-weight: 700;
			}
		}
	}
	.back{
		width: 100rpx;
		height: 100rpx;
		background-color: #4D73FF;
		color: #fff;
		font-size: 32rpx;
		text-align: center;
		line-height: 100rpx;
		box-shadow:1px 2px 8px rgba(0,0,0,0.5);
		position: fixed;	
		right: 20rpx;
		top: 40%;
		z-index: 999;
		border-radius: 50%;
	}
}
.isEmpty{
	width: 100%;
	height: 100vh;
	background-image: url(data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgNzkgODYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjxkZWZzPjxsaW5lYXJHcmFkaWVudCBpZD0iYSIgeDE9IjM4Ljg1JSIgeTE9IjAlIiB4Mj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gMTUuNjQ2LS40MjctMTUuNDA3LTcuMTg1eiIvPjxwYXRoIGZpbGw9InVybCgjYikiIGQ9Ik0xMyA0NWg0MHYzNkgxM3oiLz48ZyB0cmFuc2Zvcm09InRyYW5zbGF0ZSg1MyA0NSkiPjxtYXNrIGlkPSJkIiBmaWxsPSIjZmZmIj48dXNlIHhsaW5rOmhyZWY9IiNjIi8+PC9tYXNrPjx1c2UgZmlsbD0iI0UwRTNFOSIgdHJhbnNmb3JtPSJtYXRyaXgoLTEgMCAwIDEgMTcgMCkiIHhsaW5rOmhyZWY9IiNjIi8+PHBhdGggZmlsbD0iI0Q1RDdERSIgbWFzaz0idXJsKCNkKSIgdHJhbnNmb3JtPSJtYXRyaXgoLTEgMCAwIDEgMjQgMCkiIGQ9Ik03IDBoMTdsLTQgMTgtMjAtMnoiLz48L2c+PHBhdGggZmlsbD0iI0Y4RjlGQiIgZD0iTTcwIDQ1SDUzbDkgMTNoMTd6Ii8+PC9nPjwvc3ZnPg==);
	background-size: contain;
	background-repeat: no-repeat;
	position: relative;
	background-color: #fff;
	/* #ifndef MP-WEIXIN */
		height: calc( 100vh - 100px);	
	/* #endif */
	.word{
		width: 100%;
		text-align: center;
		position: absolute;
		top: 80vh;
		left: 0;
		color: #999;
		font-size: 28rpx;
		letter-spacing: 2rpx;
	}
}
	
.item-list-wrap{
	padding: 0 20rpx;
	background-color: #F5F6F8;
	margin-top: 18rpx;
	.item-list{
		background-color: #fff;
	    border-radius: 16rpx;	
		overflow: hidden;
		.chat-custom-right{
			.chat-custom-text{
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 28rpx;
				color: #666666;					
			}
			.bottom{
				text-align: right;
				padding-top: 10rpx;	
				.status{
					font-size: 28rpx;
					line-height: 1;
					display: inline-block;				
					border: 1px solid #13ce66;
					color: #13ce66;
					padding: 8rpx 10rpx;
					border-radius: 4rpx;
				}
				.level{
					line-height: 1;
					display: inline-block;	
					font-size: 28rpx;
					color: #666666;		
				    margin-left: 10rpx;		
					border: 1px solid transparent;
					padding: 8rpx 10rpx;
					border-radius: 4rpx;				
					&-1{
						color:#13ce66 ;
						border-color: #13ce66;
					}
					&-2{
						color: red;
						border-color: red;				
					}
				}			
			}
	
		}
		&.basics{
			background-color: #F5F6F8;	
			.basics-item{
				padding:30rpx 40rpx ;
				border-radius: 16rpx;
				margin-bottom: 20rpx;
				background-color: #fff;	
				.basics-title{
					font-size: 32rpx;
					line-height: 48rpx;
					font-weight: 500;
					color: #212121;
				}
				.basics-body{
					display: flex;
					align-items: center;
					padding-top: 32rpx;
					.lf{
						flex: 1;
						.time{
							font-size: 24rpx;
							line-height: 48rpx;
							color: #999999;
							padding-left: 34rpx;
							background-position: center left; 
							background-repeat: no-repeat;
							background-size: 24rpx 24rpx;
							background-image: url('/static/time.png');
						}
						.user{
							margin-left:24rpx ;
							font-size: 24rpx;
							line-height: 48rpx;
							color: #999999;
							padding-left: 34rpx;
							background-position: center left;
							background-repeat: no-repeat;
							background-size: 24rpx 24rpx;
							background-image: url('/static/user.png');
						}
					}
					.status{
						font-size: 28rpx;
						line-height: 48rpx;
						color: #4D73FF;
					}
				}
			}			
		}
		&.satisfaction{
			padding: 10rpx 24rpx;
			&::v-deep{
				.uni-table-td,.uni-table-th{
					border: 0;
				}
			}
		}
		&.danger{
			.basics-item{
				display: flex;
				align-items: flex-start;
				.basics-item-sceneIcon{
					width:100rpx ;
					height: 100rpx;
					background-color: #f5f5f5;
					border-radius: 10rpx;
					overflow: hidden;
					.icon{
						display: block;
						width:100rpx ;
						height: 100rpx;
					}
				}
				.basics-item-info{
					flex: 1;
					padding-left: 16rpx;
					.basics-body{
						.lf{
							.user{
								margin-left: 0;
							}
						}
					}
				}				
			}
		}
	}
}
.custom-item-footer{
	.name{
		font-size: 30rpx;
		color: #333;
		text-align: right;
	}
	.status{
		font-size: 26rpx;
		color: #666;
		text-align: right;
		padding-top: 10rpx;
	}
}
</style>
