<template>
	<view class="luntan-index">
		<u-navbar title=" " bgColor="#00B0B0" :autoBack="false" :placeholder="true" :fixed="true" height="44">
			<view class="search" slot="left">
				<u-search placeholder="" v-model="search" :disabled="true" :showAction="false"
					@click="tosearch"></u-search>
				<!-- <u-search placeholder="" v-model="search" :showAction="false" @search="tosearch"></u-search> -->
			</view>
			<view class="search-right hflex acenter" slot="right">
				<image src="@/static/images/menu.png" mode="aspectFill"></image>
				<image src="@/static/images/add.png" mode="aspectFill" @click.stop="show_add = !show_add"></image>
				<view class="add-show" v-if="show_add">
					<view class="hflex acenter show-item" @click="toadd('info')">
						<image src="@/static/images/add-zixun.png" mode="aspectFill"></image>
						<text>发布资讯</text>
					</view>
					<view class="hflex acenter show-item" @click="toadd('article')">
						<image src="@/static/images/add-wenzhang.png" mode="aspectFill"></image>
						<text>发布文章</text>
					</view>
					<view class="hflex acenter show-item" @click="toadd('video')">
						<image src="@/static/images/add-shipin.png" mode="aspectFill"></image>
						<text>发布视频</text>
					</view>
					<view class="hflex acenter show-item" @click="toadd('post')">
						<image src="@/static/images/add-luntan.png" mode="aspectFill"></image>
						<text>发布论坛</text>
					</view>
				</view>
			</view>
		</u-navbar>
		<view class="top">
			<swiper-list type="post"></swiper-list>
			<view class="vflex huati">
				<view class="hflex acenter jbetween">
					<view class="title">话题推荐</view>
					<view class="more-text" @click="tomore">查看更多</view>
				</view>
				<scroll-view :scroll-x="true" class="tuijian">
					<view class="hflex acenter">
						<view class="tuijian-item" v-for="(item,index) in tuijian_list" :key="index">
							<view class="item-box vflex" v-for="(a,b) in item" :key="b">
								<view @click="toinfo(a)">{{a.title}}</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
			<view class="tabs hflex acenter">
				<text :class="current == 0 ? 'tabs-active' : ''" @click="tochangetab(0)">最新</text>
				<text class="line">|</text>
				<text :class="current == 1 ? 'tabs-active' : ''" @click="tochangetab(1)">点赞最多</text>
				<text class="line">|</text>
				<text :class="current == 2 ? 'tabs-active' : ''" @click="tochangetab(2)">评论最多</text>
			</view>
		</view>
		<view class="list">
			<listinfo type="post" :data="item" v-for="(item,index) in list" :key="index"></listinfo>
		</view>
		<view class="fixed hflex acenter jcenter" @click="toadd('luntan')">
			<u-icon name="edit-pen-fill" color="#00b0b0" size="20"></u-icon>
			<text class="text">参与</text>
		</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	import listinfo from '@/components/list-info/index.vue'
	import swiperList from '@/components/swiper-list/index.vue'
	export default {
		components: {
			listinfo,
			swiperList
		},
		data() {
			return {
				show_add: false,
				search: '',
				swiper_list: [],
				swiper_current: 0,
				tuijian_list: [],
				list: [],
				page: 1,
				limit: 10,
				last_page: 1,
				current: 0
			}
		},
		onLoad() {
			this.getswiper()
			this.page = 1
			this.list = []
			this.getlist()
			this.tuijian_list = []
			this.gettuijian()
		},
		onShow() {
			
		},
		onReachBottom() {
			if (this.last_page == this.page) {
				uni.$u.toast('已经到底了')
				return
			} else {
				this.page++
				this.getlist()
			}
		},
		methods: {
			toinfo(item) {
				uni.navigateTo({
					url: '/pageA/topic-detail?id=' + item.id
				})
			},
			tochangetab(index) {
				this.current = index
				this.page = 1
				this.list = []
				this.getlist()
			},
			gettuijian() {
				var _this = this
				$api.req({
					url: 'topic',
					method: 'GET',
					data: {
						is_recommend: 1,
						page: 1,
						limit: 9,
					}
				}, function(res) {
					if (res.code == 10000) {
						var huati_list = res.data.list
						_this.tuijian_list = Array.from({
							length: Math.ceil(huati_list.length / 3)
						}, (_, i) => {
							return huati_list.slice(i * 3, i * 3 + 3);
						});
					}
				})
			},
			getswiper() {
				var _this = this
				$api.req({
					url: 'banner',
					method: 'GET',
					data: {
						type: 'post',
						limit: 5
					}
				}, function(res) {
					if (res.code == 10000) {
						_this.swiper_list = res.data
					}
				})
			},
			tomore() {
				uni.navigateTo({
					url: '/pageA/huati'
				})
			},
			getlist() {
				var _this = this
				var order = 'id'
				if (this.current == 1) {
					order = 'like_count'
				} else if (this.current == 2) {
					order = 'comment_count'
				}
				$api.req({
					url: 'post',
					method: 'GET',
					data: {
						page: _this.page,
						limit: _this.limit,
						order: order
					}
				}, function(res) {
					if (res.code == 10000) {
						_this.list = _this.list.concat(res.data.list)
						_this.last_page = res.data.last_page
					}
				})
			},
			/* 发布 */
			toadd(val) {
				if (val == 'post') {
					uni.navigateTo({
						url: '/pageA/add-luntan'
					})
				} else {
					uni.navigateTo({
						url: '/pageA/add-wenzhang?type=' + val
					})
				}
				this.show_add = false
			},
			/* 搜索 */
			tosearch() {
				uni.navigateTo({
					url: '/pageA/search'
				})
			},
		}
	}
