<template>
	
	<view class="index-detail-page">
		<view class="container">
			<view class="tab-cont">
				<view class="part" v-if="type==='task'">
<!-- 					<view class="tab">
						<view class="tab-item" :class="activeTab===1?'active':''" @click="tabChange(0,1)">待处理</view>
						<view class="tab-item" :class="activeTab===2?'active':''" @click="tabChange(1,2)">已完成</view>
						<view class="tab-item" :class="activeTab===3?'active':''" @click="tabChange(-1,3)">已撤销</view>
					</view> -->
					<view class="part-cont">
						<view class="item" v-for="(item,taskIdx) in items" :key="item.taskId" @click="handle('task',item)">
							<view class="index">
								{{taskIdx+1}}
							</view>							
							<view class="title">
								<view class="name">{{item.taskTitle}}</view>
								<view class="state">{{taskStatus(item.status)}}</view>
							</view>
							<view class="tags">
								<text class="tag">{{taskPriority(item.taskPriority)}}</text>
								<text class="tag">{{item.taskCatTitle}}</text>
								<text class="tag">{{taskType(item.taskTypeId)}}</text>
							</view>
							<view class="time">
								预期结束时间：{{item.expectedEndTime}}
							</view>
							<view class="bottom">
								<view class="user">
									<image class="avatar" src="/static/index/user.png" mode="widthFix"></image>
									处理人：{{item.handleAccountName}}
								</view>		
								<view class="hand-time">
									{{item.launchTime}}
								</view>																
							</view>
						</view>
					</view>
				</view>
				<view class="part" v-if="type==='warn'">
<!-- 					<view class="tab">
						<view class="tab-item" :class="activeTab===1?'active':''" @click="tabChange(0,1)">待处理</view>
						<view class="tab-item" :class="activeTab===2?'active':''" @click="tabChange(1,2)">已处理</view>
						<view class="tab-item" :class="activeTab===3?'active':''" @click="tabChange(-1,3)">已撤销</view>
					</view> -->
					<view class="part-cont">
						<view class="item" v-for="(item,warnIdx) in items" :key="item.warnId" @click="handle('warn',item)">
							<view class="index">
								{{warnIdx+1}}
							</view>
							<view class="title">
								<view class="name">{{item.warnTitle}}</view>
								<view class="state">{{warningStatus(item.status)}}</view>
							</view>
							<view class="tags">
								<text class="tag">{{alertLevel(item.warnLevel)}}</text>
							</view>
							<view class="time">
								发生时间：{{item.warnTime}}
							</view>
							<view class="bottom" v-if="item.handleAccountName">
								<view class="user">
									<image class="avatar" src="/static/index/user.png" mode="widthFix"></image>
									处理人：{{item.handleAccountName}}
								</view>		
								<view class="hand-time">
									{{item.handleTime}}
								</view>																
							</view>
						</view>
					</view>
				</view>
				<view class="part" v-if="type==='alert'">
<!-- 					<view class="tab">
						<view class="tab-item" :class="activeTab===1?'active':''" @click="tabChange(0,1)">活动</view>
						<view class="tab-item" :class="activeTab===2?'active':''" @click="tabChange(1,2)">已解除</view>
						<view class="tab-item" :class="activeTab===3?'active':''" @click="tabChange(-1,3)">已撤销</view>
					</view> -->
					<view class="part-cont">
						<view class="item" v-for="(item,alertIdx) in items" :key="item.alertId" @click="handle('alert',item)">
							<view class="index">
								{{alertIdx+1}}
							</view>
							<view class="title">
								<view class="name">{{item.alertTitle}}</view>
								<view class="state">{{alertStatus(item.status)}}</view>
							</view>
							<view class="tags">
								<text class="tag">{{alertLevel(item.alertLevel)}}</text>
							</view>
							<view class="time">
								发生时间：{{item.alertTime}}
							</view>
							<view class="bottom" v-if="item.clearTime">		
								<view class="hand-time">
									解除时间：{{item.clearTime}}
								</view>																
							</view>
							<view class="bottom" v-if="item.alertContent">
								<view class="alertContent">
									内容：{{item.alertContent}}
								</view>																
							</view>
						</view>
					</view>
				</view>
				<view class="part" v-if="type==='danger'">
