<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="two_1">
					<view class="name textOne">{{info.name||'暂无'}}</view>
					<view class="collect">
						<uni-fav :checked="info.is_collect" @click="onClick(info)" />
					</view>
				</view>
				<view class="two_2">{{info.open_time||'暂无'}}</view>
			</view>
			<view class="thr">
				<view class="thr_1">
					<view class="left">地址：</view>
					<view class="right"><text :user-select='true'>{{info.address||'暂无'}}</text></view>
				</view>
				<view class="thr_1">
					<view class="left">类型：</view>
					<view class="right">
						<text class="text_1">#{{info.zhType||'暂无'}}</text>
					</view>
				</view>
				<view class="thr_1">
					<view class="left">人均消费：</view>
					<view class="money">
						<text>￥{{info.money||'暂无'}}</text>
					</view>
				</view>
				<view class="thr_1">
					<view class="left">联系电话：</view>
					<view class="right"><text :user-select='true'>{{info.phone||'暂无'}}</text></view>
				</view>
			</view>
			<view class="four" v-if="info.ticket">
				<view class="four_1">票务政策</view>
				<rich-text :nodes="formatRichText(info.ticket)"></rich-text>
			</view>
			<view class="four">
				<view class="four_1">简介</view>
				<rich-text :nodes="formatRichText(info.brief)"></rich-text>
			</view>
		</view>
	</view>
</template>

<script>
	import moment from 'moment';
	let innerAudioContext = uni.createInnerAudioContext();
	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();
		},
		onShow: async function(e) {
			const that = this;
			await that.searchToken();
			await that.search();
		},
		onHide: function(e) {
			innerAudioContext.destroy(); //销毁这个实例
		},
		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 info = {}
					if (that.user._id) info.user = that.user._id
					const res = await that.$api(`/location/location/${that.id}`, 'GET', info)
					if (res.errcode == '0') {
						const type = that.typeList.find(i => i.value == res.data.type)
						if (type) res.data.zhType = type.label
						// 播放语音
						const voice = res.data.voice
						if (voice && voice.length > 0) {
							innerAudioContext.destroy(); //销毁这个实例
							innerAudioContext = uni.createInnerAudioContext();
							innerAudioContext.autoplay = true;
							innerAudioContext.sessionCategory = "soloAmbient"
							innerAudioContext.src = voice[0].url
							innerAudioContext.onPlay(() => {
								console.log('开始播放');
							});
							innerAudioContext.onError((res) => {
								uni.showToast({
									title: res.errMsg,
									icon: 'error',
									duration: 2000
								});
							});
						}
						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%
				}
			},
			// 收藏
			async onClick(item) {
				const that = this;
				if (that.user && that.user._id) {
					let res;
					if (item.is_collect == false) {
						const form = {
							user: that.user._id,
							source: item._id,
							source_type: "location",
							type: '1',
							create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
						}
						res = await that.$api(`/like`, 'POST', form);
					} else res = await that.$api(`/like/${item.collect}`, 'DELETE', {})
					if (res.errcode == '0') that.search()
				} 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;

				.two_1 {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.name {
						width: 80vw;
						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;
					}

					.money {
						color: var(--fF0Color);
					}

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

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

			.four {
				padding: 2vw;

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