<template>
	<view class="main">
		<view class="info">
			<scroll-view scroll-y="true" class="scroll-view">
				<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="info.content"></rich-text>
						</view>
						<view class="two_4">发布时间：{{info.create_time}}</view>
					</view>
					<view class="thr">
						<view class="thr_1">评论 {{total}}</view>
						<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
							<view class="list-scroll-view">
								<view class="thr_2">
									<view class="list" v-for="(item,index) in list" :key="index" @tap="toBuy(item)">
										<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>
									</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"><text class="iconfont icon-aixin"></text>{{like}}</view>
					<view class="right_1"><text class="iconfont icon-shoucang1"></text>{{collect}}</view>
				</view>
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	import swiperImg from '../../components/swiper/index.vue';
	export default {
		components: {
			swiperImg
		},
		data() {
			return {
				id: '',
				config: {},
				user: {},
				info: {},
				// 评论数量
				list: [],
				total: 0,
				skip: 0,
				limit: 15,
				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.content = res.data.content.replace(/\<img/gi, '<img class="rich-img"');
						res.data.zhType = that.searchDict(res.data.type, 'type')
						that.$set(that, `info`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
					// 查看评论
					that.searchComment();
				}
			},
			// 查看评论
			async searchComment() {
				const that = this;
				let res;
				let info = {
					skip: that.skip,
					limit: that.limit,
					is_use: '0',
					status: '1',
				}
				res = await that.$api(`/comment`, 'GET', {
					...info,
				});
				if (res.errcode == '0') {
					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 '暂无'
				}
			},
			// 发送
			async toSend(item) {
				console.log(item);
			},
			// 分页
			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`, 15)
				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">
	.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_3 {
					.rich-img {
						width: 100% !important;
						display: block;
					}
				}

				.two_4 {
					padding: 2vw;
					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);
				}
			}
		}

		.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: 40%;
					display: flex;
					justify-content: space-around;
					padding: 2vw;

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

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

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