<template>
	<view class="center">
		<view>
			<u-navbar bgColor="#1FCA7C" 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>
		<view style="background: linear-gradient( 180deg, #1FCA7C 0%, #F2F2F2 100%);height: 800rpx;"></view>
		<view class="mlr-24" style="margin-top: -800rpx;">
			<u-swiper :list="bannerList" radius="6"></u-swiper>
		</view>
		<view class="mtb-20 mlr-24 br-12" style="background-color: white;">
			<view class="mar-20 fs-32 fw-600" style="text-align: left;">常用服务</view>
			<view>
				<view class="grid-container">
					<view @click="onClick(index)" class="grid-item" v-for="(item, index) in listService" :key="index">
						<image class="w-86 h-86" :src="item.img"></image>
						<text class="fs-32 fw-600 mtb-20">{{item.title}}</text>
						<text class="fs-24" style="color: #999999;">{{item.describe}}</text>
					</view>
				</view>
			</view>
		</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>
		<!-- <u-popup :show="showHh" mode="center" @close="closeHh" @open="openHh" 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 listHh" :key="index" @click="onSelectHh(index)">
						<view class="fs-24 pad-30" style="border-bottom: 1rpx solid #E9E9E9;">
							<text :style="selectHhIndex==index?'color: #1FCA7C;':'color: #000000;'">{{item.room}}</text>
						</view>
					</view>
				</scroll-view>
				<view @click="onSubmitHh()" class="w-250 h-60 lh-60 br-12 mt-20"
					style="background:#1FCA7C;color: #fff;">
					完成
				</view>
			</view>
		</u-popup> -->
		<!-- <view class="v-top-popup">
			<u-popup :show="show" @close="close" @open="open" mode="top" :style="{ top: 100 + 'px' }">
				<view class="flex-column">
					<text class="fs-24 mtb-20 text-left ml-30">当前小区：{{address}}</text>
					<scroll-view scroll-y>
						<view v-for="(item, index) in listXq" :key="index" @click="onSelect(index)">
							<view class="fs-24 pad-30 text-left" style="border-bottom: 1rpx solid #E9E9E9;">
								<text>{{item.communityName}}</text>
							</view>
						</view>
					</scroll-view>
				</view>
			</u-popup>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//选择小区用到的字段
				address: '请选择',
				show: false,
				selectIndex: -1,
				listXq: [],
				communityId: '', //小区ID
				//选择小区户号用到的字段
				showHh: false,
				selectHhIndex: -1,
				listHh: [],
				proprietorId: '', //户号id
				userInfo: '',
				selecteIndex: 0, //绑定房屋信息
				houseList: [],
				bannerList: [],
				list1: [
					"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2023%2F0826%2F07b46817j00rzyq1h000kd000hs00alp.jpg&thumbnail=660x2147483647&quality=80&type=jpg",
					"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2023%2F0826%2F07b46817j00rzyq1h000kd000hs00alp.jpg&thumbnail=660x2147483647&quality=80&type=jpg",
					"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2023%2F0826%2F07b46817j00rzyq1h000kd000hs00alp.jpg&thumbnail=660x2147483647&quality=80&type=jpg",
				],
				listService: [{
						img: "/static/icon_home_1.png",
						title: "访客",
						describe: "请点击输入访客信息"
					},
					{
						img: "/static/icon_home_2.png",
						title: "报修报事",
						describe: "保修报事一键即发"
					},
					{
						img: "/static/icon_home_3.png",
						title: "生活缴费",
						describe: "水费电费在线缴纳"
					},
					{
						img: "/static/icon_home_4.png",
						title: "常用电话",
						describe: "随时查找常用电话"
					},
					{
						img: "/static/icon_home_5.png",
						title: "停车缴费",
						describe: "输入车牌在线缴费"
					},
					{
						img: "/static/icon_home_6.png",
						title: "投诉表扬",
						describe: "投诉表扬线上发布"
					}

				],
			};
		},
		onShow() {
			this.getUserInfo()
			this.getXqList()
			this.getHouseList()
			this.getBannerList()
		},
		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])
			},
			//获取小区数据
			getXqList() {
				const params = {
					url: '/api/community/listAll',
					method: 'GET',
					data: {}
				}
				this.$request(params).then(res => {
					console.log(res.data)
					this.listXq = res.data
				})
			},
			//轮播图列表
			getBannerList() {
				const than = this;
				const params = {
					url: '/api/slideshow/list?type=1',
					method: 'GET',
					data: {}
				}
				this.$request(params).then(res => {
					than.bannerList = [];
					res.data.forEach(function(item, index) {
						than.bannerList.push(item.url)
					});
				})
			},
			openHh() {
				this.showHh = true
			},
			closeHh() {
				this.showHh = false
			},
			onSelectHh(index) {
				this.selectHhIndex = index

			},
			onSubmitHh() {
				this.showHh = false
				this.proprietorId = this.listHh[this.selectHhIndex].proprietorId
				this.upHh()
			},
			//获取户号数据
			getHhList() {
				const params = {
					url: '/api/community/room/listAllByCommunityId/' + this.communityId,
					method: 'GET',
					data: {}
				}
				this.$request(params).then(res => {
					console.log(res.data)
					this.listHh = res.data
					//打开户号选择的弹框
					this.openHh()
				})
			},
			upHh() {
				const params = {
					url: '/api/community/cut/' + this.communityId + "/" + this.proprietorId,
					data: {}
				}
				this.$request(params).then(res => {
					this.$wskj.toast("设置成功")
					this.userInfo()
				})
			},
			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
					}
				})
			},
			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
				})
			},

			onClick(index) {
				//访客
				if (index == 0) {
					this.$wskj.to("/pagesMain/pages/fkdj")
					return
				}

				if (parseInt(this.userInfo.userPermission) > 1) {
					this.$wskj.showModal('提示', '只有业主以及其家人可使用该功能', false, null);
					return
				}
				if (parseInt(this.userInfo.userPermission) < 2 && this.$wskj.isNull(uni.getStorageSync("fwInfo"))) {
					this.$wskj.showModal('提示', '请先选择房屋信息', true, (confirmed) => {
						if (confirmed) {
							this.$wskj.to("/pagesMine/pages/fwxx?type=1")
						}
					});
					return
				}
				//报修报事
				if (index == 1) {
					this.$wskj.to("/pagesMain/pages/bxbs")
					return
				}
				//生活缴费
				if (index == 2) {
					this.$wskj.to("/pagesMain/pages/shjf")
					return
				}
				//常用电话
				if (index == 3) {
					this.$wskj.to("/pagesMain/pages/cydh")
					return
				}
				//停车缴费
				if (index == 4) {
					this.$wskj.toast("敬请期待")
					return
				}
				//投诉表扬
				if (index == 5) {
					this.$wskj.to("/pagesMain/pages/tsby")
					return
				}
			}
		}
	};
</script>

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

	.grid-container {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around; // 可以根据需求调整元素的水平排列方式，这里是均匀分布
		align-items: center; // 垂直方向居中对齐
	}

	.grid-item {
		width: 49%;
		height: 300rpx; // 宫格高度
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		border-bottom: 1rpx solid #f5f5f5;
		border-right: 1rpx solid #f5f5f5;
	}

	.v-top-popup /deep/ .u-popup__content {
		border-radius: 0 0 24rpx 24rpx;
		margin-top: 20%;
	}
</style>