<template>
	<view class="userpost" :class="$store.state.AppStyle" style="background-color: #f6f6f6;">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar bg-white" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					所有圈子
				</view>
				<view class="action">
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		<view class="data-box" v-for="(item,index) in sectionList" :key="index">
			<view class="cu-bar bg-white">
				<view class="action data-box-title">
					{{item.name}}
				</view>
			</view>
			<view class="section-page">
				<view class="section-page-box"  v-for="(data,j) in item.subList" :key="j" >
					<view class="section-ico"  @tap="goSection(data.id)">
						<image :src="data.pic" mode="aspectFill" v-if="data.pic&&data.pic!=''"></image>
						<image src="https://starimg.illlt.com/upload/2024/1/27/51139d81-1c5b-4568-a50b-79dae4656a31.png" mode="aspectFill" v-else></image>
					</view>
					<view class="section-intro"  @tap="goSection(data.id)">
						<view class="section-item-tile">
							{{data.name}}
						</view>
						<view class="section-item-value">
							{{data.postNum}} 帖子
						</view>
					</view>
					<view class="section-page-btn">
						<block v-if="type==0">
							<view class="cu-btn bg-gradual-red"  @tap="data.isFollow=1,sectionFollow(data.id,1)" v-if="data.isFollow==0">
								<text class="cuIcon-add margin-right-xs"></text> 加入
							</view>
							<view class="cu-btn"  @tap="data.isFollow=0,sectionFollow(data.id,0)" v-if="data.isFollow==1">
								退出
							</view>
						</block>
						
						<view class="cu-btn bg-blue" @tap="setCur(data)"  v-if="type==1">
							选择
						</view>
						<view class="cu-btn bg-gradual-orange"  @tap="addPost(data)"  v-if="type==2">
							<text class="cuIcon-add margin-right-xs"></text> 发帖
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	export default {
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				sectionList:[],
				moreText:"加载更多",
				page:1,
				isLoading:0,
				type:0
			}
		},
		onLoad(res) {
			var that = this;
			if(localStorage.getItem('token')){
				
				that.token = localStorage.getItem('token');
			}
			
			// #ifdef APP-PLUS || MP
			that.NavBar = this.CustomBar;
			// #endif
			if(res.type){
				that.type = res.type;
			}
		},
		onShow(){
			var that = this;
			if(localStorage.getItem('token')){
				
				that.token = localStorage.getItem('token');
			}
			if(localStorage.getItem('sectionList')){
				that.sectionList = JSON.parse(localStorage.getItem('sectionList'));
			}
			// #ifdef APP-PLUS
			
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			that.getSectionList(false);
			that.page = 1;
			
		},
		methods: {
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			setCur(data){
				var that = this;
				localStorage.setItem('curSection',JSON.stringify(data));
				that.back();
			},
			getSectionList(isPage){
				var that = this;
				that.$Net.request({
					url: that.$API.sectionList(),
					data:{
						"limit":50,
						"page":1,
						"token":that.token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						uni.stopPullDownRefresh()
						that.isLoad=0;
						if(res.data.code==1){
							var list = res.data.data;
							var parentList = [];
							for(var i in list){
								if(list[i].parent==0){
									list[i].subList = [];
									parentList.push(list[i]);
								}
							}
							for(var j in list){
								if(list[j].parent!=0){
									for(var p in parentList){
										if(list[j].parent == parentList[p].id){
											parentList[p].subList.push(list[j]);
										}
									}
								}
							}
							that.sectionList = parentList;
							localStorage.setItem('sectionList',JSON.stringify(that.sectionList));
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						uni.stopPullDownRefresh()
						that.moreText="加载更多";
						that.isLoad=0;
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			goSection(id){
				var that = this;
				if(that.type!=0){
					return false
				}
				uni.navigateTo({
				    url: '/pages/forum/home?id='+id
				});
			},
			sectionFollow(id,type){
				var that = this;
				uni.showLoading({
					title: "加载中"
				});
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"sectionId":id,
					"token":token,
					"type":type
				}
				that.$Net.request({
					url: that.$API.sectionFollow(),
					data:data,
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
						if(res.data.code==0){
							that.getSectionList();
						}
						
					},
					fail: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: "网络开小差了哦",
							icon: 'none'
						})
					}
				})
			},
			addPost(data){
				var that = this;
				
				if(!localStorage.getItem('token')||localStorage.getItem('token')==""){
					uni.showToast({
						title: "请先登录哦",
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: '/pages/edit/addPost?section='+data.name+"&sectionid="+data.id
				});
				
				// uni.navigateTo({
				// 	url: '/pages/forum/addPost?section='+data.name+"&sectionid="+data.id
				// });
			},
		}
	}
</script>

<style>
</style>