<template>
	<view class="main">
		<view class="top">
			<view class="left">
				<image v-if="info.file.length>0" class="image" :src="info.file&&info.file.length>0?info.file[0].url:''"
					mode="aspectFill">
					<image v-else class="image" :src="config.file&&config.file.length>0?config.file[0].url:''"
						mode="aspectFill">
			</view>
			<view class="right">
				<view class="right_1 textOver">{{info.name||'暂无'}}</view>
				<view class="right_2">已售 {{info.buy_num||0}}</view>
				<view class="right_3">
					<uni-fav :checked="info.is_collect" @tap.stop="onClick(info)" />
				</view>
			</view>
		</view>
		<view class="center">
			<scroll-view scroll-y="true" class="scroll-view">
				<view class="list-scroll-view">
					<view class="center_1" v-if="info.buy_explain">
						<view class="name">购买须知</view>
						<view class="cotent">
							<rich-text :nodes="formatRichText(info.buy_explain)"></rich-text>
						</view>
					</view>
					<view class="center_1" v-if="info.use_explain">
						<view class="name">使用说明</view>
						<view class="cotent">
							<rich-text :nodes="formatRichText(info.use_explain)"></rich-text>
						</view>
					</view>
					<view class="center_1" v-if="info.money_explain">
						<view class="name">费用说明</view>
						<view class="cotent">
							<rich-text :nodes="formatRichText(info.money_explain)"></rich-text>
						</view>
					</view>
					<view class="center_1" v-if="info.other_explain">
						<view class="name">其他说明</view>
						<view class="cotent">
							<rich-text :nodes="formatRichText(info.other_explain)"></rich-text>
						</view>
					</view>
					<view class="evaluate" v-if="rateList.length>0">
						<view class="evaluate_1">评价</view>
						<scroll-view scroll-y="true" class="scroll">
							<view class="list-scroll-view">
								<view class="list" v-for="(item,index) in rateList" :key="index">
									<image class="image"
										:src="item.user.logo&&item.user.logo.length>0?item.user.logo[0].url:'../../static/my.png'"
										mode="aspectFill">
									</image>
									<view class="other">
										<view class="name textOver">{{item.user.nick_name||'暂无'}}</view>
										<view class="content"><uni-rate size="18" :readonly="true" :value="item.rate" />
										</view>
										<view class="content">{{item.content||'暂无'}}</view>
										<view class="content">
											<div v-for="(i,indexx) in item.file" :key="indexx">
												<image class="images" :src="i.url" mode="aspectFill">
												</image>
											</div>
										</view>
										<view class="name">{{item.create_time||'暂无'}}</view>
									</view>
								</view>
								<view class="is_bottom" v-if="is_bottom">
									<text>{{config.bottom_title||'到底了！'}}</text>
								</view>
							</view>
						</scroll-view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="foot">
			<view class="money">
				<text>￥{{info.original_price||'暂无'}}</text>
				<text>￥{{info.money||'暂无'}}起</text>
			</view>
			<view class="button">
				<button type="warn" @tap.stop="toBuy(info)">预订</button>
			</view>
		</view>
	</view>
</template>

<script>
	import moment from 'moment';
	export default {
		data() {
			return {
				id: '',
				user: {},
				config: {},
				info: {
					file: []
				},
				// 字典表
				typeList: [],
				// 评价
				rateList: []
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.searchConfig();
		},
		onShow: async function(e) {
			const that = this;
			await that.searchToken();
			await that.searchOther();
			await that.search();
		},
		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
					});
				}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, 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(`/ticket/ticket/${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
						that.$set(that, `info`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
					// 评价
					const rate = await that.$api(`/rateOrder`, 'GET', {
						source: that.id,
						status: '1',
					})
					if (rate.errcode == '0') that.$set(that, `rateList`, rate.data);
				}
			},
			// 处理富文本
			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: "ticket",
							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`
					})
				}
			},
			// 预订
			toBuy(item) {
				const that = this;
				if (that.user && that.user._id) {
					uni.navigateTo({
						url: `/pagesIndex/order/order?id=${item.id||item._id}`
					})
				} 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 {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.top {
			display: flex;
			padding: 1vw;
			border-radius: 5px;
			border: 1px solid var(--f9Color);

			.left {
				width: 25vw;

				.image {
					width: 100%;
					height: 25vw;
				}
			}

			.right {
				padding: 2vw;
				width: 65vw;
				margin: 0 0 0 2vw;

				.right_1 {
					font-size: var(--font18Size);
					font-weight: bold;
					padding: 0 0 2px 0;
				}

				.right_2 {
					padding: 0 0 2px 0;
					font-size: var(--font14Size);
					color: var(--f85Color);
				}

				.right_3 {
					display: flex;
					justify-content: flex-end;
				}
			}
		}

		.center {
			position: relative;
			flex-grow: 1;

			.center_1 {
				padding: 2vw;

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

				.content {
					padding: 2vw;
				}
			}

			.evaluate {
				.evaluate_1 {
					padding: 2vw;
					font-size: var(--font18Size);
					font-weight: bold;
					border-bottom: 1px solid var(--f9Color);
				}

				.list {
					display: flex;
					align-items: center;
					padding: 2vw;

					.image {
						width: 10vw;
						height: 10vw;
						border-radius: 10vw;
					}

					.other {
						display: flex;
						flex-direction: column;
						width: 80vw;
						padding: 2vw;
						border-bottom: 1px solid var(--f9Color);
						font-size: var(--font12Size);

						.name {
							color: var(--f85Color);
							padding: 2px 0;
						}

						.content {
							display: flex;
							margin: 1vw 0;

							.images {
								width: 15vw;
								height: 15vw;
								margin: 0 5px 0 0;
							}
						}
					}
				}
			}
		}

		.foot {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 2vw;
			background-color: var(--mainColor);

			.money {
				text-align: right;
				color: var(--fF0Color);
				font-size: var(--font16Size);
				font-weight: bold;

				text:first-child {
					text-decoration: line-through;
					color: var(--f69Color);
					font-size: var(--font12Size);
					font-weight: 400;
				}
			}

			.button {
				button {
					width: 30vw;
					font-size: var(--font14Size);
					border-radius: 40px;
					background: linear-gradient(to right, #00BFFF, #007AFF);
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		width: 100%;
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}
</style>