<template>
	<view class="container">
		<view class="page">
			<view class="house">
				<view class="details">
					<view class="item">
						<view class="name">物业公司</view>
						<view class="detail">{{formModel.propertyName}}</view>
					</view>
					<view class="item">
						<view class="name">小区</view>
						<view class="detail">{{formModel.platformName}}</view>
					</view>
					<view class="item">
						<view class="name">房屋ID</view>
						<view class="detail">{{formModel.id}}</view>
					</view>
					<view class="item">
						<view class="name">楼栋</view>
						<view class="detail">{{formModel.buildingName}}</view>
					</view>
					<view class="item">
						<view class="name">单元</view>
						<view class="detail">{{formModel.unitName}}</view>
					</view>
					<view class="item">
						<view class="name">房屋编号</view>
						<view class="detail">{{formModel.roomName}}</view>
					</view>
					<view class="item">
						<view class="name">房屋用途</view>
						<view class="detail">{{formModel.buildingTypeName}}</view>
					</view>
					<!-- <view class="item">
						<view class="name">户型</view>
						<view class="detail">{{formModel.propertyName}}</view>
					</view> -->
					<view class="item">
						<view class="name">建筑面积</view>
						<view class="detail">{{formModel.floorSpace || ''}}</view>
					</view>
					<view class="item">
						<view class="name">房屋状态</view>
						<view class="detail" :class="formModel.color">
							<view class="tag">{{formModel.statusName}}</view>
						</view>
					</view>
				</view>
			</view>
			<template v-if="formModel.isAuth === 0">
				<view class="form-submit" @click="toEditor()">修改</view>
			</template>
			<template v-if="formModel.auditStatus === 'refuse'">
				<view class="form-delete" @click="onUnbinding(true)">删除</view>
			</template>
			<view v-if="formModel.auditStatus === 'auditPass' && formModel.isAuth === 0" class="form-submit" @click="onAuthentication()">认证房屋</view>
			<view v-if="formModel.auditStatus === 'auditPass'" class="form-submit" @click="onUnbinding()">解除绑定</view>
		</view>
	</view>
</template>

<script>
	import {
		getHouseDetailApi,
		deleteByOpenIdApi,
		getDictItemsApi,
		editIsAuditApi
	} from '@/config/api.js'
	export default {
		data() {
			return {
				houseId: '',
				formModel: {},
				typeList: []
			}
		},
		methods: {
			ongetDictItems(name) {
				return getDictItemsApi(name)
			},
			async getDetail() {
				let {result} = await getHouseDetailApi({id: this.houseId})
				result['statusName'] = result.isAuth ? '已认证' : '未认证'
				result['color'] = result.isAuth ? 'green' : 'grey'
				result['buildingTypeName'] = this.typeList.find(item=>item.value === result.useTo)?.label
				this.formModel = {...result}
			},
			deleteDialog() {
				this.$refs.popup.open()
			},
			onUnbinding(isDelete) {
				let _this = this
				uni.showModal({
					title: '提示',
					content: `确认要${isDelete ? '删除' : '解绑'}此房屋吗?`,
					success: function (res) {
						if (res.confirm) {
							_this.dialogConfirm(isDelete)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			onAuthentication() {
				let _this = this
				uni.showModal({
					title: '提示',
					content: "确认要认证此房屋吗?",
					success: function (res) {
						if (res.confirm) {
							_this.authenticationConfirm()
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			async dialogConfirm(isDelete) {
				await deleteByOpenIdApi({id: this.formModel.id})
				// let userType = uni.getStorageSync('user_type')
				// if(userType === '1') {
					
				// 	let code = uni.getStorageSync('platform_code')
				// 	if(this.formModel.platformCode === code) {
				// 		uni.setStorageSync('platform_code', '')
				// 		setTimeout(()=> {
				// 			uni.showToast({
				// 				icon: "none",
				// 				title: `${isDelete ? '删除' : '解绑'}成功！`
				// 			})
				// 			setTimeout(()=> {
				// 				uni.reLaunch({
				// 					url: '/pages/home/index'
				// 				});
				// 			}, 1500)
				// 		}, 50)
				// 	} else {
				// 		setTimeout(()=> {
				// 			uni.showToast({
				// 				icon: "none",
				// 				title: `${isDelete ? '删除' : '解绑'}成功！`
				// 			})
				// 			var pages = getCurrentPages();
				// 			var page = pages[pages.length - 2];
				// 			page.$vm.getList()
				// 			setTimeout(()=> {
				// 				uni.navigateBack({delta: 1});
				// 			}, 1500)
				// 		}, 50)
				// 	}
				// }
				setTimeout(()=> {
					uni.showToast({
						icon: "none",
						title: `${isDelete ? '删除' : '解绑'}成功！`
					})
					setTimeout(()=> {
						uni.reLaunch({
							url: '/pages/home/index'
						});
					}, 1500)
				}, 50)
			},
			async authenticationConfirm() {
				await editIsAuditApi({
					id: this.formModel.id
				})
				this.getDetail()
			},
			toEditor() {
				getApp().globalData.registerInfo = {...this.formModel}
				uni.navigateTo({
					url: '/pages/housing/index',
				});
			}
		},
		async onLoad(option) {
			this.typeList = await this.ongetDictItems('house_purpose')
			this.houseId = option.id
			this.getDetail()
		}
	}
</script>
<style>
	page {
		background-color: #FFFFFF;
	}
</style>
<style lang="scss" scoped>
	.page {
		padding: 30rpx 30rpx 90rpx;
	}

	.house {
		margin-bottom: 60rpx;

		.details {
			background-color: #ffffff;
			border-radius: 16rpx;
			margin-bottom: 30rpx;

			.item {
				display: flex;
				height: 84rpx;
				line-height: 84rpx;
				justify-content: space-between;
				border-bottom: 1px solid #F0F0F2;
				align-items: center;

				.name {
					font-size: 28rpx;
					color: #CECED3;
				}

				.detail {
					font-size: 28rpx;
					color: #373737;

					.tag {
						width: 130rpx;
						text-align: center;
						height: 36rpx;
						line-height: 36rpx;
						font-size: 22rpx;
						border-radius: 30rpx;

					}
				}

			}

			.green .tag {
				color: #10D680;
				border: 1px solid #10D680;
				background-color: #F1FFF9;
			}

			.grey .tag {
				color: #CDD0D6;
				border: 1px solid #CDD0D6;
				background-color: #FFFCF6;
			}


		}
	}

	.form-submit {
		height: 80rpx;
		line-height: 80rpx;
		background-color: #6A59F2;
		border-radius: 80rpx;
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		color: #FFFFFF;
		margin-bottom: 30rpx;
	}
	.form-delete {
		height: 80rpx;
		line-height: 80rpx;
		background-color: #ffffff;
		border: 1px solid #6A59F2;
		border-radius: 80rpx;
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		color: #6A59F2;
		margin-top: 30rpx;
	}
</style>