<template>
	<view class="login-container">
		<view class="title-back">
			<view class="left-father" @click="goBack">
				<image class="back-img" src="https://www.focusnu.com/media/directive/index/left.png" />
				<view style="font-size: 30rpx;">机构信息</view>
			</view>
		</view>
		<view class="index-up">
			<image class="index-up-img" src="https://www.focusnu.com/media/directive/index/indexgif.gif" mode="widthFix"
				lazy-load="false" />
		</view>
		
		<view class="bgc-card">
			<!-- <view class="title-card">
				<view class="big-weight">机构加盟</view>
				<view class="title-other">
					加盟我们，共享银发经济红利!依托成熟运营体系，标准化服务流程降低人力成本及管理开支，背靠品牌资源，助力企业快速实现营收增长。
				</view>
			</view> -->
			<view class="none" v-if="!workArray.length">
				<image style="width: 300rpx;height: 300rpx;" src="https://www.focusnu.com/media/directive/index/none.png" mode="widthFix"
					lazy-load="false" />
					<view class="">暂无机构信息</view>
				
			</view>
			<view class="under-scroll">
				<view v-for="(item,index) in workArray" :key="index">
					<view class="white-small" @click="jumpToAll(item)">
						<view style="font-size: 32rpx;">
							{{item.comName}}
						</view>
						<view class="bottom-father">
							<view style="margin-top: 45rpx;">
								<view class="">
									<text style="color: #999;"> {{item.isend?`申请`: `变更` }}日期：</text>{{item.commitTime}}
								</view>
								<view style="margin-top: 10rpx;">
									<text style="color: #999;"> 审核日期：</text>{{item.auditTime}}
								</view>
							</view>
							<view class=""></view>
							<image class="bottom-img" :src="`https://www.focusnu.com/media/directive/index/${statusarray[(Number(item.status))]}.png`" />
						</view>
						<!-- <image class="enter-img" src="https://www.focusnu.com/media/directive/index/right.png" /> -->
						<!-- <view class="bottom-father">
							<view style="font-weight: 600;margin-top: 5rpx;" :style="item.status==`5`?{color:`red`}:{}">
								{{ item.status ? namearraay[Number(item.status)] :"" }}
							</view>
					
						</view> -->
						<view class="blue-button" v-if="item.modifyStatus===`0`&&item.ischange" @click.stop="again(item)">
							<image class="blue-button-img" src="https://www.focusnu.com/media/directive/change.png" mode="widthFix"
								lazy-load="false" />
						</view>
						<!-- <view style="height: 30rpx;">
							
						</view> -->
					</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script setup>
	import {
		reactive,
		ref,
		nextTick
	} from 'vue';
	import {
		onLoad,
		onPullDownRefresh
	} from '@dcloudio/uni-app'
	import {
		getMessageListsuccess
	} from '@/pages/addjigou/api/addjigou.js'
	const type = ref(0)
	const workArray = ref([])
	const special = ref(false);
	const statusarray =  ["success","loading","success","fail","loading","fail"]
	const namearraay = ["审核通过","审核中","审核通过","审核不通过","审核中","审核不通过"]
	
	const getData = () => {
		uni.getStorage({
			key: 'openid',
			success: function(res) {
				getMessageListsuccess(res.data)
					.then(res => {
						// 1. 统计各 pkId 出现次数
						const counts = {}
						res.result.forEach(item => {
							counts[item.pkId] = (counts[item.pkId] || 0) + 1
						})
				
						// 2. 遍历时分别处理“第一个”标记(ischange)和“最后一个”标记(isend)
						const seenFirst = {} // 记录哪些重复 pkId 已经标记过 ischange = true
						const seenCount = {} // 记录每个 pkId 已经遍历到第几条
				
						workArray.value = res.result.map(item => {
							const {
								pkId
							} = item
				
							// —— ischange 逻辑 —— 
							if (counts[pkId] > 1) {
								// 重复组：第一个设 true，后续设 false
								if (!seenFirst[pkId]) {
									item.ischange = true
									seenFirst[pkId] = true
								} else {
									item.ischange = false
								}
							} else {
								// 只出现一次，也设 true
								item.ischange = true
							}
				
							// —— isend 逻辑 —— 
							// 先累加当前是该 pkId 的第几条
							seenCount[pkId] = (seenCount[pkId] || 0) + 1
							// 如果当前序号等于总次数，则是最后一条
							item.isend = (seenCount[pkId] === counts[pkId])
				
							return item
						})
					})
					.catch(err => {
						console.error(err)
					})

			}
		});
	}
	
	// 页面加载时接收 URL 参数
	onLoad((options) => {
		getData()
	})

	onPullDownRefresh(() => {
		getData()
		uni.stopPullDownRefresh()
	})

	const goBack = () => {
		uni.navigateBack()
	}

	const again = (item) => {
		let trans = item
		trans.id = null
		uni.setStorageSync("baddata", trans)
		uni.setStorageSync('specicalid', trans.id);
		uni.setStorageSync("backhuancun", {})
		uni.navigateTo({
			url: `/pages/addjigou/name`
		});
	}

	const jumpToAll = (element) => {
		// console.log("?????",element)
		uni.navigateTo({
			url: `/pages/addjigou/all?element=${JSON.stringify(element)}&special=1`
		});
	}
	
