<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 v-else>
						<view class="order-item flex" v-for="(item, index) in tabItem.orderList" :key="index"
							@click="navTo('/pages/user/evaluate?id=' + item.id + '&type=' + tabCurrentIndex)">
							<view class="order-image">
								<image :src="item.main_pic" mode=""></image>
							</view>
							<view class="order-info">
								<view class="order-title clamp2">{{ item.title }}</view>
								<view class="order-info-main flex">
									<view class="order-time">{{ item.add_time }}</view>
									<view class="order-btnBox flex" v-if="tabCurrentIndex == 0">
										<view class="order-btn-item"
											@click.stop="navTo('/pages/order/evaluate?id=' + item.id)">
											<view class="order-btn-icon">
												<image src="../../static/icon/bj.png" mode=""></image>
											</view>
											<view class="order-btn-font">编辑</view>
										</view>
										<view class="order-btn-item" @click.stop="del(item.id)">
											<view class="order-btn-icon1">
												<image src="../../static/icon/sc.png" mode=""></image>
											</view>
											<view class="order-btn-font1">删除</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<uni-load-more :status="tabItem.loadingType"
						v-if="!(tabItem.orderList.length == 0 && tabItem.loaded)"></uni-load-more>
				</scroll-view>
				<view class="btn" @click="navTo('/pages/order/order?state=3')" v-if="tabCurrentIndex == 0">发帖</view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import empty from '@/components/empty';
	import {
		delete_comment,
		my_replylist,
		reply_savelist
	} from '@/api/activity.js';
	export default {
		components: {
			empty
		},
		data() {
			return {
				height: '',
				tabCurrentIndex: 0,
				navList: [{
						state: 1,
						text: '草稿箱',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 1,
						text: '已审核',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 0,
						text: '审核中',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					}
				]
			};
		},
		onLoad(opt) {
			this.tabCurrentIndex = opt.id * 1;
		},
		onShow() {
			this.loadData();
		},
		onReachBottom() {},
		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: {
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			navTo(url) {
				uni.navigateTo({
					url
				});
			},
			// 删除草稿
			del(id) {
				const obj = this;
				uni.showModal({
					title: '提示',
					content: '是否删除这个草稿',
					success: function(res) {
						if (res.confirm) {
							delete_comment({}, id).then(({
								data
							}) => {
								obj.$api.msg('删除成功');
								obj.loadData('shua');
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			async loadData(source) {
				let obj = this;
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;
				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';
				if (index == 0) {
					reply_savelist({
							page: navItem.page,
							limit: navItem.limit
						})
						.then(({
							data
						}) => {
							console.log(data, '123456');
							navItem.orderList = navItem.orderList.concat(data);
							navItem.page++;
							if (navItem.limit == data.length) {
								navItem.loadingType = 'more';
							} else {
								navItem.loadingType = 'noMore';
							}
							obj.$set(navItem, 'loaded', true);
						})
						.catch(e => {
							console.log(e);
						});
				} else {
					my_replylist({
							page: navItem.page,
							limit: navItem.limit,
							type: state
						})
						.then(({
							data
						}) => {
							navItem.orderList = navItem.orderList.concat(data);
							navItem.page++;
							if (navItem.limit == data.length) {
								navItem.loadingType = 'more';
							} else {
								navItem.loadingType = 'noMore';
							}
							obj.$set(navItem, 'loaded', true);
						})
						.catch(e => {
							console.log(e);
						});
				}
			}
		}
	};
</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;
				}
			}
		}
	}

	//列表
	.swiper-box {
		margin-top: 20rpx;

		.order-item {
			padding: 22rpx 24rpx 24rpx;
			border-bottom: 1px solid #eeeeee;
			justify-content: flex-start;

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

			.order-image {
				flex-shrink: 0;
				width: 200rpx;
				height: 160rpx;
			}

			.order-info {
				margin-left: 16rpx;
				width: 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.order-title {
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
				}

				.order-info-main {
					margin-top: 20rpx;

					.order-time {
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}

					.order-btnBox {
						justify-content: flex-start;

						.order-btn-item {
							display: flex;
							align-items: center;
							margin: 0 10rpx;

							.order-btn-icon {
								width: 30rpx;
								height: 30rpx;
							}

							.order-btn-font {
								margin-left: 10rpx;
								font-size: 26rpx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #000000;
							}

							.order-btn-icon1 {
								width: 30rpx;
								height: 30rpx;
							}

							.order-btn-font1 {
								margin-left: 10rpx;
								font-size: 26rpx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #ff4c4c;
							}
						}
					}
				}
			}
		}
	}

	.list-scroll-content {
		background: #ffffff;
		height: 100%;

		.yan {
			padding-bottom: 100rpx;
		}
	}

	.tab-content {
		position: relative;
	}

	.btn {
		position: absolute;
		bottom: 20rpx;
		left: 0;
		right: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 560rpx;
		height: 76rpx;
		background: #000000;
		border-radius: 38rpx;
		font-size: 34rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #ffffff;
		margin: 0 auto;
	}
</style>