<template>
	<view class="contentStyle" style="">
		<view class="status-bar"></view>
		<uni-nav-bar title="房屋详情" left-icon="left" @clickLeft="goBack" :border="false" backgroundColor="transparent">
		</uni-nav-bar>

		<scroll-view class="page" scroll-y="true">
			<!-- 顶部状态区 -->
			<view class="status-header" v-if="statusConfig">
				<view class="status-icon" :class="statusConfig.iconClass">
					<text class="icon-text">{{ statusConfig.iconText }}</text>
				</view>
				<text class="status-title">{{ statusConfig.title }}</text>

				<!-- 步骤条 -->
				<view class="step-bar">
					<view class="step-item" v-for="(step, index) in stepList" :key="index"
						:class="{ active: index < currentStep, done: index < currentStep - 1 }">
						<view class="step-circle">{{ index + 1 }}</view>
						<text class="step-text">{{ step }}</text>
						<view class="step-line" v-if="index < stepList.length - 1"
							:class="{ active: index < currentStep - 1 }"></view>
					</view>
				</view>
			</view>

			<!-- 房屋信息 -->
			<view class="info-card">
				<view class="card-title">
					<view class="title-line"></view>
					房屋信息
				</view>
				<view class="info-row">
					<text class="info-label">小区</text>
					<text class="info-value">{{form.villageName}}</text>
				</view>
				<view class="info-row">
					<text class="info-label">房间号</text>
					<text class="info-value">{{form.houseNo}}</text>
				</view>
				<view class="info-row">
					<text class="info-label">业主</text>
					<text class="info-value">{{form.ownerName}}</text>
				</view>
				<view class="info-row">
					<text class="info-label">房间状态</text>
					<text class="info-value" :style="{color: form.status === '0' ? '#00aaff' : form.status === '1' ? 
					'#FF700A' : form.status === '2' ? '#00aa00' :  form.status === '3' ? '#00557f' : '#999' }">{{
						form.status === '0' ? '自住' : form.status === '1' ?
						'闲置' : form.status === '2' ? '租赁' :  form.status === '3' ? '其他' : ''
					}}</text>
				</view>

			</view>

			<!-- 住户信息 -->
			<view class="info-card">
				<view class="card-title">
					<view class="title-line"></view>
					住户信息
				</view>
				<view class="info-row">
					<text class="info-label">住户</text>
					<text class="info-value">{{form.name}}</text>
				</view>
				<view class="info-row">
					<text class="info-label">房间号</text>
					<text class="info-value">{{form.houseNo}}</text>
				</view>
				<view class="info-row">
					<text class="info-label">证件类型</text>
					<text
						class="info-value">{{form.idCardType=='0'?'居民身份证':form.idCardType=='1'?'护照':form.idCardType=='2'?'港澳通行证':''}}</text>
				</view>
				<view class="info-row">
					<text class="info-label">证件号码</text>
					<text class="info-value">{{form.idCard}}</text>
				</view>
				<view class="info-row">
					<text class="info-label">与业主关系</text>
					<text
						class="info-value">{{form.ownerRelationship=='0'?'业主本人':form.ownerRelationship=='1'?'亲属':form.ownerRelationship=='2'?'租户':form.ownerRelationship=='3'?'朋友':''}}</text>
				</view>
				<view class="info-row">
					<text class="info-label">手机号码</text>
					<text class="info-value">{{form.phone}}</text>
				</view>

				<!-- 身份证照片区 -->
				<view class="photo-row">
					<text class="info-label" style="width: 200rpx;">本人身份证照片</text>
					<view class="photo-list">
						<view class="photo-item"
							:style="{backgroundImage: `url(${form.cardImageFront||''})`,backgroundSize: 'cover', backgroundPosition: 'center', backgroundRepeat: 'no-repeat'}">
						</view>
						<view class="photo-item"
							:style="{backgroundImage: `url(${form.cardImageBack||''})`,backgroundSize: 'cover', backgroundPosition: 'center', backgroundRepeat: 'no-repeat'}">
						</view>

					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 底部操作按钮 -->
		<view class="bottom-btn-wrap">
			<!-- 仅认证失败显示修改按钮 -->
			<button v-if="currentStatus === 'fail'" class="btn-modify" @click="clickUpdate()">
				修改
			</button>
			<button class="btn-delete" :class="{ 'delete-only': currentStatus !== 'fail' }" @click="onDelete">
				删除
			</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		computed
	} from 'vue'
	import {
		onReachBottom,
		onPullDownRefresh,
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	import {
		getMyHouseDetail,
		deleteMyHouse
	} from '/pageSubPack/api/apiSub.js'

	// 响应式数据
	const goBack = () => {
		uni.navigateBack()
	}

	const statusBarHeight = ref(20)
	const id = ref()
	const currentStatus = ref('')
	const stepList = ref(["房屋信息", "住户信息", "物业审核", "认证成功"])
	const form = ref([])
	const statusMap = ref({
		success: {
			title: "房屋认证成功",
			iconClass: "icon-success",
			iconText: "✓",
			currentStep: 4,
		},
		pending: {
			title: "房屋审核中，请耐心等待",
			iconClass: "icon-pending",
			iconText: "⏳",
			currentStep: 3,
		},
		fail: {
			title: "房屋认证失败，请重新提交",
			iconClass: "icon-fail",
			iconText: "✕",
			currentStep: 3,
		},
	})

	// 计算属性
	const statusConfig = computed(() => {
		return statusMap.value[currentStatus.value]
	})
	const currentStep = computed(() => {
		return statusConfig.value.currentStep
	})

	// 生命周期
	onLoad((option) => {
		id.value = option.id
		currentStatus.value = option.certificationStatus == '0' ? 'pending' : option.certificationStatus == '1' ?
			'success' : option.certificationStatus == '2' ? 'fail' : ''

	})

	onShow(() => {})
	onMounted(() => {
		getDetail()
	})

	const getDetail = async () => {
		// 防重复请求

		try {
			const res = await getMyHouseDetail({
				id: id.value
			})

			const data = res.data
			form.value = data
			console.log(form.value);
		} catch (err) {
			uni.showToast({
				title: err.msg || '网络异常',
				icon: 'none'
			})
			// console.error('列表接口报错：', err)
		} finally {

		}
	}
	// 方法（函数名完全不变）
	const clickUpdate = () => {
		uni.setStorageSync('operationType', 'update')
		uni.navigateTo({
			url: '/pageSubPack/my/addHouse?id=' + id.value + '&sourcePage=addHouse'
		})
	}

	const onDelete = () => {
		uni.showModal({
			title: "确认删除",
			content: "确认删除此房屋信息吗?",
			success: (res) => {
				if (res.confirm) {
					uni.showLoading({
						title: '删除中...',
						mask: true
					})
					deleteMyHouse({
						id: id.value
					}).then(res => {
						if (res.code === 200) {
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							})
							setTimeout(() => {
									uni.redirectTo({ url: "/pageSubPack/my/myHouseIndex" })
							}, 1000)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}

					}).catch(err => {
						uni.showToast({
							title: err.msg,
							icon: 'none'
						});
					})

				}
			}
		})
	}

