<template>
	<view class="container">
		<view class="page" v-if="houseList.length">
			<view class="house">
				<view class="information" @click="toDetails(item.id)" v-for="item in houseList" :key="item.id">
					<view class="title" :class="item.color">{{item.platformName}}<view class="tag">{{item.statusName}}</view>
					</view>
					<view class="content">
						<view class="item">
							<view class="name">业主</view>
							<view class="detail">{{item.residentName}} {{item.residentPhone}}</view>
						</view>
						<view class="item">
							<view class="name">房屋状态</view>
							<view class="detail">自住</view>
						</view>
						<view class="item">
							<view class="name">房间号</view>
							<view class="detail">{{item.buildingName}}{{item.unitName}}{{item.roomName}}</view>
						</view>
						<view class="item">
							<view class="name">物业公司</view>
							<view class="detail">{{item.propertyName}}</view>
						</view>
						<view class="item">
							<view class="name">关系</view>
							<view class="detail">{{item.relationShipName}}</view>
						</view>
						<view class="item">
							<view class="name">认证状态</view>
							<view class="tag" :class="item.isAuth ? 'green' : 'grey'">{{item.isAuth ? '已认证' : '未认证'}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<no-data :show="houseList.length === 0" text="暂无房屋数据"></no-data>
		<view class="form-submit-fixed" v-if="!hideAdd">
			<view class="form-submit" @click="toEditor()">添加房屋</view>
		</view>
	</view>
</template>

<script>
	import { getHouseListApi } from '@/config/api.js'
	import {relationShipList} from '@/utils/dictionary'
	import noData from '@/components/no-data/no-data'
	let statusList = {
		waitAudit: { name: '审核中', color: 'yellow' },
		refuse: { name: '审核失败', color: 'red' },
		auditPass: { name: '审核成功', color: 'green' }
	}
	export default {
		components: {noData},
		data() {
			return {
				pageNumber: 1,
				houseList: [],
				isFinish: false
			}
		},
		computed: {
			hideAdd() {
				return this.houseList.some(item=>item.auditStatus !== 'auditPass')
			}
		},
		methods: {
			async getList(flag) {
				let {result} = await getHouseListApi({
					pageNo: this.pageNumber,
					pageSize: 10
				})
				let data = result.records.map(item=> {
					let relationShipObj = relationShipList.find(row=>row.value === item.relationShip)
					return {
						...item,
						statusName: statusList[item.auditStatus].name,
						color: statusList[item.auditStatus].color,
						relationShipName: relationShipObj ? relationShipObj.label : ''
					}
				})
				if(flag) {
					uni.stopPullDownRefresh();
					this.houseList = [].concat(data)
				} else {
					this.houseList = this.houseList.concat(data)
				}
				if(data.length < 10) {
					this.isFinish = true
				} else {
					this.pageNumber += 1
				}
			},
			toEditor() {
				uni.navigateTo({
					url: '/pages/housing/index',
				});
			},
			toDetails(id) {
				uni.navigateTo({
					url: '/pages/myHouseDetails/index?id=' + id
				});
			}
		},
		onLoad() {
			this.getList()
		},
		onPullDownRefresh() {
			this.pageNumber = 1
			this.getList(true)
		},
		onReachBottom() {
			if(this.isFinish) return
			this.getList()
		}
	}
</script>
<style>
	page {
		background-color: #F8F6F9;
	}
</style>
<style lang="scss" scoped>
	.page {
		padding: 30rpx 30rpx 90rpx;
		background-color: #F8F6F9;
	}

	.house {


		.information {
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.title {
				height: 40rpx;
				line-height: 40rpx;
				padding-left: 20rpx;
				font-size: 32rpx;
				color: #373737;
				font-weight: bold;
				margin-bottom: 30rpx;

				.tag {
					width: 136rpx;
					text-align: center;
					height: 36rpx;
					line-height: 36rpx;
					font-size: 22rpx;
					border-radius: 30rpx;
					float: right;
				}
			}

			.red {
				border-left: 2px solid #F42E2E;

				.tag {
					color: #F42E2E;
					border: 1px solid #F42E2E;
					background-color: #FFF5F5;
				}
			}

			.green {
				border-left: 2px solid #10D680;

				.tag {
					color: #10D680;
					border: 1px solid #10D680;
					background-color: #F1FFF9;
				}
			}

			.gray {
				border-left: 2px solid #B6B6BA;

				.tag {
					color: #B6B6BA;
					border: 1px solid #B6B6BA;
					background-color: #F8F8F8;
				}
			}

			.yellow {
				border-left: 2px solid #F1BE51;

				.tag {
					color: #F1BE51;
					border: 1px solid #F1BE51;
					background-color: #FFFCF6;
				}
			}

			.content {
				border-top: 1px solid #F0F0F2;
				padding-top: 20rpx;

				.item {
					display: flex;
					height: 70rpx;
					line-height: 70rpx;

					.name {
						width: 35%;
						font-size: 28rpx;
						color: #CECED3;
					}

					.detail {
						font-size: 28rpx;
						color: #373737;
					}

					.tag {
						width: 130rpx;
						text-align: center;
						height: 36rpx;
						line-height: 36rpx;
						font-size: 22rpx;
						border-radius: 30rpx;
						margin-top: 17rpx;
						&.green {
							color: #10D680;
							border: 1px solid #10D680;
							background-color: #F1FFF9;
						}
						&.grey {
							color: #CDD0D6;
							border: 1px solid #CDD0D6;
							background-color: #FFFCF6;
						}
					}
				}
			}

		}
	}

	.form-submit-fixed {
		position: fixed;
		width: 100%;
		bottom: 10rpx;
	}

	.form-submit {
		margin: 0 30rpx;
		box-sizing: border-box;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #6A59F2;
		border-radius: 80rpx;
		text-align: center;
		font-size: 28rpx;
		color: #FFFFFF;
	}
</style>