<template>
	<view>
		<uni-nav-bar  fixed="true"  height="80px" dark color="#ffffff" backgroundColor="#1989fa" shadow left-icon="home"
				title="巡更点" @clickLeft="GoBackHome()"/>		
		<button style="height:40px;line-height:40px;border-radius:4px;background-color:#1989fa;color:#fff;border:#1989fa;margin:5px 20px;" class="button" type="info" @click="NfcScan()">NFC感应</button>
			
	    <uni-section type="line" padding style="margin-left:5px">
	    	<text class="uni-subtitle">巡更点编号</text>
	    	<uni-easyinput style="margin: 10px 10px;width: 95%;" disabled class="uni-mt-4" trim="all" v-model="nfcId"></uni-easyinput>		
	    </uni-section>
		
		<uni-section type="line" padding style="margin-left:5px">
			<text class="uni-subtitle">巡更点名称</text>
			<uni-easyinput style="margin: 10px 10px;width: 95%;" class="uni-mt-5" trim="all" v-model="pointname"></uni-easyinput>		
		</uni-section>
		<text class="uni-subtitle" style="margin-left:5px">项目楼栋配置</text>	  
		<view class="checkout">
			<view class="select_box">
				<picker @change="ChangeProject" :range="exitDataproject">
					<label class="">{{ currExitproject }}</label>
				</picker>
			</view>
			<view class="select_box">
				<picker @change="ChangeBuild" :range="exitDatabuild">
					<label class="">{{ currExitbuild }}</label>
				</picker>
			</view>	
			<view class="select_box">
				<picker @change="ChangeHouse" :range="exitDatahouse">
					<label class="">{{ currExithouse }}</label>
				</picker>
			</view>	
		</view>  
		<button style="height:40px;line-height:40px;border-radius:4px;background-color:#1989fa;color:#fff;border:#1989fa;margin:5px 20px;" class="button" type="info" @click="SaveData()">确定</button>	
		
	</view>
</template>

<script>
	import NFC from "../../static/js/nfc.js"
	export default{		
		data(){
			return{
				nfcId:'',	
				pointname:'',
				exitDataproject:[],
				exitDatabuild:[],
				exitDatahouse:[],
				currIndex:0,
				currExitproject:'请选择项目',
				currExitbuild:'请选择楼栋',
				currExithouse:'请选择房间'
			}
		},
		created(){
			this.GetProjectList()						
		},
		methods:{
			async NfcScan(){
				// 这里用异步获取读取到的NFC数据					
				 this.nfcId =await NFC.listenNFCStatus()					 
				 console.log("nfcId:---"+this.nfcId)					 
				    if(!this.nfcId) return	    
				
			},
			
			SaveData(){	
				if(this.nfcId==''){
					uni.showToast({
						icon:'none',
						title:'巡更点编号不能为空'
					})
					
					return
				}
				
				if(this.pointname==''){
					uni.showToast({
						icon:'none',
						title:'巡更点名称不能为空'
					})
					
					return
				}
				
				if(this.currExitproject=='请选择项目'||this.currExitbuild=='请选择楼栋'||this.currExithouse=='请选择房间'){
					uni.showToast({
						icon:'none',
						title:'请选择项目楼栋配置'
					})
					
					return
				}
				
				var data = {					  
					   pointCode: this.nfcId,
					   pointname:this.pointname,
					   pcode:this.currExitproject.slice(this.currExitproject.indexOf('|')+1),
					   bcode:this.currExitbuild.slice(this.currExitbuild.indexOf('|')+1),
					   hcode:this.currExithouse.slice(this.currExithouse.indexOf('|')+1)
					}						
				this.$api.PointAdd(data).then(res=>{											
					if(!res.isSuccess){
							uni.showToast({
								icon: 'none',
								title: res.errMsg
							});
							return
						}
					uni.showToast({
						icon: 'none',
						title: res.msg
					});
					setTimeout(function() {
					         uni.navigateTo({
					             url:"../index/home"
					             })
					    }, 1500);	
															
				})
			},
			GoBackHome(){
				uni.navigateTo({
					url:"/pages/index/home"
				})
			},
			ChangeProject(e) {
			    this.currIndex = e.target.value;
			    this.currExitproject = this.exitDataproject[this.currIndex]
				// console.info("currIndex:", e.target.value+"==="+this.currExitproject)
				this.GetBuildList()
			},
			ChangeBuild(e){
				this.currIndex = e.target.value;
				this.currExitbuild = this.exitDatabuild[this.currIndex]
				console.info("currIndex:", e.target.value)
				this.GetHouseList()
			},			
			ChangeHouse(e){
				this.currIndex = e.target.value;
				this.currExithouse = this.exitDatahouse[this.currIndex]
				console.info("currIndex:", e.target.value)
			},
		    GetProjectList(){				
				this.$api.GetProjectList({}).then(res=>{										
					if (res.length !=undefined &&res.length>0) {
						var project=[]
						res.filter(function (item, i) {  
						    project.push(item.pname+"|"+item.pcode)        
						})	
												
					    this.exitDataproject= project								
																									 
						}else{															 
					}				
				})
			},
			
			GetBuildList(){	
				var data ={
					 value:this.currExitproject.slice(this.currExitproject.indexOf('|')+1)
					}
				this.$api.GetBuildList(data).then(res=>{										
					if (res.length !=undefined &&res.length>0) {
						var build=[]
						res.filter(function (item, i) {
							build.push(item.bname+"|"+item.bcode)  
						})
						this.exitDatabuild = build								
																									 
						}else{															 
					}				
				})
			},
			GetHouseList(){	
				var data = {
						  value:this.currExitbuild.slice(this.currExitbuild.indexOf('|')+1)
						}
				this.$api.GetHouseListByBcode(data).then(res=>{											
					if (res.length !=undefined &&res.length>0) {
						var house=[]
						res.filter(function (item, i) {
						    house.push(item.hname+"|"+item.hcode)        
						})
						this.exitDatahouse = house												
																									 
						}else{															 
					}				
				})
			},
			
		}
		
	}
</script>

<style lang="scss">	
.checkout{
	padding: 30rpx;
	.select_box{
		height: 35px;	
		line-height: 35px;
		border: 1px solid #d6dbe5;
		border-radius: 6rpx;
		font-size: 30rpx;
		text-align: center;
		margin-bottom: 10rpx;
	}
}
</style>