<template>
	<view class="container">
		<label class="sel_event" @click="sel_show()">
			<view class="sel_point">
				<view class="sel_field" v-if="sel_item1 != ''">
					{{sel_item1 + "/" + sel_item2 + "/" + (sel_item3.name || sel_item3.label)}}
				</view>
				<view class="triangle" :style="tri_style">
				</view>
			</view>
		</label>
		<u-select v-model="show" value-name="id" label-name="name" mode="mutil-column-auto" :list="list"
			@confirm="confirm"></u-select>
		<view class="content">
			<view class="table_hed">
				<view class="table_cell_h">
					<span>名称</span>
					<span>编号</span>
					<span>类型</span>
					<span>关联项目数</span>
				</view>
			</view>
			<scroll-view  :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="lower">
				<view class="table_cell" v-for="( item, i) in tabArr" :key="item.id">
					<span>{{item.name}}</span>
					<span>{{item.number}}</span>
					<span>{{item.type == 1 ? '机柜' : '其他'}}</span>
					<span>{{item.checkItemTotal}}</span>
				</view>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	import {
		building,
		floor,
		room,
		device
	} from '@/api/index.js'
	export default {
		data() {
			return {
				sel_item1: '', //选择列1值
				sel_item2: '', //选择列2值
				sel_item3: {}, //选择列3值
				tri_style: '', //小三角样式
				show: false, //选择框弹出
				tabArr: [], //设备列表
				list: [], //选择框列表
				scrollnum: 2 //页码（懒加载）
			}
		},
		onLoad() {
			this.getBuilding()
		},
		watch: {
			sel_item3: {
				deep: true,
				handler(newOption, oldOption) {
					if (newOption.value) {
						this.getTotleDevice(newOption.value);
					} else {
						this.getTotleDevice(newOption.id);
					}

				},
			},
			show(newOption, oldOption) {
				if (!newOption) {
					this.tri_style =
						'margin-top: 10rpx;margin-bottom:0rpx;border-top-color:#808080;border-bottom-color:transparent;';
				}
			}

		},
		methods: {
			//选择框弹出
			sel_show() {
				this.show = !this.show;
				this.tri_style =
					'margin-top: 0rpx;margin-bottom:10rpx;border-top-color:transparent;border-bottom-color: #808080;';
			},
			//获取楼栋
			async getBuilding() {
				let {
					data
				} = await building();
				this.list.length = 0;
				data.forEach(i => {
					this.list.push(i)
				});
				for (let i = 0, len = this.list.length; i < len; i++) {
					await this.getFloor(this.list[i].id, i)
					for (let j = 0, len = this.list[i].children.length; j < len; j++) {
						await this.getRoom(this.list[i].children[j].id, i, j)
					}
				}
				//选择框默认值
				this.$nextTick(function() {
					this.sel_item1 = this.list[0].name;
					this.sel_item2 = this.list[0].children[0].name;
					this.sel_item3 = this.list[0].children[0].children[0];
				})
			},
			//获取楼层
			async getFloor(buildingId, i) {
				let {
					data
				} = await floor(buildingId)
				this.list[i].children = [];
				data.forEach(ite => {
					this.list[i].children.push(ite)
				})
			},
			//获取房间
			async getRoom(roomId, i, j) {
				let {
					data
				} = await room(roomId)
				this.list[i].children[j].children = [];
				data.forEach(ite => {
					this.list[i].children[j].children.push(ite)
				})
			},
			//获取房间所有设备
			async getTotleDevice(roomId, page, size) {
				page || (page = 1)
				size || (size = 10)
				let {
					data
				} = await device({
					roomId,
					page,
					size
				})
				this.tabArr = data.list;
			},
			// 选择框确认（回调参数为包含多个元素的数组，每个元素分别反应每一列的选择情况）
			confirm(e) {
				this.sel_item1 = e[0].label;
				this.sel_item2 = e[1].label;
				this.sel_item3 = e[2]
				this.getTotleDevice(e[2].value);
			},
			//懒加载
			lower() {
				this.getTotleDevice(this.sel_item3, this.scrollnum, 10);
				this.scrollnum += 1;
			},

		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100%;
		background-color: #FFFFFF;
	}

	.sel_point {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		width: 690rpx;
		height: 30rpx;
		margin: 29rpx 0 38rpx 0;
		text-align: left;
	}

	.sel_field {
		margin-right: 30rpx;
		font-size: 28rpx;
		font-family: Microsoft YaHei-3970(82674968);
		font-weight: 400;
		color: #333333;
		text-indent: 10rpx;
	}

	.triangle {
		margin-top: 10rpx;
		display: inline-block;
		/* Base Style */
		border: solid 10rpx transparent;
		border-top-color: #808080;
	}

	.content {
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: center;
		width: 690rpx;
		height: 100%;
		background: #FFFFFF;
		box-shadow: 0px 0px 24px 0px rgba(6, 0, 1, 0.1);
		border-radius: 10px 10px 0px 0px;
		overflow: hidden;

		.table_hed {
			width: 690rpx;
			height: 70rpx;
			background: #EBF8FF;
		}

		.table_cell,
		.table_cell_h {
			display: flex;
			justify-content: space-around;
			align-items: center;
			width: 630rpx;
			height: 70rpx;
			margin: 0 auto;
			border-bottom: solid 1rpx #E6E6E6;

			span {
				width: 140rpx;
				font-size: 28rpx;
				font-family: Microsoft YaHei-3970(82674968);
				font-weight: 400;
				color: #333333;
				text-align: center;
			}
		}

		.table_cell span {
			max-width: 140rpx;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
		}

		.table_cell_h {
			border-bottom: none;
		}

		.scroll-Y {
			height: calc( 100vh - 180rpx );
			width: 690rpx;
			
		}
	}
</style>