</script>

<style lang="scss">
	page {
		background: #fff;
	}
</style>

<style scoped>
	.contentStyle {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f5f7fa;
	}

	.page {
		flex: 1;
		overflow-y: auto;
		padding: 0px 40rpx;
		box-sizing: border-box;
	}

	.status-header {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 20rpx 0px 20rpx;
	}

	.status-icon {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 20rpx;
	}

	.icon-success {
		background-color: #2F77FD;
	}

	.icon-pending {
		background-color: #2F77FD;
	}

	.icon-fail {
		background-color: #E34D59;
	}

	.icon-text {
		font-size: 40rpx;
		font-weight: bold;
		color: inherit;
	}

	.icon-success .icon-text {
		color: #fff;
	}

	.icon-pending .icon-text {
		color: #fff;
	}

	.icon-fail .icon-text {
		color: #fff;
	}

	.status-title {
		font-size: 30rpx;
		font-weight: 500;
		color: #333;
		margin-bottom: 40rpx;
	}

	.step-bar {
		display: flex;
		align-items: center;
		width: 100%;
		justify-content: space-between;
		padding: 0 20rpx;
	}

	.step-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		position: relative;
		flex: 1;
	}

	.step-circle {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		background-color: #1677ff;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		z-index: 2;
	}

	.step-item.active .step-circle {
		background-color: #1677ff;
	}

	.step-item:not(.active) .step-circle {
		background-color: #e5e5e5;
		color: #999;
	}

	.step-text {
		font-size: 24rpx;
		color: #666;
		margin-top: 10rpx;
		text-align: center;
	}

	.step-line {
		position: absolute;
		top: 20rpx;
		left: 50%;
		width: 100%;
		height: 4rpx;
		background-color: #e5e5e5;
		z-index: 1;
	}

	.step-line.active {
		background-color: #1677ff;
	}

	.info-card {
		padding: 20rpx;
		border: 1rpx solid #f0f0f0;
		border-radius: 8rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
	}

	.card-title {
		display: flex;
		align-items: center;
		font-size: 30rpx;
		font-weight: 500;
		color: #333;
		margin-bottom: 30rpx;
	}

	.title-line {
		width: 8rpx;
		height: 28rpx;
		background-color: #1677ff;
		margin-right: 10rpx;
		border-radius: 4rpx;
	}

	.info-row {
		display: flex;
		margin-bottom: 24rpx;
		align-items: center;
	}

	.info-row:last-child {
		margin-bottom: 0;
	}

	.info-label {
		width: 180rpx;
		font-size: 28rpx;
		color: #666;
	}

	.info-value {
		font-size: 28rpx;
		color: #333;
		flex: 1;
	}

	.status-tag {
		color: #ff7d00;
		font-weight: 500;
	}

	.photo-row {
		margin-top: 30rpx;
		display: flex;
		flex-direction: column;
	}

	.photo-list {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
		margin-top: 20rpx;
	}

	.photo-item {
		width: 100%;
		height: 170px;
		background-color: #f6f6f6;
		border-radius: 8rpx;
	}

	.bottom-btn-wrap {
		padding: 20rpx 30rpx 40rpx;
		display: flex;
		gap: 20rpx;
	}

	.btn-modify {
		flex: 1;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #1677ff;
		color: #fff;
		font-size: 30rpx;
		border-radius: 8rpx;
		border: none;
	}

	.btn-delete {
		flex: 1;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #fff;
		color: #f53f3f;
		border: 1rpx solid #f53f3f;
		font-size: 30rpx;
		border-radius: 8rpx;
	}

	.btn-delete.delete-only {
		flex: 1;
	}

	.toast-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: transparent;
		z-index: 9999;
	}

	.toast-content {
		background-color: rgba(0, 0, 0, 0.8);
		padding: 40rpx 60rpx;
		border-radius: 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.toast-icon {
		width: 60rpx;
		height: 60rpx;
		line-height: 60rpx;
		border-radius: 50%;
		background-color: #fff;
		color: #1677ff;
		font-size: 40rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.toast-text {
		color: #fff;
		font-size: 28rpx;
	}

	.status-bar {
		width: 100vw;
		height: 46px;
	}
</style>