</script>

<style lang="scss" scoped>
	.login-container {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		width: 100%;
		background-color: rgb(239, 241, 252);
		position: relative;

	}

	.under-scroll {
		width: 100%;
		height: calc(100% - 460rpx);
		margin-top: 30rpx;
		.white-small {
			width: 94%;
			margin-left: 3%;
			background-color: #fff;
			border-radius: 30rpx;
			// padding: 40rpx;
			padding: 30rpx 40rpx;
			margin-bottom: 30rpx;
			font-size: 25rpx;
			// color: #999999;
			position: relative;
		}
	}

	.button-heng {
		// width: 100%;
		display: flex;
		justify-content: flex-end;
		position: absolute;
		bottom: 30rpx;
		right: 0;

		.white-button {
			width: 180rpx;
			height: 60rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: linear-gradient(to bottom, #F3F3F5, #DEE4E9);
			border-radius: 30rpx;
			color: black;
			margin-right: 20rpx;
		}

		
	}
	.bgc-card{
		margin-top: 450rpx;
		background-color: #F7F7F7;
		width: 98%;
		margin-left: 1%;
		border-top-left-radius: 30rpx;
		border-top-right-radius: 30rpx;
		min-height: calc(100vh - 650rpx);
		z-index: 1;
		position: relative;
	}
	.index-up {
		position: absolute;
		top: 0rpx;
		left: 0;
		width: 100%;
	
		.index-up-img {
			width: 100%;
			height: 20rpx;
		}
	}
	.title-card{
		margin: 30rpx;
		background-color: #fff;
		width: 93%;
		height: 250rpx;
		border-radius: 30rpx;
		padding: 0 45rpx;
		margin-top: 40rpx;
		padding-top: 40rpx;
		.big-weight{
			font-size: 30rpx;
			color: #333333;
			font-weight: 600;
			margin-bottom: 30rpx;
		}
		.title-other{
			color: #666666;
			font-size: 25rpx;
		}
	}
	.title-back {
		margin-top: 100rpx;
		width: 100%;
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		z-index: 1;
	}
	
	.left-father {
		display: flex;
		align-items: center;
		z-index: 1;
		.back-img {
			width: 45rpx;
			height: 40rpx;
			margin-left: 40rpx;
			margin-right: 15rpx;
		}
	}
	.enter-img{
		width: 25rpx;
		height: 25rpx;
		position: absolute;
		right: 30rpx;
		top: 35rpx;
	}
	.bottom-father{
		display: flex;
		justify-content: space-between;
	}
	.bottom-img{
		width: 140rpx;
		height: 120rpx;
	}
	.blue-button {
		margin-top: 40rpx;
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: rgb(240,240,240);
		color: #fff;
		font-size: 30rpx;
		.blue-button-img{
			width: 30rpx;
			height: 30rpx;
			margin-left: 5rpx;
		}
	}
	.none{
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%,-50%);
		display: flex;
		// justify-content: center;
		align-items: center;
		flex-direction: column;
		color: #999;
	}
</style>