<template>
	<view class="container">
	    <view class="header" v-if="type == '1'">
	        <view class="title">社区楼宇信息</view>
	        <view class="item" v-for="(item, index) in facList" :key="index" :data-id="item.facility_id" :data-name="item.name" @click="chooseFac">
	            <view class="left">
	                {{item.name}}
	            </view>
	        </view>
	    </view>
	
	
	    <view class="header" v-if="type == '2'">
	        <view class="title">房间列表</view>
	        <view class="item" v-for="(item, index ) in RoomList" :key="index" :data-id="item.room_id" :data-name="item.name" @click="chooseRoom">
	            <view class="left">
	                {{item.name}}
	                <!-- {{item.location}} -->
	            </view>
	        </view>
	    </view>
	</view>
</template>

<script>
	import { request, picUrl, NavgateTo } from '../../utils';
	import { apiArr } from '../../api/community';
	export default {
		data() {
			return {
				type:"",
				communityId:"",
				facList:[],
		
				flag:false,
				page_num:1,
				page_size:10,
				RoomList:[],
				FacId:"",
			}
		},
		methods: {
			async getFacList(id){
				const res = await request(apiArr.getFacilityList, 'POST', {
					community_id:Number(id)
				});
				console.log('12313131', res);
				this.facList = res?.rows || [];
			},
			
			async getRoomList(id, facId){
				const res = await request(apiArr.getRoomList, "POST", {
					facility_id: Number(facId),
					community_id: Number(id),
					page_num: this.page_num,
					page_size: this.page_size
				})
				console.log('getRoomList res', res);
				
				if(res?.rows){
					let flag = false
					if(res.rows.length == this.page_size){
						flag = true
					}else{
						flag = false
					}
					
					this.flag = flag;
					this.RoomList = this.RoomList.concat(res?.rows || []);
					this.page_num = this.page_num + 1;
				}
			},
		
			
			// 选择楼宇号
			chooseFac(e){
				let that= this
				let FacName = e.currentTarget.dataset.name
				let FacId = e.currentTarget.dataset.id
				uni.setStorageSync('FacName', FacName)
				uni.setStorageSync('FacId', FacId)
				// 返回上一页
				uni.navigateBack({
					delta: 1
				});
			},
			
			
			// 选择房间号
			chooseRoom(e){
				let that= this
				let RoomName = e.currentTarget.dataset.name
				let RoomId = e.currentTarget.dataset.id
				uni.setStorageSync('RoomName', RoomName)
				uni.setStorageSync('RoomId', RoomId)
				uni.navigateBack({
					delta: 1
				});
			},
			
		},
	
		onLoad(options){
			this.communityId = options.id;
			this.type = options.type;
			this.FacId = options.FacId;

			if(options.type == '1'){
				this.getFacList(options.id)
			}else{
				this.getRoomList(options.id, options.FacId)
			}
		},
		
		onReachBottom() {
			if(this.flag){
				that.getRoomList()
			}
		},
	}
</script>

<style>
	@import url("./index.css");
</style>