<!-- 					<view class="tab">
						<view class="tab-item" :class="activeTab===1?'active':''" @click="tabChange(0,1)">待处理</view>
						<view class="tab-item" :class="activeTab===2?'active':''" @click="tabChange(1,2)">我提交</view>
						<view class="tab-item" :class="activeTab===3?'active':''" @click="tabChange(2,3)">我处理</view>
						<view class="tab-item" :class="activeTab===4?'active':''" @click="tabChange(-1,4)">我撤销</view>
					</view> -->
					<view class="part-cont">
						<view class="item" v-for="(item,dangerIdx) in items" :key="item.dangerId" @click="handle('danger',item)">
							<view class="index">
								{{dangerIdx+1}}
							</view>
							<view class="title">
								<view class="name">{{item.dangerTitle}}</view>
								<view class="state">{{dangerStatus(item.status)}}</view>
							</view>
							<view class="tags">
								<text class="tag">{{dangerLevel(item.dangerLevel)}}</text>
								<text class="tag">{{item.dangerCatTitle}}</text>
							</view>
							<view class="time">
								预期结束时间：{{item.dangerDeadLine}}
							</view>
							<view class="supervise">
								<span class="supervise-item">督办部门：{{item.superviseGroupName}}</span>
								<span class="supervise-item">督办人员：{{item.superviseAccountName}}</span>
							</view>					
							<view class="bottom">
								<view class="user">
									<image class="avatar" src="/static/index/user.png" mode="widthFix"></image>
									处理人：{{item.curAccountName}}
								</view>		
								<view class="hand-time">
									{{item.submitTime}}
								</view>																
							</view>
						</view>
					</view>
				</view>
			</view>										
		</view>
		<template v-if="total<1">
			<view class="isEmpty" >
				<p class="word">没有数据 ^_^ </p>
			</view>
		</template>
		<uni-popup ref="alertpopup" is-mask-click>
			<view class="alert-container">
				<uni-list>
					<uni-list-item title="名称" :rightText="alertView.alertTitle"></uni-list-item>
					<uni-list-item title="等级" :rightText="alertView.level"></uni-list-item>
					<uni-list-item title="责任主体" :rightText="alertView.gpainfo"></uni-list-item>
					<uni-list-item title="名称" :rightText="alertView.alertTitle"></uni-list-item>
					<uni-list-item title="发生时间" :rightText="alertView.alertTime" v-if="alertView.alertTime"></uni-list-item>
					<uni-list-item title="解除时间" :rightText="alertView.clearTime" v-if="alertView.clearTime"></uni-list-item>
					<uni-list-item title="详情" :note="alertView.alertContent"></uni-list-item>		
					<uni-list-item>
						<template v-slot:body>
							<view class="list-item-handle">
								<button type="default" class="handle-bt del" @click="handAlert">删除</button>
								<button type="default" class="handle-bt cancel" @click="cancelAlert">取消</button>
							</view>	
						</template>
					</uni-list-item>
				</uni-list>	
	
			</view>
		</uni-popup>		
	</view>
</template>

