<template>
	<view class="main">
		<view class="info">
			<scroll-view scroll-y="true" :scroll-top="scrollTop" class="scroll-view" @scrolltolower="toPage"
				@scroll="toScroll">
				<view class="list-scroll-view">
					<view class="one">
						<swiperImg :imgsList='info.file'></swiperImg>
					</view>
					<view class="two">
						<view class="two_1">{{info.title||'暂无标题'}}</view>
						<view class="two_2">#{{info.zhType||'暂无类型'}}</view>
						<view class="two_3">
							<rich-text :nodes="formatRichText(info.content)"></rich-text>
						</view>
						<view class="two_4">发布时间：{{info.create_time||'暂无'}}</view>
					</view>
					<view class="thr">
						<view class="thr_1">评论 {{total||0}}</view>
						<scroll-view scroll-y="true" class="scroll">
							<view class="list-scroll-view">
								<view class="list" v-for="(item,index) in list" :key="index">
									<image class="image"
										:src="item.user_file&&item.user_file.length>0?item.user_file[0].url:'../../static/my.png'"
										mode="aspectFill">
									</image>
									<view class="other">
										<view class="name textOver">{{item.user_name||'暂无'}}</view>
										<view class="content">{{item.content||'暂无'}}</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="foot_1">
				<view class="left">
					<uni-easyinput confirmType="send" prefixIcon="chatbubble" v-model="content" placeholder="说点什么..."
						@confirm="toSend">
					</uni-easyinput>
				</view>
				<view class="right">
					<view class="right_1" @tap.stop="toLike('0')">
						<text v-if="info.is_like" class="iconfont icon-aixin1"></text>
						<text v-else class="iconfont icon-aixin"></text>
						<text>{{like||0}}</text>
					</view>
					<view class="right_1" @tap.stop="toLike('1')">
						<text v-if="info.is_collect" class="iconfont icon-shoucang"></text>
						<text v-else class="iconfont icon-shoucang1"></text>
						<text>{{collect||0}}</text>
					</view>
					<view class="right_1" @click="pageScroll">
						<text class="iconfont icon-pinglun"></text>
						<text>{{total||0}}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	import moment from 'moment';
	import swiperImg from '../../components/swiper/index.vue';
	export default {
		components: {
			swiperImg
		},
		data() {
			return {
				id: '',
				config: {},
				user: {},
				info: {},
				// 评论数量
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 评论
				content: '',
				// 点赞
				like: 0,
				// 收藏
				collect: 0,
				// 字典表
				typeList: []
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.searchConfig();
			that.searchToken();
			await that.searchOther();
			await that.search();
		},
		methods: {
			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
					});
				}
			},
			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) {
					let res;
					res = await that.$api(`/article/${that.id}`, 'GET', {})
					if (res.errcode == '0') {
						res.data.zhType = that.searchDict(res.data.type, 'type')
						if (that.user._id) {
							const like = await that.$api(`/like`, 'GET', {
								source: res.data._id,
								user: that.user._id,
								type: '0'
							});
							if (like.errcode == '0' && like.total > 0) res.data.is_like = true
							else res.data.is_like = false
							const collect = await that.$api(`/like`, 'GET', {
								source: res.data._id,
								user: that.user._id,
								type: '1'
							});
							if (collect.errcode == '0' && collect.total > 0) res.data.is_collect = true
							else res.data.is_collect = false
							const likenum = await that.$api(`/like`, 'GET', {
								source: res.data._id,
								type: '0',
								limit: 1
							});
							if (likenum.errcode == '0') that.$set(that, `like`, likenum.total)
							const collectnum = await that.$api(`/like`, 'GET', {
								source: res.data._id,
								type: '1',
								limit: 1
							});
							if (collectnum.errcode == '0') that.$set(that, `collect`, collectnum.total)
						}
						that.$set(that, `info`, res.data)
						uni.setNavigationBarTitle({
							title: res.data.contact_name
						});
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
					// 查看评论
					that.searchComment();
				}
			},
			// 处理富文本
			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 searchComment() {
				const that = this;
				let res;
				let info = {
					skip: that.skip,
					limit: that.limit,
					is_use: '0',
					status: '1',
					source: that.info._id
				}
				res = await that.$api(`/comment`, 'GET', {
					...info,
				});
				if (res.errcode == '0') {
					for (let val of res.data) {
						const user = await that.$api(`/user/${val.user}`, 'GET', {});
						if (user.errcode == '0') {
							val.user_file = user.data.logo
							val.user_name = user.data.name || user.data.nick_name
						}
					}
					let list = [...that.list, ...res.data];
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
			},
			// 查询字典表
			searchDict(e, model) {
				const that = this;
				let data
				if (model == 'type') {
					data = that.typeList.find((i) => i.value == e);
					if (data) return data.label
					else return '暂无'
				}
			},
			// 滑动到评论专区
			pageScroll() {
				this.$set(this, `scrollTop`, 0);
				this.$nextTick(() => {
					setTimeout(() => {
						uni.createSelectorQuery().in(this).select(`.thr`).boundingClientRect(res => {
							this.$set(this, `scrollTop`, res.top);
						}).exec();
					}, 100);
				});
			},
			// 点赞/取消点赞
			async toLike(type) {
				const that = this;
				const info = that.info
				let res;
				let like;
				if (type == '0') like = info.is_like
				else like = info.is_collect
				if (like == false) {
					const form = {
						user: that.user._id,
						source: info._id,
						source_type: "article",
						type: type,
						create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
					}
					res = await that.$api(`/like`, 'POST', form);
					if (res.errcode == '0') {
						that.clearPage();
						that.search()
					}
				} else {
					res = await that.$api(`/like`, 'GET', {
						source: info._id,
						user: that.user._id,
						type: type
					});
					if (res.errcode == '0' && res.total > 0) {
						await that.$api(`/like/${res.data[0]._id}`, 'DELETE', {})
						that.clearPage();
						that.search()
					}
				}
			},
			// 发送
			async toSend(content) {
				const that = this;
				if (that.user && that.user._id) {
					const form = {
						user: that.user._id,
						user_name: that.user.name || that.user.nick_name,
						source: that.info._id,
						content,
						create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
					}
					let res = await that.$api(`/comment`, 'POST', form);
					if (res.errcode == '0') {
						uni.showToast({
							title: '评论成功 等待审核',
							icon: 'none'
						})
						that.$set(that, `content`, '')
						that.searchComment()
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						})
					}
				} else {
					uni.navigateTo({
						url: `/pagesIndex/login/index`
					})
				}
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.searchComment();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			// 触底
			toScroll(e) {
				const that = this;
				// let up = that.scrollTop;
				// that.$set(that, `scrollTop`, e.detail.scrollTop);
				// let num = Math.sign(up - e.detail.scrollTop);
				// if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询类型
				res = await that.$api(`/dictData`, 'GET', {
					type: 'home_tabs',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `typeList`, res.data);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		width: 100vw;
		height: 100vh;

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

			.two {
				padding: 0 2vw;

				.two_1 {
					font-weight: bold;
					font-size: var(--font16Size);
				}

				.two_2 {
					padding: 1vw;
					font-size: var(--font14Size);
					color: var(--f3CColor);
				}


				.two_4 {
					padding: 3vw 0;
					font-size: var(--font12Size);
					color: var(--f99Color);
					border-bottom: 1px solid var(--f9Color);
				}
			}

			.thr {

				.thr_1 {
					padding: 2vw 4vw;
					font-weight: 500;
					font-size: var(--font15Size);
					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;
						}
					}
				}
			}
		}

		.foot {
			padding: 2vw;
			background-color: var(--mainColor);

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

				.left {
					.is-input-border {
						border-radius: 45px;
					}
				}

				.right {
					width: 45%;
					display: flex;
					justify-content: space-around;

					.right_1 {
						display: flex;
						align-items: center;
						font-size: var(--font17Size);

						.iconfont {
							padding: 0 1vw;
							font-size: 18px;
						}
					}
				}
			}
		}
	}

	.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>