<template>
	<view class="container">
		<view class="page">
			<view class="search">
				<uni-search-bar placeholder="请输入您的小区名" bgColor="#EEEEEE" @confirm="search" @cancel="cancel" />
				<view class="list">
					<view class="item" :class="{'active': item.communityCode === currentHouseCode}" @click="onChangeHouse(item)" v-for="item in houseList" :key="item.communityCode">
						{{item.communityName}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getCompanyListApi } from '@/config/api.js'
	let houseAllList = []
	export default {
		data() {
			return {
				houseList: [],
				currentHouseCode: ''
			}
		},
		methods: {
			async getList() {
				let {result} = await getCompanyListApi()
				houseAllList = result
				this.houseList = [...result]
			},
			onChangeHouse(item) {
				getApp().globalData.registerInfo = {}
				// if(item.communityCode === this.currentHouseCode) return
				uni.setStorageSync('user_type', '1')
				uni.setStorageSync('property_code', item.platformCode)
				uni.setStorageSync('property_name', item.platformName)
				uni.setStorageSync('platform_code', item.communityCode)
				uni.setStorageSync('platform_name', item.communityName)
				uni.reLaunch({
					url: '/pages/home/index'
				})
			},
			search(value) {
				this.houseList = houseAllList.filter(item=> {
					return item.communityName.includes(value.value)
				})
			},
			cancel() {
				this.houseList = [...houseAllList]
			}
		},
		onLoad() {
			this.currentHouseCode = uni.getStorageSync('platform_code') || ''
			let type = uni.getStorageSync('user_type')
			if(type === '2') {
				let data = uni.getStorageSync('user_house_list')
				houseAllList = JSON.parse(data)
				this.houseList = [...houseAllList]
			} else {
				this.getList()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding: 15rpx;
		background-color: #ffffff;
	}

	.search {
		.list {
			padding:20rpx 30rpx;
			.item {
				font-size: 28rpx;
				color: #373737;
				height: 84rpx;
				line-height: 84rpx;
				border-bottom: 1px solid #F8F6F9;
				&.active {
					color: #6A59F2;
				}
			}
		}
	}
</style>