<template>
	<div class="container">
		<u-modal v-model="show" :content="content"></u-modal>
		<u-popup v-model="popupshow" mode="bottom" border-radius="20" height="600px">
			<view class="popup-father">

				<view class="popup-title">
					<view style="display: flex; justify-content: center;align-items: center;height: 100rpx;width: 100%;position: relative;">
						<view class="title-font">
							请选择所在地区
						</view>
						<image class="title-imge" src="https://www.focusnu.com/media/directive/index/workjoin/x.png" @click="popupshow=false" />
					</view>
					<view style="height: 80rpx;width: 100%;display: flex; align-items: center;justify-content: space-between;position: relative;" v-for="(item,index) in address">
						<view style="margin-left: 60rpx;">
							{{item.name}}
						</view>
						<image class="title-imge" style="width: 25rpx;height: 25rpx;" src="https://www.focusnu.com/media/directive/index/workjoin/x.png" @click="deleteUP(index)" />
					</view>
				</view>
				<view style="height: 100rpx;width: 100%;">
					
				</view>
				<view style="height: 80rpx;width: 100%;" v-for="(item) in address">
					
				</view>
				<view class="other-title">
					选择地区
				</view>
				<view v-for="(item,index) in list" :key="index" @click="clickcard(item)">
					<view class="cards">
						<view class="zimu">
							{{item.firstLetter}}
						</view>
						<view class="font">
							{{item.name}}
						</view>
					</view>
					
				</view>
			</view>

		</u-popup>
		<view class="white-content">
			<view class="white-message">
				<view @click="openpopupshow">
					<view class="one">
						<view class="one-left">机构位置</view>
						<input disabled style="cursor: not-allowed;pointer-events: none;" class="one-right" type="text"
							placeholder="请选择机构位置" v-model="form.address" />
						<!-- <view class="special" @click="showselect = true"> -->
						<!-- <view class="one-right" style="width: 200rpx;justify-content: flex-start;font-size: 26rpx;"
								@click="openLook(jigouweizhi)">
								{{ jigouweizhi ? jigouweizhi : `请选择机构位置` }}
							</view>
							<u-select v-model="showselect" mode="mutil-column-auto" :list="list" label-name="name" value-name="id" child-name="children" @confirm="confirm" @cancel-text="showselect=false"></u-select> -->
						<!-- <image class="one-img" src="https://www.focusnu.com/media/directive/index/norelmap.png" @click="jumpToMap" /> -->
					</view>
				</view>
				<view>
					<view class="one" style="min-height: 150rpx;overflow: hidden;">
						<view class="one-left">详细地址</view>
						<textarea class="one-right-spec" placeholder="请输入详细地址" auto-height v-model="form.orgAddress" maxlength="60" />
					</view>
				</view>
				<view>
					<view class="one">
						<view class="one-left">机构负责人</view>
						<input class="one-right" type="text" placeholder="请输入机构负责人姓名" v-model="form.orgLeader" />
					</view>
				</view>
				<view>
					<view class="one">
						<view class="one-left">机构负责人电话</view>
						<input class="one-right" type="number" placeholder="请输入机构负责人电话" v-model="form.orgLeaderPhone" />
					</view>
				</view>
				
				<view>
					<view class="one">
						<view class="one-left">房屋性质</view>
						<input class="one-right" type="text" placeholder="请输入房屋性质" v-model="form.orgPropertyType" />
					</view>
				</view>
				<view style="margin-bottom: 20rpx;">
					<view class="one" style="position: relative;">
						<view class="one-left">建筑面积</view>
						<input class="one-right" type="number" placeholder="请输入建筑面积" v-model="form.orgBuildingArea" />
						<!-- <view class="pingfangmi">
							平方米
						</view> -->
					</view>
				</view>
			</view>
		</view>

		<view style="display: flex;width: 100%;margin-top: 10rpx;">
			<view class="finish-button" @click="goBack">
				上一步
			</view>
			<view class="finish-button" @click="next">
				确认并提交
			</view>
		</view>
	</div>
</template>

