<template>
	<view class="userpost" :class="AppStyle">
		<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>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		<view class="data-box">
			<view class="forum-list-main postReview">
				<view class="no-data" v-if="postList.length==0">
					<text class="cuIcon-text"></text>暂时没有数据
				</view>
				<view class="forum-list-box" v-for="(item,index) in postList" :key="index">
					<view class="forum-list-box">
						<view class="forum-list-content forum-shadow">
							<view class="forum-list-user">
								<view class="forum-avatar">
									<tn-lazy-load :image="item.userJson.avatar" borderRadius="50%" height="90" mode="aspectFill">
									</tn-lazy-load>
								</view>
							
								<view class="forum-userinfo">
									<view class="forum-userinfo-name" :class="item.userJson.isvip>0?'name-vip':''">
										{{item.userJson.name}}
										<image v-if="item.userJson.isvip>0" :src="vipImg"
											style="border-radius: 0px;margin-left: 6upx;margin-right: 0upx;width: 70upx;height: 35upx;"
											mode="widthFix"></image>
										<image :src="lvImg+getLv(item.userJson.experience)+'.png'"
											style="border-radius: 0px;margin-left: 6upx;margin-right: 0upx;width: 44upx;height: 22upx;"
											mode="widthFix"></image>
									</view>
									<view class="forum-userinfo-date">
										{{formatDate(item.created)}}
									</view>
								</view>
							</view>
							<block v-if="item.isAds!=1">
								<view class="forum-list-text">
									<view class="forum-list-title text-content-1">
										{{item.title}}
									</view>
									<view class="forum-list-intro text-content-2">
										{{item.text}}
									</view>
								</view>
								 <block v-if="item.images.length==1">
									<view class="forum-media forum-one">
										<view class="blogger__main-image blogger__main-image--1 tn-margin-bottom-sm">
											<tn-lazy-load :image="item.images[0]"
											mode="aspectFill" @tap="previewImage(item.images,item.images[0])"></tn-lazy-load>
										</view>
									</view>
								</block>
								<block v-if="item.images.length==2">
									<view class="grid flex-sub col-2 grid-square grid-square-2 grid-square-10">
										<view class="bg-img radius-qz">
											<tn-lazy-load :image="item.images[0]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[0])"></tn-lazy-load>
										</view>
										<view class="bg-img radius-pm">
											<tn-lazy-load :image="item.images[1]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[1])"></tn-lazy-load>
										</view>
									</view>
								</block>
								<block v-if="item.images.length==3">
									<view class="grid flex-sub col-3 grid-square grid-square-2 grid-square-10">
										<view class="bg-img radius-qz">
											<tn-lazy-load :image="item.images[0]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[0])"></tn-lazy-load>
										</view>
										<view class="bg-img">
											<tn-lazy-load :image="item.images[1]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[1])"></tn-lazy-load>
										</view>
										<view class="bg-img radius-pm">
											<tn-lazy-load :image="item.images[2]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[2])"></tn-lazy-load>
										</view>
									</view>
								</block>
								<block v-if="item.images.length==4">
									<view class="grid flex-sub col-2 grid-square grid-square-1 grid-square-10">
										<view class="bg-img radius-q">
											<tn-lazy-load :image="item.images[0]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[0])"></tn-lazy-load>
										</view>
										<view class="bg-img radius-p">
											<tn-lazy-load :image="item.images[1]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[1])"></tn-lazy-load>
										</view>
										
									</view>
									<view class="grid flex-sub col-2 grid-square grid-square-2 grid-square-10">
										<view class="bg-img radius-z">
											<tn-lazy-load :image="item.images[2]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[2])"></tn-lazy-load>
										</view>
										<view class="bg-img radius-m">
											<tn-lazy-load :image="item.images[3]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[3])"></tn-lazy-load>
										</view>
									</view>
								</block>
								<block v-if="item.images.length>4">
									<view class="grid flex-sub col-3 grid-square grid-square-1 grid-square-10">
										<view class="bg-img radius-q">
											<tn-lazy-load :image="item.images[0]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[0])"></tn-lazy-load>
										</view>
										<view class="bg-img">
											<tn-lazy-load :image="item.images[1]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[1])"></tn-lazy-load>
										</view>
										<view class="bg-img radius-p">
											<tn-lazy-load :image="item.images[2]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[2])"></tn-lazy-load>
										</view>
									</view>
									<view class="grid flex-sub col-3 grid-square grid-square-2 grid-square-10">
										<view class="bg-img radius-z">
											<tn-lazy-load :image="item.images[3]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[3])"></tn-lazy-load>
										</view>
										<view class="bg-img">
											<tn-lazy-load :image="item.images[4]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[4])"></tn-lazy-load>
										</view>
										<view class="bg-img radius-m">
											<tn-lazy-load :image="item.images[5]"
												mode="aspectFill" @tap="previewImage(item.images,item.images[5])"></tn-lazy-load>
										</view>
									</view>
								</block>
							</block>
							<view class="forum-list-operate padding-sm text-center bg-white">
								<block v-if="item.status==0">
									<text class="bg-green cu-btn xs radius" @tap="toReview(item.id,1,index)"> <text class="cuIcon-check"></text>通过</text>
									<text class="bg-red cu-btn xs radius margin-left" @tap="toReview(item.id,0,index)"><text class="cuIcon-close"></text>不通过</text>
								</block>
								<block v-if="item.status==1">
									<text class="bg-black cu-btn xs radius" @tap="toLock(item.id,2,index)"><text class="cuIcon-lock"></text>锁定</text>
									<text class="bg-purple cu-btn xs radius  margin-left" @tap="move(item.id)"><text class="cuIcon-triangleupfill"></text>移动</text>
								</block>
								<block v-if="item.status==2">
									<text class="bg-black cu-btn xs radius" @tap="toLock(item.id,1,index)"><text class="cuIcon-unlock"></text>解除锁定</text>
								</block>
							</view>
							<view class="forum-list-operate padding-sm text-center" style="display: flex;justify-content: space-evenly;">
									<tn-button backgroundColor="#01BEFF" fontColor="white" @tap="toEdit(item)">编辑</tn-button>
									<tn-button backgroundColor="#E83A30" fontColor="white" @tap="toDelete(item.id)">删除</tn-button>
							</view>
							</view>
					
					</view>
					
					
				</view>
				<view class="load-more" @tap="loadMore" v-if="dataLoad&&postList.length>0">
					<text>{{moreText}}</text>
				</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,
				vipImg: this.$API.SPvip(),
				lvImg: this.$API.SPLv(),
				page:1,
				moreText:"加载更多",
				dataLoad:false,
				isLoad:0,
				token:"",
				postList:[],
				
				isLoading:0,
				type:"waiting",
				
				allowDelete:0,
			}
		},
		onPullDownRefresh(){
			var that = this;
			that.page=1;
			that.getPostList(false);
			var timer = setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000)
			
		},
		onReachBottom() {
		    //触底后执行的方法，比如无限加载之类的
			var that = this;
			if(that.isLoad==0){
				that.loadMore();
				
			}
		},
		onShow(){
			var that = this;
			that.page = 1;
			uni.$emit('tOnLazyLoadReachBottom');
			// #ifdef APP-PLUS
			
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			if(localStorage.getItem('token')){
				that.token=localStorage.getItem('token');
				that.getPostList(false);
			}
		},
		onLoad() {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = this.CustomBar;
			// #endif
			
		},
		methods: {
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			loadMore(){
				var that = this;
				that.moreText="正在加载中...";
				that.isLoad=1;
				that.getPostList(true);
			},
			getPostList(isPage){
				var that = this;
				var page = that.page;
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				if(isPage){
					page++;
				}
				that.$Net.request({
					url: that.$API.draftList(),
					data:{
						"limit":10,
						"page":page,
						"token":token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						that.dataLoad =true;
						that.isLoad=0;
						if(res.data.code==1){
							var list = res.data.data;
							if(list.length>0){
								
								var postList = list;
								if(isPage){
									that.page++;
									that.postList = that.postList.concat(postList);
								}else{
									that.postList = postList;
								}
							}else{
								if(isPage){
									that.moreText="没有更多数据了";
								}else{
									that.postList = [];
								}
								
							}
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
						that.isLoading = 1;
					},
					fail: function(res) {
						that.dataLoad =true;
						that.isLoad=0;
						that.moreText="加载更多";
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
						that.isLoading = 1;
					}
				})
			},
			
			getLv(i) {
				var that = this;
				if (!i) {
					var i = 0;
				}
				var lv = that.$API.getLever(i);
				return lv;
			},
			contentConfig(){
				var that = this;
				that.$Net.request({
					
					url: that.$API.contentConfig(),
					method: "get",
					dataType: 'json',
					success: function(res) {
						if(res.data.code==1){
							that.allowDelete = res.data.data.allowDelete;
						}
					},
					fail: function(res) {
						uni.showToast({
							title: "网络开小差了哦",
							icon: 'none'
						})
					}
				})
			},
			toDelete(id){
				var that = this;
				var token = "";
				
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"key":id,
					"token":token
				}
				uni.showModal({
				    title: '确定要删除该草稿吗',
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: "加载中"
				            });
				            
				            that.$Net.request({
				            	url: that.$API.contentsDelete(),
				            	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==1){
				            			that.page=1;
				            			that.moreText="加载更多";
				            			that.isLoad=0;
				            			that.getPostList();
				            		}
				            		
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: "网络开小差了哦",
				            			icon: 'none'
				            		})
				            	}
				            })
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			toPost(){
				var that = this;
				
				uni.navigateTo({
					url: '/pages/edit/articlePost'
				});
			},
			toType(i){
				var that = this;
				that.type=i;
				that.page=1;
				that.moreText="加载更多";
				that.isLoad=0;
				that.getPostList(false);
			},
			subText(text,num){
				if(text.length < null){
					return text.substring(0,num)+"……"
				}else{
					return text;
				}
				
			},
			formatDate(datetime) {
				var datetime = new Date(parseInt(datetime * 1000));
				// 获取年月日时分秒值  slice(-2)过滤掉大于10日期前面的0
				var year = datetime.getFullYear(),
					month = ("0" + (datetime.getMonth() + 1)).slice(-2),
					date = ("0" + datetime.getDate()).slice(-2),
					hour = ("0" + datetime.getHours()).slice(-2),
					minute = ("0" + datetime.getMinutes()).slice(-2);
				//second = ("0" + date.getSeconds()).slice(-2);
				// 拼接
				var result = year + "-" + month + "-" + date + " " + hour + ":" + minute;
				// 返回
				return result;
			},
			replaceSpecialChar(text) {
				if(!text){
					return false;
				}
				text = text.replace(/&quot;/g, '"');
				text = text.replace(/&amp;/g, '&');
				text = text.replace(/&lt;/g, '<');
				text = text.replace(/&gt;/g, '>');
				text = text.replace(/&nbsp;/g, ' ');
				return text;
			},
			toEdit(data){
				
				var that = this;
				
				if(data.isMd==1){
					uni.navigateTo({
						url: '/pages/forum/addPost?type=edit&section='+data.sectionJson.name+"&sectionid="+data.section+"&id="+data.id+'&draft=1'
					});
				}else{
					uni.navigateTo({
						url: '/pages/edit/addPost?type=edit&section='+data.sectionJson.name+"&sectionid="+data.section+"&id="+data.id+'&draft=1'
					});
				}
				
			},
			toDelete(id){
				var that = this;
				var token = "";
				
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"id":id,
					"token":token
				}
				uni.showModal({
				    title: '确定要删除该帖子草稿吗',
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: "加载中"
				            });
				            
				            that.$Net.request({
				            	url: that.$API.draftDelete(),
				            	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==1){
				            			// that.back();
										that.getPostList(false);
				            		}
				            		
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: "网络开小差了哦",
				            			icon: 'none'
				            		})
				            	}
				            })
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
		}
	}
</script>
<style>
</style>