<!-- 顶部警告 -->
<template>
	<view class="top-warn" v-if="pageType">
		<view>{{text}}</view>
		<view @click="buttonClick" v-if="whetherRoute">查看</view>
	</view>
</template>

<script>
    import { selectTriggerInfo,evacuate,closeRiskPlan } from '@/api/basicsModules/index.js'

	export default {
		data() {
			return {
				pageType:false,
				text:"",
				buildId:"",
				floorId:"",
				subId:"",
				closePlan:false,//  true 火焰报警，false 没有火焰报警
				whetherRoute:true,

				subjectId:"",
				buildingId:"",
				subjectName:"",
				groupId:"",
			}
		},
		created() {

		},
		mounted(){
			// this.getWarn();
			// getApp().watch(this.lineDataFunction,'lineData');
		},
		methods: {
			//应急变更
			lineDataFunction(val){
				this.getWarn();
			},
			buttonClick(){
				let self = this;
				let list = [];
				if(self.closePlan){
					list = ['查看', '结束预案'];
				}else{
					self.goPage();
				}
				uni.showActionSheet({
					itemList: list,
					success: function (res) {
						if(res.tapIndex == 0){
							self.goPage();
						}else if(res.tapIndex == 1){
							self.offEvacuationClick();
						}
					},
					fail: function (res) {
						console.log(res.errMsg);
					}
				});
			},
			//结束预案
			offEvacuationClick(){
				let self = this;
				uni.showModal({
					content: '确认结束预案?',
					cancelColor:"#999",
					confirmColor:"#0183FA",
					success: function (res) {
						if (res.confirm) {
							self.closeRiskPlan();
						} else if (res.cancel) {
						}
					}
				});
			},
			async closeRiskPlan(){
				const {data} = await closeRiskPlan({id:this.groupId});
				if(data.code == 200){
					uni.showToast({
						title: '操作成功',
						icon:"none",
						mask:true,
						duration: 2000
					});
					this.pageType = false;
				}
			},
			//页面跳转
			goPage(){
				let obj = {
				  buildId: this.buildId,
				  floorId:this.floorId,
				  subId:this.subId,
				  groupId:this.groupId,
				}
				uni.navigateTo({
					url:'/pages_manage/emergencyEvacuationBig?item='+encodeURIComponent(JSON.stringify(obj))
				});
			},
			//获取报警信息
			async getWarn(){
				let self = this;
				const {data} = await selectTriggerInfo();
					if(data.data[0]){
					  this.$set(this,'text',data.data.length>1?'有多个实验室发生预案':'有实验室发生预案');
					  this.$set(this,'buildId',data.data[0].buildId);
					  this.$set(this,'floorId',data.data[0].floorId);
					  this.$set(this,'subId',data.data[0].subId);
					  if(data.data[0].riskAttribute == 1){
						this.$set(this,'closePlan',true);
						this.$set(this,'groupId',data.data[0].groupId);
					  }
					  this.$set(this,'pageType',true);
					 //  for(let i=0;i<data.data.length;i++){
						// if(data.data[i].riskAttribute == '1'&&data.data[i].ifCheck != '1'){
						// 	let obj = {
						// 	  buildId: data.data[i].buildId,
						// 	  floorId:data.data[i].floorId,
						// 	  subId:data.data[i].subId,
						// 	  riskPlanId:data.data[i].riskPlanId,
						// 	}
						// 	uni.navigateTo({
						// 		url:'/pages/emergencyEvacuationBig?item='+encodeURIComponent(JSON.stringify(obj))
						// 	});
						// 	return
						// }
					 //  }
					  //没有火焰预案并且没有查看过
					  for(let i=0;i<data.data.length;i++){
						if(data.data[i].riskAttribute != '1'&&data.data[i].ifCheck != '1'){
							let obj = {
							  buildId: data.data[i].buildId,
							  floorId:data.data[i].floorId,
							  subId:data.data[i].subId,
							  groupId:data.data[i].groupId,
							}
							uni.navigateTo({
								url:'/pages_manage/emergencyEvacuationBig?item='+encodeURIComponent(JSON.stringify(obj))
							});
							return
						}
					  }
					}else{
					  this.$set(this,'pageType',false);
					}
			},
			timeFuncontion(){
				let self = this;
				var t1 = setInterval(refreshCount, 300);
				function refreshCount() {
					let txt = self.text
					let start = txt.substring(0, 1);//取该字符串的第一个字符
					let end = txt.substring(1);//取该字符串的从1之后的所有字符
					let newText = end + start;//拼接新的字符串
					self.$set(self,'text',newText)
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
	.top-warn{
		height:80rpx;
		line-height:80rpx;
		background rgba(2550,0,0,0.2)
		margin:20rpx 0;
		display flex;
		overflow hidden
		view:nth-child(1){
			padding-left:20rpx;
			color:#FF0000;
			flex:1;
			view{
				padding-left:20rpx;
				white-space:nowrap;
			}
		}
		view:nth-child(2){
			width:140rpx;
			color:#0183FA;
			text-align center;
		}
	}
</style>