<script setup>
	import {
		ref,
		reactive,
	} from 'vue'
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app';

	import {
		base_url
	} from '@/request/index.js'

	import {
		changemessage,
		getdate
	} from './api/addjigou.js'

	const address = ref([])
	const show = ref(false);
	const content = ref("");
	const jigouweizhi = ref("");
	const showselect = ref(false);
	const popupshow = ref(false);
	const list = ref([]);

	const form = reactive({
		orgLeader: "",
		orgLeaderPhone: "",
		orgAddress: "",
		orgPropertyType: "",
		orgBuildingArea: "",
		orgAddress: "",
		orgCoordinateLo: "",
		orgCoordinateLa: "",
		id: uni.getStorageSync('specicalid') || "",
		tel: uni.getStorageSync('tel'),
		status:"1"
	})
	// 本地保存的临时文件路径
	const tempImagePath = ref('')

	const headImge = ref("");
	const backImge = ref("");

	const openLook = (res) => {
		if (res) {
			content.value = res;
			show.value = true
		}
	}
	//配置删除逻辑
	function takeFirst(arr, num) {
	  // 1. 安全裁剪：确保 num 在 [0, arr.length] 范围内
	  const count = Math.max(0, Math.min(num, arr.length));
	  // 2. slice(0, count) 会返回前 count 项，count=0 时即返回 []
	  return arr.slice(0, count);
	}
	const deleteUP = (index) => {
		address.value = takeFirst(address.value,index);
		 if(index){
			 clickcard(list.value[index - 1])
		 }else{
			 list.value = [];
			 getdate(`xzqhdm`).then(res => {
			 	list.value = dedupeFirstLetter(res.result)
			 })
		 }
		
		console.log("删除了",index)
	}
	const next = () => {
		const allNonEmpty = form.orgLeader && form.orgLeaderPhone && form.orgAddress && form.orgPropertyType &&
			form.orgBuildingArea
		if (allNonEmpty) {
			changemessage(form).then(res => {
				if (res.success) {
					if (res.message == `保存成功！`) {
						uni.setStorageSync('specicalid', "");
						uni.reLaunch({
							url: `/pages/login/threeselectone`
						});
					} else {
						uni.setStorageSync('specicalid', res.result.id);
						uni.reLaunch({
							url: `/pages/login/threeselectone`
						});
					}

				} else {
					uni.showToast({
						title: res.message,
						icon: 'error'
					})
				}
			})
		} else {
			uni.showToast({
				title: '请完善信息',
				icon: 'error'
			})
		}
	}

	const goBack = () => {
		uni.navigateBack()
	}
	const jumpToMap = () => {
		uni.navigateTo({
			url: "/pages/map/index"
		});
	}
	
	const clickcard = (element) => {
		if(address.value.length<3){
			list.value = [];
			address.value.push(element)
			getdate(element.id).then(res => {
				if(res.result.length){
					list.value = dedupeFirstLetter(res.result)
				}else{
					popupshow.value = false;
					form.address = "";
					address.value.forEach(res=>{
						form.address += res.name
					})
					const keys = ['orgProvince', 'orgCity', 'orgDistrict'];
					
					keys.forEach((key, idx) => {
					  // 如果 address.value[idx] 存在就取它的 id，否则用空字符串
					  form[key] = address.value[idx]?.id ? address.value[idx]?.id : "";
					});
					address.value = [];
					list.value = [];
					getdate(`xzqhdm`).then(res => {
						list.value = dedupeFirstLetter(res.result)
					})
				}
				
			})
		}
		
		
	}
	const openpopupshow = () =>{
		popupshow.value=true;
		
	}
	onShow(() => {
		// 读取并立即清除，避免下次重复拿到
		const data = uni.getStorageSync('dingwei')
		if (data) {
			console.log("data", data)
			jigouweizhi.value = data.name
			form.orgAddress = data.name
			form.orgCoordinateLo = data.lng
			form.orgCoordinateLa = data.lat
			uni.removeStorageSync('dingwei')
		}
	})
	//处理首字母
	function dedupeFirstLetter(arr) {
	  const seen = new Set();
	  return arr.map(item => {
	    const letter = item.firstLetter;
	    if (!letter || seen.has(letter)) {
	      // 如果没 letter 或者已经出现过，则清空
	      return { ...item, firstLetter: '' };
	    } else {
	      // 第一次出现，保留并记录
	      seen.add(letter);
	      return item;
	    }
	  });
	}
	onLoad(() => {
		getdate(`xzqhdm`).then(res => {
			list.value = dedupeFirstLetter(res.result)
		})
		if(uni.getStorageSync('baddata')){
			let data = uni.getStorageSync('baddata');
			// textArray[0] = data.comName;
			// textArray[1] = data.comRegisterAddress;
			// textArray[2] = data.comCreditCode;
			// textArray[3] = data.comLegalPerson;
			// headImge.value = `${base_url}/sys/common/static/${data.comBusinessLicense}`;
			// fontphoto.value = data.comBusinessLicense
			if(data.orgProvince_dictText){
				form.address = data.orgProvince_dictText + data.orgCity_dictText +data.orgDistrict_dictText;
			}
			
			// 'orgProvince', 'orgCity', 'orgDistrict'
			form.orgProvince = data.orgProvince;
			form.orgCity = data.orgCity;
			form.orgDistrict = data.orgDistrict;
			// textArray1[0] = data.orgProvince_dictText + data.orgCity_dictText +data.orgDistrict_dictText;
			form.orgAddress = data.orgAddress;
			form.orgLeader = data.orgLeader;
			form.orgLeaderPhone = data.orgLeaderPhone;
			form.orgPropertyType = data.orgPropertyType;
			form.orgBuildingArea = data.orgBuildingArea ;
			
			// orgLeader: "",
			// orgLeaderPhone: "",
			// orgAddress: "",
			// orgPropertyType: "",
			// orgBuildingArea: "",
			// orgAddress: "",
			// orgCoordinateLo: "",
			// orgCoordinateLa: "",
			// id: uni.getStorageSync('specicalid') || "",
			// tel: uni.getStorageSync('tel'),
			// status:"2"
		}
	})
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		width: 100%;
		background-color: rgb(239, 241, 252);
		position: relative;
		box-shadow: 2rpx 2rpx 4rpx rgba(0, 0, 0, 0.1);

		.white-content {
			width: 90%;
			margin-left: 5%;
			margin-top: 180rpx;
			// height: 1200rpx;
			border-radius: 35rpx;
			background-color: rgb(245, 251, 254);

			.content-title {
				display: flex;
				// align-items: center;
				height: 100rpx;
				position: relative;

				.content-weight {
					// font-size: 35rpx;
					font-weight: 600;
					margin-left: 40rpx;
					margin-top: 20rpx;
				}

				.content-img {
					position: absolute;
					right: 0;
					top: 0;
					width: 400rpx;
					height: 100%;
				}
			}
		}

		.white-photo {
			width: 90%;
			margin-left: 5%;
			// margin-top: 30rpx;
			height: 300rpx;
			border-radius: 35rpx;
			background-color: #fff;
			box-shadow: 4rpx 4rpx 8rpx rgba(0, 0, 0, 0.1);
			justify-content: space-around;
			align-items: center;
			display: flex;

			.photo {
				width: 300rpx;
				height: 200rpx;
			}
		}

		.white-message {
			width: 90%;
			margin-left: 5%;
			margin-top: 30rpx;
			margin-bottom: 30rpx;
			// height: 800rpx;
			border-radius: 35rpx;
			background-color: #fff;
			box-shadow: 4rpx 4rpx 8rpx rgba(0, 0, 0, 0.1);
			justify-content: space-around;
			// align-items: center;
			display: flex;
			flex-direction: column;

			.message-title {
				width: 100%;
				height: 100rpx;
				align-items: center;
				display: flex;

				.shu {
					width: 10rpx;
					height: 30rpx;
					background-color: #0097FF;
					border-radius: 10rpx;
					margin: 0 20rpx 0 30rpx;
				}

				.message-weight {
					font-size: 30rpx;
					// font-weight: 600;
				}
			}

			.one {
				width: 90%;
				margin-left: 5%;
				border-bottom: 1rpx solid #d7d7d7;
				height: 90rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;

				.one-left {
					margin-left: 10rpx;
				}
				.one-right-spec {
					// margin-right: 10rpx;
					color: #999999;
					// overflow: hidden;
					/* 隐藏超出内容 */
					// white-space: nowrap;
					/* 不换行 */
					font-size: 25rpx;
					// text-overflow: ellipsis;
					width: 300rpx;
					display: flex;
					flex-wrap: wrap;
					// justify-content: flex-end;
				}
				.one-right {
					// margin-right: 10rpx;
					color: #999999;
					overflow: hidden;
					/* 隐藏超出内容 */
					white-space: nowrap;
					/* 不换行 */
					font-size: 25rpx;
					text-overflow: ellipsis;
					width: 300rpx;
					display: flex;
					justify-content: flex-end;
				}
			}
		}
	}

	.photo-left {
		.photo-weight {
			font-size: 26rpx;
			font-weight: 600;
		}

		.photo-font {
			font-size: 23rpx;
			margin-top: 10rpx;
		}

	}

	.finish-button {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 45%;
		height: 100rpx;
		margin: 0rpx auto;
		margin-bottom: 80rpx;
		color: #fff;
		background: linear-gradient(to right, #00C9FF, #0076FF);
		border-radius: 50rpx;
		font-size: 35rpx;
	}

	.one-img {
		position: absolute;
		top: 50%;
		transform: translateY(-50%);
		right: 10rpx;
		width: 60rpx;
		height: 50rpx;
		// margin-right: 10rpx;
		// margin-left: 35rpx;
	}

	.pingfangmi {
		position: absolute;
		top: 50%;
		right: 5rpx;
		transform: translateY(-50%);
	}

	.special {
		display: flex;
		align-items: center;
		width: 280rpx;
		position: relative;
	}

	.popup-father {
		width: 100%;
		// height: 100%;
		position: relative;

		.popup-title {
			position: fixed;
			top: 0;
			left: 0;
			// background-color: red;
			display: flex;
			flex-direction: column;
			width: 100%;

			.title-font {
				font-size: 35rpx;
				font-weight: 600;
			}

			.title-imge {
				width: 35rpx;
				height: 35rpx;
				position: absolute;
				top: 50%;
				right: 40rpx;
				transform: translateY(-50%);
			}

			background-color: #fff;
			z-index: 1;
		}

		.other-title {
			font-size: 32rpx;
			font-weight: 600;
			margin-left: 30rpx;
			margin-top: 50rpx;
		}
	}
	.cards{
		width: 100%;
		display: flex;
		// justify-content: center;
		align-items: center;
		height: 100rpx;
		.zimu{
			margin: 0 30rpx;
			color: #d7d7d7;
			width: 28rpx;
		}
		.font{
			font-size: 30rpx;
		}
	}
</style>
<style>
	.uni-scroll-view {
		height: auto;
	}
</style>