<script>
	import { getAlertByPage,deleteAlertById } from '@/api/aqpt/alertApi.js';
	import { getDangerByPage } from '@/api/aqpt/dangerApi.js';
	import { getTaskByPage } from '@/api/aqpt/taskApi.js';
	import { getWarningByPage } from '@/api/aqpt/warning.js';	
	import { handleCheckTask } from '@/api/aqpt/checkTaskApi'
	import{
		taskStatus,taskPriority,taskType,
		alertLevel,alertStatus,
		dangerStatus,dangerLevel,
		warningLevel,warningStatus
	} from '@/utils/enum.js';	
	export default {
		data() {
			return {
				type:undefined,
				activeTab:1,
				user:{},
			    conditions: {
					page: 1,
					limit: 10,
					status:0
			    },
				total:0,
				items:[],
				alertView:{}		
			}
		},
		onLoad({type,name,rid}) {
			this.type=type
			this.riskPointId=rid
			let user=uni.getStorageSync('accountInfo');
			this.user=user
			uni.setNavigationBarTitle({
				title:`${name||'详情'}`
			})
			this.tabChange(0,1)
		},
		methods: {
			taskStatus,taskPriority,taskType,
			alertLevel,alertStatus,
			dangerStatus,dangerLevel,
			warningLevel,warningStatus,
			getData(){
				if(this.type==='task'){
					this.getTaskByPage()
				}
				if(this.type==='danger'){
					delete this.conditions.status
					this.getDangerByPage()
				}				
				if(this.type==='alert'){
					this.getAlertByPage()
				}
				if(this.type==='warn'){
					this.getWarningByPage()
				}
				
			},
			getWarningByPage(){
				let items=JSON.parse(JSON.stringify(this.items))
				getWarningByPage(this.conditions).then((res)=>{
					this.items=items.concat(res.data)
					this.total=res.total
				})				
			},
			getAlertByPage(){
				let items=JSON.parse(JSON.stringify(this.items))
				getAlertByPage(this.conditions).then((res)=>{
					this.items=items.concat(res.data)
					this.total=res.total
				})				
			},
			getDangerByPage(){
				let items=JSON.parse(JSON.stringify(this.items))
				getDangerByPage(this.conditions).then((res)=>{
					this.items=items.concat(res.data)
					this.total=res.total
				})				
			},
			getTaskByPage(){
				let items=JSON.parse(JSON.stringify(this.items))
				getTaskByPage(this.conditions).then((res)=>{
					this.items=items.concat(res.data)
					this.total=res.total
				})				
			},
			tabChange(value,index){
				this.activeTab=index
				this.total=0,
				this.items=[]
				this.conditions= {page: 1,limit: 10}
				if(this.type==='task'||this.type==='alert'||this.type==='warn'){
					this.conditions.status=value
				}
				if(this.type==='danger'){
					  if (index === 1) {
						this.conditions.curAccountId = this.user.userId
					  } else if (index === 2) {
						this.conditions.submitAccountId = this.user.userId
					  } else if (index === 3) {
						this.conditions.handleAccountId = this.user.userId
					  } else if (index === 4) {
						this.conditions.submitAccountId = this.user.userId
						this.conditions.status = -1
					  }
				}
	            this.conditions.riskPointId=this.riskPointId								
				this.getData()
			},
			handle(type,item){			
				let url=""
				if(type==='danger'){				
					// uni.navigateTo({
					// 	url:`/pages/webview/webview?href=${this.$h5}/danger/views/detail.html&dangerId=539241&name=隐患处理`
					// })	
					uni.navigateTo({
						url:`/pages/index/handle/danger/index/index?id=${item.dangerId}`
					})					
										
					return
				}
				if(type==='task'){this.handTask(item);return};
				if(type==='warn'){	
					if(this.conditions.status!==0) return;
					// if(item.handleAccountId!==this.user.userId) return;
					url=`/pages/index/handle/warning/warning?id=${item.warnId}`
				}	
				if(type==='alert'){					
					  const levelstrs = [
					    '未知',
					    '1级',
					    '2级',
					    '3级',
					    '4级',
					    '5级',
					    '6级'
					  ]
					item.gpainfo=`${item.groupName}${item.positionName}${item.accountName}`
					item.level=levelstrs[item.alertLevel]
					this.alertView=item
					this.$refs.alertpopup.open('bottom')
					return
				}
				uni.navigateTo({
					url
				})				
			},
			handTask(item){
				// if(item.handleAccountId!==this.user.userId) return;
				if(this.conditions.status!==0) return;				
				handleCheckTask(item.taskId).then((res)=>{
					const { checklistId, checklistTypeId } = res.data //recordId-taskId
					if (checklistTypeId === 1) {// HazardRecordView
						uni.navigateTo({
							url:`/pages/index/handle/task/hazardRecord/hazardRecord?rid=${item.taskId}&cid=${checklistId}&title=${item.taskTitle}`
						})															  
					} else if (checklistTypeId === 2) {// PointRecordView
						uni.navigateTo({
							url:`/pages/index/handle/task/task?rid=${item.taskId}&cid=${checklistId}&title=${item.taskTitle}`
						})																  
					} else if (checklistTypeId === 3) {// ScoreRecordView	
						uni.navigateTo({
							url:`/pages/index/handle/task/task?rid=${item.taskId}&cid=${checklistId}&title=${item.taskTitle}`
						})															  
					}
				})
			
			},
			cancelAlert(){
				this.alertView={}
				this.$refs.alertpopup.close()
			},
			handAlert(){
				  const { alertId } = this.alertView
				  const self=this;
					uni.showModal({
						title: '提示！',
						content: '此操作将永久删除, 是否继续?',
						success: function (res) {
							if (res.confirm) {
								deleteAlertById(alertId).then((resp) => {
									const {msg,code}=resp	
									self.$refs.alertpopup.close()
									self.getData()
									uni.showToast({
										icon:"none",
										title:msg
									})																	
								})
							} else if (res.cancel) {
							  uni.showToast({
								icon:"none",
								title:"已取消删除！"
							  })
							}
						}
					});
			},
			onPullDownRefresh() {					
				this.tabChange(this.conditions.status||0,this.activeTab)
				uni.stopPullDownRefresh()
			},
			onReachBottom() {
				if(this.total>=this.conditions.page*this.conditions.limit){
					this.conditions.page++
					this.getData()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.index-detail-page {
		min-height: 100vh;
		padding-bottom: 100rpx;
		background-color: rgba(245, 246, 248, 1);
		padding-top: 6rpx;
		.container{	
			.tab-cont{				
				.part{
					.tab{
						display: flex;
						align-items: center;
						justify-content: center;
						overflow-x: auto;
						background-color: #fff;
						padding:32rpx;
						.tab-item{
							display: inline-block;
							padding: 0 36rpx;
							background-color: #fff;
							text-align: center;      
							flex-shrink: 0;
							line-height: 1;
							font-size: 32rpx;
							color: #333;
							box-sizing: border-box;
							border-bottom: 4rpx solid transparent;
							&.active{
								// background-color: #1e80ff;
								// color: #fff;
								// border-color: #1e80ff;
								color: #1e80ff;
								font-weight: 800;
							}
						}
					}
					.part-cont{
						padding: 0 32rpx;
						.item{
							background-color: #fff;
							border-radius: 12rpx;
							margin-top: 16rpx;
							padding: 24rpx 20rpx 24rpx 55rpx;
							position: relative;
							.index{
								width: 44rpx;
								height: 44rpx;
								display: flex;
								align-items: center;
								justify-content: center;
								border-radius: 50%;
								color:#ea8259;
								background-color:#fbf1ed;
								font-size:18rpx;
								position: absolute;
								left: 4rpx;
								top: 26rpx;
								word-break: break-all;
							}							
							.title{
								display: flex;
								justify-content: space-between;
								align-items: center;
								.name{
									font-size: 32rpx;
									color: rgba(0, 0, 0, 0.8);
								}
								.state{
									font-size: 30rpx;
									color: rgba(250, 139, 55, 1);
								}
							}
							.tags{
								padding-top: 24rpx;
								.tag{
									background: rgba(245, 246, 248, 1);	
									font-size: 20rpx;
									margin-left: 10rpx;
									padding: 10rpx 20rpx;
									&:nth-child(1){
										color: rgba(234, 130, 89, 1);
										background: rgba(251, 241, 237, 1);
									}
								}
							}
							.time{
								padding-top: 20rpx;
								font-size: 28rpx;									
								color:rgba(241, 57, 57, 0.8);
							}
								
							.supervise{
								font-size: 24rpx;
								color: rgba(0, 0, 0, 1);
								padding: 10rpx 0;
								.supervise-item{
									padding-right: 10rpx;
								}
							}
							.bottom{
								display: flex;
								justify-content: space-between;
								align-items: center;
								padding-top: 20rpx;
								.user{
									display: flex;
									align-items: center;
									font-size: 24rpx;
									color: rgba(0, 0, 0, 1);
									.avatar{
										width: 44rpx;
										margin-right: 10rpx;
									}
								}
								.hand-time{
									font-size: 24rpx;
									color: rgba(0, 0, 0, 1);
								}
							}
							.alertContent{
								font-size: 26rpx;
								color: #666;
								line-height: 1.5;
								text-overflow: ellipsis;
								overflow: hidden;
								word-break: break-all;
								display: -webkit-box;
								-webkit-box-orient: vertical;
								-webkit-line-clamp: 2;
							}
						}
					}
				}
			}
		}
		.isEmpty{
			width: 100%;
			height: 100vh;
			background-image: url(data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgNzkgODYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjxkZWZzPjxsaW5lYXJHcmFkaWVudCBpZD0iYSIgeDE9IjM4Ljg1JSIgeTE9IjAlIiB4Mj0iNjEuMTUlIiB5Mj0iMTAwJSI+PHN0b3Agc3RvcC1jb2xvcj0iI0ZDRkNGRCIgb2Zmc2V0PSIwJSIvPjxzdG9wIHN0b3AtY29sb3I9IiNFRUVGRjMiIG9mZnNldD0iMTAwJSIvPjwvbGluZWFyR3JhZGllbnQ+PGxpbmVhck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Ij48dXNlIHhsaW5rOmhyZWY9IiNjIi8+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;
			}
		}
		.handle-bt{
			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;
			&.del{
				background-color: #FA5B41;	
				color: #fff;						
			}
			&.submit{
				background-color: #3384FF;	
				color: #fff;
				padding: 18rpx 38rpx;
			}
		}
		.list-item-handle{
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			.handle-bt{
				min-width: 120rpx;
				padding: 0;
				&.cancel{
					margin: 0 20rpx;
				}
			}
		}
	}

</style>
