<template>
	<view class="container">
		<search @search="headerSearch" />
		<view v-if="pageType">
			<swiper class="banner" indicator-dots autoplay circular>
				<swiper-item v-for="(item, index) in bannerList" :key="index">
					<image class="banner_item" :src="item.pic" mode="" />
				</swiper-item>
			</swiper>
		</view>
		<view class="main">
			<tabularCard
				v-for="(item,index) in list"
				:key="index"
				:item="item"
				:type="pageType"
				:isLast="index === list.length -1"
				:joinType="joinType"
				@chooseCommunity="chooseCommunity"
			/>
		</view>

	</view>
</template>

<script>
	import { request, picUrl, uniqueByField } from '../../../utils';
	import { apiArr } from '../../../api/community';
	export default {
		data() {
			return {
				pageType: 2,
				page_num: 1,
				page_size: 10,
				list: [],
				bannerList: [{
						pic: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com/community/_assets/Group_62.png'
					},
					{
						pic: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com/community/_assets/Group_62.png'
					},
					{
						pic: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com/community/_assets/Group_62.png'
					},
				],
				blurdesc: '', // 模糊搜索输入值
				flag: false,
				choose: true,
				joinType: "2"
			}
		},
		onLoad(options) {
			console.log('1231', options);
			// 基于父页面传递参数设置页面名称
			uni.setNavigationBarTitle({
				title: options.title || '社区列表'
			})

			this.pageType = options?.type || '';
			this.joinType = options?.joinType || '2'

			// 仅个人中心页点击申请业主时才携带types  === 2
			if(options?.types == 2){
				this.init(options?.type || '');
			}else{
				this.getUserCommunity()
			}
		},


		methods: {
			
			// 筛选指定状态订单
			headerSearch(item) {
				console.log('1', item);
				let value = item.detail.__args__[0].value;
				console.log('12112313221', value)
				this.blurdesc = value;
				this.page_num = 1;
				this.flag = false,
				this.list = [],
				this.init();
			},
			
			
			async init(type) {
				console.log('12313', this);
				const {
					list,
					page_num,
					page_size,
					blurdesc
				} = this;

				let url;
				//  共享空间 与 社区列表公共此页面， type 参数存在则该页面为共享空间
				if (type) {
					url = 'bianming'
				} else {
					url = apiArr.list
				}
				const res = await request(url, 'POST', {
					page_num,
					page_size,
					name: blurdesc || ''
				});
				console.log('11231321', res);

				let flag = false;
				if (res?.rows && res?.rows?.length == this.page_size) {
					flag = true
				} else {
					flag = false
				}
				res?.rows && res?.rows.forEach(item => {
					item.pic = picUrl + item.pic
				});
				console.log('变更后的', res);

				this.list = list.concat(res.rows || []),
				this.page_num = this.page_num + 1;
				this.flag = flag;
			},
			
			async getUserCommunity() {
				const { page_num, page_size, blurdesc } = this;
				let param = {
					page_num,
					page_size,
				};
				if (blurdesc !== '') {
					param = {
						page_num,
						page_size,
						name: blurdesc,
					};
				}
				const res = await request(apiArr.getUserCommunity, 'POST', param);
				console.log('reeee', res);
		
				
				let communityList = []
				if(res?.rows){
				  communityList = uniqueByField(res.rows,'community_id')
				}
				communityList  = communityList.map( item => item.community)
				if(communityList.length == 0){
				    this.init(this.pageType || '');
				}else{
					communityList.forEach(item => {
						item.pic = picUrl + item.pic
					});
					console.log('变更后的', communityList);
					this.list = communityList;
				}

				
			},
			
			
			chooseCommunity(e){
				console.log(e,'eeeee');
				return;
				// let community_id = e.detail
		
				const pages = getCurrentPages();
				if (pages.length < 2) return; // 没有上一页
				const prevPage = pages[pages.length - 2]; // 获取上一个页面实例
				// 调用上一个页面的方法（假设方法名为updateData）
				if (prevPage.updateData) {
					prevPage.updateData(e.detail);
				}
			},    
		},
		
		onReachBottom() {
			if (this.flag) {
				this.init(this.pageType);
			};
		},
	}
</script>

<style>
	@import url("./index.css");
</style>