<template>
	<view class="center">
		<view style="background: linear-gradient( 180deg, #1FCA7C 0%, #F2F2F2 100%);">
			<u-navbar bgColor="transparent" placeholder="true" @leftClick="onXz">
				<view style="display: flex;align-items: center;font-size: 32rpx;color: #fff;" slot="left">
					<image src="/static/icon_home_dw1.png" style="width: 30rpx;height: 30rpx;margin-right: 10rpx;"
						mode=""></image>
					<text>{{address}}</text>
					<image src="/static/icon_xsj.png" style="width: 30rpx;height: 8.5rpx;margin-left: 8rpx;"
						mode="widthFix"></image>
				</view>
			</u-navbar>
			<view class="h-300 flex flex-row-between v-center  ml-60 mr-35">
				<view class="flex-column" style="color: #FFFFFF;">
					<text class="fs-52" style="text-align: left;">便民服务</text>
					<text class="fs-30">洗衣清洁 | 超市送水~</text>
				</view>
				<image class="w-280 h-216" src="/static/icon_bmfw.png"></image>
			</view>
		</view>
		<view class="overflow-y mlr-25" style="background: white;border-radius: 20rpx;margin-top: -20rpx;">
			<scroll-view scroll-y style="height: 100%;">
				<view class="mlr-25" style="align-items: left;">
					<view class="flex">
						<text class="fs-32 fw-700 mt-30 mb-10">便民服务</text>
					</view>

					<view class="flex-column">
						<image @click="onClick(0)" class="item" src="/static/icon_bmfw_1.png"></image>
						<image @click="onClick(1)" class="item" src="/static/icon_bmfw_2.png"></image>
						<image @click="onClick(2)" class="item" src="/static/icon_bmfw_3.png"></image>
						<image @click="onClick(3)" class="item" src="/static/icon_bmfw_4.png"></image>
					</view>
				</view>
			</scroll-view>
			<u-popup :show="show" mode="center" @close="close" @open="open" round="20">
				<view class="flex-column mlr-50 ptb-30 vh-center" style="width:600rpx;">
					<text class="fs-32 fw-600 mtb-20">选择小区</text>
					<scroll-view scroll-y class="h-400">
						<view v-for="(item, index) in listXq" :key="index" @click="onSelect(index)">
							<view class="fs-24 pad-30" style="border-bottom: 1rpx solid #E9E9E9;">
								<text
									:style="selectIndex==index?'color: #1FCA7C;':'color: #000000;'">{{item.communityName}}</text>
							</view>
						</view>
					</scroll-view>
					<view @click="onSubmit()" class="w-250 h-60 lh-60 br-12 mt-20"
						style="background:#1FCA7C;color: #fff;">
						完成
					</view>
				</view>
			</u-popup>
		</view>



	</view>
</template>

<script>
	export default {
		data() {
			return {
				address: '请选择',
				userInfo: '',
				show: false,
				selectIndex: -1,
				listXq: [],
				communityId: '', //小区ID

			};
		},
		onLoad(options) {
			this.getUserInfo()
			this.getXqList()
			this.getHouseList()
		},
		methods: {
			onXz() {
				console.log(this.userInfo.userPermission)
				//非业主 弹出选择小区框
				if (parseInt(this.userInfo.userPermission) > 1) {
					this.open()
					return
				}
				//业主和家人 选择房屋信息
				if (parseInt(this.userInfo.userPermission) < 2) {
					this.$wskj.to("/pagesMine/pages/fwxx?type=1")
					return
				}
			},
			open() {
				this.show = true
			},
			close() {
				this.show = false
			},
			onSelect(index) {
				this.selectIndex = index
			},
			onSubmit() {
				if (this.selectIndex == -1) {
					this.$wskj.toast('请选择')
					return
				}
				this.show = false
				this.address = this.listXq[this.selectIndex].communityName
				uni.setStorageSync('addressItem', this.listXq[this.selectIndex])
			},
			onClick(inder) {
				this.$wskj.toast('敬请期待')
			},
			getUserInfo() {
				const params = {
					url: '/api/login/userInfo',
					method: 'GET',
					data: {}
				}
				this.$request(params).then(res => {
					uni.setStorageSync('userInfo', res.data)
					this.userInfo = res.data
					//用户类型（0=业主，1= 家庭成员，2=访客，3=保洁，4=保安）
					if (parseInt(this.userInfo.userPermission) < 2) {
						const fwInfo = uni.getStorageSync("fwInfo")
						this.address = fwInfo.communityName
					} else {
						//获取小区信息
						const addressItem = uni.getStorageSync("addressItem")
						this.address = addressItem.communityName
					}

				})
			},
			//获取小区数据
			getXqList() {
				const params = {
					url: '/api/community/listAll',
					method: 'GET',
					data: {}
				}
				this.$request(params).then(res => {
					console.log(res.data)
					this.listXq = res.data
				})
			},
			getHouseList() {
				uni.showLoading({
					title: ''
				})
				const than = this;
				const params = {
					url: '/api/community/myRoomListAll',
					method: 'GET',
				}
				than.$request(params).then(res => {
					if (than.$wskj.isNull(res.data)) {
						uni.hideLoading()
						return
					}
					than.houseList = res.data
					than.houseList.forEach(function(item, index) {
						if (item.room == than.userInfo.room) {
							than.selecteIndex = index
						}
					});
					//默认绑定房屋信息
					than.setHouse()
				})
			},
			setHouse() {
				const params = {
					url: '/api/community/cut/' + this.houseList[this.selecteIndex].communityId + "/" + this.houseList[
						this.selecteIndex].proprietorId,
				}
				this.$request(params).then(res => {
					uni.hideLoading()
					uni.setStorageSync('fwInfo', res.data)
					this.address = res.data.communityName
				})
			},

		}
	};
</script>

<style>
	.center {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: #f5f5f5;
	}

	.item {
		width: 100%;
		height: 200rpx;
		margin-top: 20rpx;
	}
</style>