</script>

<style lang="scss">
	.luntan-index::v-deep {
		background: #F5F5F5;
		min-height: 100vh;

		.fixed {
			position: fixed;
			bottom: 152rpx;
			left: 270rpx;
			z-index: 999;
			width: 210rpx;
			height: 84rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 48rpx -12rpx rgba(0, 0, 0, 0.2);
			border-radius: 42rpx;

			.text {
				font-size: 32rpx;
				color: #00B0B0;
				padding: 0 0 0 18rpx;
				line-height: 44rpx;
			}
		}

		.top {
			box-sizing: border-box;
			width: 100%;
			background: #FFFFFF;
			padding: 20rpx 28rpx;

			.title {
				font-size: 32rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				color: #333333;
				padding: 24rpx 0;
			}

			.more-text {
				font-size: 24rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				color: #666666;
			}

			.huati {
				padding-bottom: 36rpx;

				.tuijian {
					width: 100%;
					height: 248rpx;
					overflow: auto;
					height: 248rpx;
					white-space: nowrap;

					.tuijian-item {
						flex-shrink: 0;
						width: 494rpx;
						height: 248rpx;
						background: #F5F5F5;
						border-radius: 12rpx;
						margin: 0 20rpx 0 0;
						box-sizing: border-box;
						padding: 28rpx 24rpx 0;

						.item-box {
							font-size: 28rpx;
							color: #333333;
							padding: 0 0 36rpx;
							white-space: nowrap;
						}
					}
				}
			}

			.tabs {
				.line {
					padding: 0 18rpx;
				}

				text {
					font-size: 28rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					color: rgba(34, 34, 34, .6);
				}

				.tabs-active {
					color: #222222 !important;
				}
			}
		}

		.search {
			width: 572rpx;
			background: rgba(255, 255, 255, .3);
			border-radius: 32rpx;

			.u-search__content {
				background: rgba(255, 255, 255, .3) !important;
			}

			.u-search__content__input {
				background-color: unset !important;
			}

			.u-search__content__icon {
				background-color: unset !important;

				.u-icon__icon {
					color: #fff !important;
				}
			}

			input {
				background: rgba(255, 255, 255, .3)
			}

		}

		.search-right {

			// padding: 0 32rpx 0 0;
			.img {
				position: relative;
			}

			image {
				width: 40rpx;
				height: 40rpx;
				margin-left: 20rpx;
			}

			.add-show {
				position: absolute;
				right: 30rpx;
				top: 88rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 4rpx 20rpx 0rpx rgba(0, 0, 0, 0.1);
				padding: 42rpx 32rpx 0;

				.show-item {
					margin-bottom: 40rpx;

					image {
						width: 36rpx;
						height: 36rpx;
						margin: 0;
					}

					text {
						white-space: nowrap;
						font-size: 26rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #333333;
						padding: 0 0 0 24rpx;
					}
				}
			}
		}
	}
</style>