<template>
	<view class="main">
		<view class="one" v-if="info.file.length>0">
			<swiper class="swiper" circular :indicator-dots="true" indicator-color="#F5F5F5"
				indicator-active-color="#ffffff" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item class="list" v-for="(item,index) in info.file" :key="index">
					<image class="image" :src="item.url" mode="">
					</image>
				</swiper-item>
			</swiper>
		</view>
		<view class="bottom">
			<view class="two">
				<view class="name">{{info.name||'暂无'}}</view>
			</view>
			<view class="thr">
				<view class="thr_1">
					<view class="left">价格：</view>
					<view class="right"><text class="text_1" :user-select='true'>￥{{info.money||'暂无'}}</text></view>
				</view>
				<view class="thr_1">
					<view class="left">数量：</view>
					<view class="right">
						<text v-if="info.num==0">售罄</text>
						<text v-else>{{info.num||'暂无'}} 间</text>
					</view>
				</view>
			</view>
			<view class="four">
				<view class="four_1">简介</view>
				<rich-text :nodes="formatRichText(info.brief)"></rich-text>
			</view>
			<view class="foot">
				<view class="button" v-if="info.num==0"><text>售罄</text></view>
				<view class="button" v-else @click="toBuy"><text>预订</text></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				user: {},
				info: {
					file: []
				},
				// 字典表
				typeList: []
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			await that.searchOther();
			await that.search();
		},
		onShow: async function(e) {
			const that = this;
			that.searchToken();
		},
		methods: {
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						that.$set(that, `user`, res);
					}
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				if (that.id) {
					const res = await that.$api(`/room/${that.id}`, 'GET', {})
					if (res.errcode == '0') {
						const type = that.typeList.find(i => i.value == res.data.type)
						if (type) res.data.zhType = type.label
						that.$set(that, `info`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 处理富文本
			formatRichText(html) {
				if (html) {
					// 富文本内容格式化
					return html && html.replace(/<img[^>]*>/gi, function(match, capture) {
						// 查找所有的 img 元素
						return match.replace(/style=".*"/gi, '').replace(/style='.*'/gi,
							'')
						// 删除找到的所有 img 元素中的 style 属性
					}).replace(/\<img/gi, '<img style="width:100%;"') // 对 img 元素增加 style 属性，并设置宽度为 100%
				}
			},
			// 预订
			toBuy(item) {
				const that = this;
				if (that.user && that.user._id) {
					console.log("预订");
				} else {
					uni.navigateTo({
						url: `/pagesIndex/login/index`
					})
				}
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询类型
				res = await that.$api(`/dictData`, 'GET', {
					type: 'project_type',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `typeList`, res.data);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.one {
			.swiper {
				height: 70vw;

				.list {
					.image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.bottom {
			position: absolute;
			top: 65vw;
			left: 0;
			right: 0;
			background-color: var(--mainColor);
			border-radius: 20px;
			padding: 2vw 0 0 0;

			.two {
				padding: 4vw 2vw;

				.name {
					padding: 1vw 0;
					font-weight: bold;
					font-size: var(--font16Size);
				}

				.two_2 {
					color: var(--f85Color);
					font-size: var(--font12Size);
				}

			}

			.thr {
				padding: 2vw;

				.thr_1 {
					display: flex;
					font-size: var(--font14Size);
					padding: 0 0 1vw 0;

					.left {
						padding: 0 1vw 0 0;
						font-weight: bold;
					}

					.right {
						color: var(--f85Color);

						.text_1 {
							font-size: var(--font14Size);
							color: var(--fF0Color);
						}
					}
				}
			}

			.four {
				padding: 2vw;
				margin: 0 0 15vw 0;

				.four_1 {
					font-weight: bold;
					font-size: var(--font16Size);
					padding: 0 0 1vw 0;
				}
			}
		}

		.foot {
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;

			.button {
				display: flex;
				justify-content: center;
				background-color: var(--f3CColor);
				padding: 4vw;

				text {
					font-size: var(--font16Size);
					color: var(--mainColor);
				}
			}
		}

	}
</style>