<template>
	<view class="content">
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item"
				:class="{ current: tabCurrentIndex === index }" @click="tabClick(index)">{{ item.text }}</view>
		</view>
		<swiper :current="tabCurrentIndex" :style="{ height: height }" class="swiper-box" duration="300"
			@change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view scroll-y="true" class="list-scroll-content" :class="{ yan: tabCurrentIndex == 0 }"
					@scrolltolower="loadData">
					<!-- 空白页 -->
					<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>
					<view class="main" v-for="(item, index) in tabItem.orderList" :key="index" @click="navTo('/pages/share/myarticle?id=' + item.id + '&type=' + tabCurrentIndex)">
						<view class="main-userInfo">
							<view class="avatar">
								<image :src="item.author" mode=""></image>
							</view>
							<view class="main-user-info">
								<view class="main-userName">{{item.title}}</view>
								<view class="main-user-time">{{item.add_time}}</view>
							</view>
							<view class="fenxiang flex">
								<image class="img-lj" src="../../static/icon/lj.png"></image>
								<view class="wen-lj">{{item.visit}}</view>
							</view>
						</view>
						<view class="text">
							<view>{{item.content}}</view>
						</view>
						<view class="fh-im">
							<image v-for="item in item.share_images" class="fh-img" :src="item" mode=""></image>
						</view>
						<view class="bot flex">
							<image class="del" src="../../static/icon/del.png" mode=""></image>
							<view @click.stop="del(item.id)" class="sc">删除</view>
						</view>
					</view>
					<!-- 订单列表 -->
					<uni-load-more :status="tabItem.loadingType"
						v-if="!(tabItem.orderList.length == 0 && tabItem.loaded)"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
			<button class="rele" @click="navTo('/pages/share/myarticle')" >发布素材</button>
	</view>
</template>

<script>
	import empty from '@/components/empty';
	import {
		my_article,
		delete_article,
	} from '@/api/user.js';
	import {
		getTime
	} from '@/utils/rocessor.js'
	export default {
		components: {
			empty
		},
		data() {
			return {
				height: '',
				tabCurrentIndex: 0,
				navList: [{
						type: 0,
						state: -1,
						text: '草稿箱',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						type: 1,
						state: 1,
						text: '已审核',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						type: 1,
						state: 0,
						text: '审核中',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					}
				]
			};
		},
		onLoad() {
		this.loadData();
		},
		onShow() {},
		onReady(res) {
			var _this = this;
			uni.getSystemInfo({
				success: resu => {
					const query = uni.createSelectorQuery();
					query.select('.swiper-box').boundingClientRect();
					query.exec(function(res) {
						_this.height = resu.windowHeight - res[0].top + 'px';
						console.log('打印页面的剩余高度', _this.height);
					});
				},
				fail: res => {}
			});
		},

		methods: {
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			dele(id) {
				const obj = this;
				uni.showModal({
					title: '提示',
					content: '是否删除这个草稿',
					success: function(res) {
						if (res.confirm) {
							delete_article({}, id).then(({
								data
							}) => {
								obj.$api.msg('删除成功');
								obj.loadData('shua');
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			navTo(url) {
				uni.navigateTo({
					url
				});
			},
			async loadData(source) {
				let obj = this;
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;
				let type = navItem.type;
				if (source === 'shua') {
					navItem.loadingType = 'more';
					navItem.page = 1;
					navItem.limit = 10;
					navItem.orderList = [];
				}
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';
				my_article({
						page: navItem.page,
						limit: navItem.limit,
						state: state
					}, type)
					.then(({
						data
					}) => {
						let arr = []
						data.list = data.list.map((e)=>{
							e.share_images = e.share_images.split(',');
							return e
						});
						if (state == -1) {
							arr = data.list
						}
						if (state == 0) {
							arr = data.list.filter(e => {
								return e.check_status == 0
							})
						}
						if (state == 1) {
							arr = data.list.filter(e => {
								return e.check_status != 0
							})
						}
						arr.forEach(e => {
							e.add_time = getTime(e.add_time)
						})
						console.log(arr);
						navItem.orderList = navItem.orderList.concat(arr);
						navItem.page++;
						if (navItem.limit == data.list.length) {
							navItem.loadingType = 'more';
						} else {
							navItem.loadingType = 'noMore';
						}
						obj.$set(navItem, 'loaded', true);
					})
					.catch(e => {
						console.log(e,'233');
					});
			}
		}
	};
</script>

<style lang="scss">
	page,
	.content {
		min-height: 100%;
		height: auto;
	}

	.navbar {
		display: flex;
		height: 88rpx;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: #999999;
			position: relative;

			&.current {
				color: #000;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid #c54b4a;
				}
			}
		}
	}

	.main {
		margin: 20rpx;
		padding: 35rpx 20rpx 24rpx;
		background: #ffffff;
		width: 702rpx;
		border-radius: 30rpx;

		image {
			width: 100%;
			height: 100%;
		}

		.main-userInfo {
			display: flex;
			align-items: center;

			.avatar {
				width: 80rpx;
				height: 80rpx;
				background: #ffffff;
				border-radius: 50%;
			}

			.main-user-info {
				margin-left: 14rpx;
				line-height: 1;

				.main-userName {
					font-size: 28rpx;
					font-weight: bold;
					color: #000000;
				}

				.main-user-time {
					margin-top: 14rpx;
					font-size: 21rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
				}
			}

			.fenxiang {
				padding-left: 310rpx;

				.img-lj {
					width: 28rpx;
					height: 28rpx;
				}

				.wen-lj {
					font-size: 28rpx;
					color: #FF6F0F;
					padding-left: 10rpx;
				}
			}
		}

		.text {
			word-wrap: break-word;
			word-break: normal;
			margin: 20rpx 0rpx;
			font-size: 28rpx;
			font-weight: 500;
			color: #666666;
		}

		.fh-im {
			.fh-img {
				// margin: auto 10rpx;
				margin: 10rpx 21rpx 0rpx 0rpx;
				width: 200rpx;
				height: 200rpx;
				// padding: 10rpx;
			}
		}

		.bot {
			.del {
				margin-left: 540rpx;
				width: 24rpx;
				height: 28rpx;
			}

			.sc {
				padding-right: 40rpx;
				font-size: 24rpx;
				font-weight: 500;
				color: #FF4C4C;
			}
		}
	}

	.rele {
		position: fixed;
		width: 750rpx;
		bottom: 0rpx;
		background-color: #FF6F0F;
		color: #ffffff;
	}

	.swiper-box {
		margin-top: 20rpx;

		.tab-content {
			position: relative;
		}
	}
</style>