<template>
	<view class="page">
		<view class="content">
			<view class="title-box"></view>
			<view class="state-bar">
				<image src="../../static/images/ic_arrow_left.png" mode="aspectFill" @click="closePage()"></image>
				<view class="tab">
					<view :class="currentIndex==1? 'tab-1':''" @click="tabChange(1)">全部</view>
					<view :class="currentIndex==2? 'tab-1':''" @click="tabChange(2)">我参与的</view>
				</view>
			</view>
			<view class="box">
				<image class="title-img" src="../../static/images/ic_activity_title_bg.png" mode="aspectFill"></image>
				<view class="title-text">
					<text>播撒一片绿色让世界更美</text>
					<text>爱护万千树木让你我同行</text>
				</view>
				<view class="container-box">
					<!-- 搜索框 -->
					<view class="search" v-if="currentIndex==1">
						<view class="search-content">
							<image src="../../static/images/ic_search.png" class="icon" mode="aspectFill"></image>
							<input @input="onSearchInput" placeholder="请输入关键字搜索" />
						</view>
						<view class="btn" @click="doSearch">搜索</view>
					</view>
					<!-- 地区选择 -->
					<!-- <view class="area-box">
						<view class="area-title">当前地区:</view>
						<view class="area-picker">全部地区</view>
						<view class="area-picker">地区选择</view>
					</view> -->
					<!-- 列表 -->
					<view class="content-scroller">
						<scroller v-if="currentIndex==1" @init="initScroller" @down="refreshData" @up="getData"
							:up="optUp" @scroll="navFloatShow(scroller)" :fixed="false">
							<!-- 列表 -->
							<view class="list-box">
								<view class="item" v-for="(item,index) in list" :key="index"
									@click="openDetail(item.activityId)">
									<view class="title">{{item.name}}</view>
									<view class="item-content">
										<image :src="imgUrl+item.img" mode="aspectFill">
										</image>
										<view class="item-box">
											<view class="item-time">活动时间:{{item.startTime}}至{{item.endTime}}</view>
											<view class="item-person">参与人数:{{item.joinedCount}}/{{item.joinCount}}
											</view>
											<view class="item-bottom">
												<view>全国</view>
												<text class="item-status">{{item.status}}</text>
											</view>
										</view>
									</view>
								</view>
							</view>
						</scroller>
						<!-- 我参加的 -->
						<!-- 列表 -->
						<view class="list-box" v-if="currentIndex==2">
							<view v-if="list2.length<=0" class="empty-data">暂无数据~</view>
							<view v-else class="item" v-for="(item,index) in list2" :key="index"
								@click="openDetailType(item.activityDTO.activityId,item.activityJoinId)">
								<view class="title">{{item.activityDTO.name}}</view>
								<view class="item-content">
									<image :src="imgUrl+item.img" mode="aspectFill">
									</image>
									<view class="item-box">
										<view class="item-time">
											活动时间:{{item.activityDTO.startTime}}至{{item.activityDTO.endTime}}</view>
										<view class="item-person">
											参与人数:{{item.activityDTO.joinedCount}}/{{item.activityDTO.joinCount}}
										</view>
										<view class="item-bottom">
											<view>全国</view>
											<text class="item-status">{{item.activityDTO.status}}</text>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import scroller from '@/components/scroller/scroller.vue';
	export default {
		components: {
			scroller
		},

		data() {
			return {
				currentIndex: 1,
				list: [],
				list2: [],
				scroller: {},
				scroller2: {},
				optUp: {
					auto: true,
					onScroll: true,
					page: {
						num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
						size: 10 //每页数据条数,默认10
					},
					empty: {
						tip: '暂无数据~'
					}
				},
				optUp2: {
					auto: true,
					onScroll: true,
					page: {
						num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
						size: 10 //每页数据条数,默认10
					},
					empty: {
						tip: '暂无数据~'
					}
				},
				token: "",
				imgUrl: this.$api.common.imgUrl,
				searchContent: ""
			}
		},
		onLoad(res) {

		},
		onShow(res) {
			this.token = getApp().globalData.token
			if (this.token != "") {
				this.getData2()
			}
		},
		methods: {
			...mapMutations(['login']),
			tabChange(i) {
				this.currentIndex = i;
				if (i == 1) {
					this.bg = this.bg1
				} else {
					this.bg = this.bg2
				}
				if (this.currentIndex == 2) {
					if (this.token == "") {
						uni.setStorageSync("loginOriginUrl", "/pages/duty/activitylist");
						uni.navigateTo({
							url: "/pages/user/login"
						})
					} else {
						this.getData2()
					}
				}
			},
			closePage() {
				uni.navigateBack()
			},
			/*收搜*/
			doSearch() {
				uni.hideKeyboard()
				if (this.searchContent == "") {
					this.$alert("请输入关键字");
				}
				this.refreshData()
			},
			/*初始化滚动*/
			initScroller(scroller) {
				this.scroller = scroller;
				this.loadData();
			},

			/*刷新数据*/
			refreshData() {
				uni.showLoading({
					title: '刷新中'
				});
				this.scroller.resetUpScroll();
			},
			onSearchInput: function(event) {
				this.searchContent = event.target.value
				console.log(event.target.value)
				if (event.target.value == "") {
					this.refreshData()
				}
			},
			/*加载数据*/
			loadData() {
				this.list = [];
				this.scroller.resetUpScroll();
			},
			/*滚动时导航栏浮动*/
			navFloatShow(scroller) {

			},
			//获取列表数据
			getData() {
				this.$app.request({
					url: this.$api.duty.getActivityList,
					method: 'GET',
					data: {
						'rows': '10',
						'page': '1',
						'keywords': this.searchContent
					},
					dataType: 'json',
					success: res => {
						console.log("加载完成")
						if (res.rows != undefined) {
							if (this.scroller.num == 1) {
								this.list = [];
							}
							var tempList = []
							tempList = res.rows
							console.log(tempList[2].photo)
							for (var i = 0; i < tempList.length; i++) {
								if (tempList[i].photo) {
									var index = tempList[i].photo.indexOf(",")
									if (index != -1) {
										tempList[i].img = tempList[i].photo.substring(0, index)
									}
									tempList[i].img = tempList[i].photo
								}
							}
							this.list = this.list.concat(tempList);
							console.log(this.list)
							this.scroller.endBySize(res.rows.length, res.rows.total);
							this.showPageLoading = false;
						} else {
							this.scroller.endSuccess();
							this.$alert(res.msg);
						}
					},
					fail: res => {
						this.scroller.endErr();
					},
					complete: res => {
						uni.stopPullDownRefresh();
						uni.hideLoading();
					}
				});
			},

			//获取列表数据
			getData2() {
				this.$app.request({
					url: this.$api.duty.getMineActivity,
					method: 'GET',
					dataType: 'json',
					header: {
						token: this.token
					},

					success: res => {
						console.log(res)
						if (res != undefined) {
							for (var i = 0; i < res.length; i++) {
								if (res[i].activityDTO.photo) {
									var index = res[i].activityDTO.photo.indexOf(",")
									if (index != -1) {
										res[i].img = res[i].activityDTO.photo.substring(0, index)
									}
									res[i].img = res[i].activityDTO.photo
								}
							}
							this.list2 = res
						}
					},
					fail: res => {
						uni.showToast({
							icon: "error",
							title: "加载失败"
						})
					},
					complete: res => {}
				});
			},
			openDetail(id) {
				uni.navigateTo({
					url: "/pages/duty/activitydetail?id=" + id + "&type=" + 1
				})
			},
			openDetailType(id, ss) {
				uni.navigateTo({
					url: "/pages/duty/activitydetail?id=" + id + "&type=" + 2 + "&itemId=" + ss
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;

	}

	.page {
		display: flex;
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		height: 100%;
	}

	.content-scroller {
		flex: 1;
		width: 100%;
		height: 100rpx;
	}

	.content {
		width: 100%;
		position: relative;
		height: 100%;
	}

	.box {
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		height: 100%;
		width: 98%;
		position: absolute;
		top: 170rpx;
		left: 50%;
		transform: translate(-50%, 0);
		-webkit-transform: translate(-50%, 0);
		box-shadow: 0rpx 7rpx 33rpx 16rpx rgba(188, 226, 204, 0.15);
		border-radius: 30rpx;

		.title-img {
			width: 443rpx;
			height: 203rpx;
			align-self: center;
		}

		.title-text {
			color: white;
			text-align: center;
			align-self: center;
			display: flex;
			flex-direction: column;
			font-size: 30rpx;
			font-weight: 400;
		}
	}

	.tab-bar {
		width: 100%;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		padding-top: 15rpx;

		.tab-active {
			text-align: center;
			font-size: 40rpx;
			font-weight: bold;
			color: #00821E;
		}

		.tab-default {
			text-align: center;
			font-size: 40rpx;
			font-weight: 200;
			color: #00821E;
		}
	}

	.title-box {
		width: 100%;
		height: 800rpx;
		background-image: url('../../static/images/ic_activity_bg.png');
		background-size: 100% 100%;
		background-repeat: no-repeat;
	}

	.state-bar {
		position: absolute;
		top: 0%;
		left: 0%;
		width: 100%;
		margin: 45rpx 0rpx;
		height: 100rpx;
		display: flex;
		flex-direction: row;
		align-items: center;

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

		.tab {
			display: flex;
			flex-direction: row;
			color: white;
			justify-content: center;
			font-size: 40rpx;
			align-self: center;
			text-align: center;
			width: 100%;
			margin-left: -70rpx;

			:last-child {
				margin-left: 40rpx;
			}

			.tab-1 {
				border-bottom-width: 8rpx;
				border-bottom-style: solid;
				border-bottom-color: white;
			}
		}
	}

	.container-box {
		display: flex;
		flex-direction: column;
		background-color: white;
		justify-content: center;
		align-items: flex-start;
		margin: 20rpx;
		padding: 30rpx 0rpx;
		border-top-left-radius: 30rpx;
		border-top-right-radius: 30rpx;
		height: 100%;

		.area-box {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: flex-start;
			margin: 30rpx;

			.area-title {
				font-size: 30rpx;
				color: #747474;
			}

			.area-picker {
				background-color: #E8F6EE;
				margin-left: 20rpx;
				color: #b3b3b3;
				padding: 10rpx 30rpx;
			}
		}
	}

	/*搜索*/
	.search {
		padding: 10rpx 24rpx 10rpx 24rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 90%;
		align-self: center;

		.search-content {
			background: #f2f2f2;
			border-radius: 15px;
			padding: 10rpx 24rpx 10rpx 24rpx;
			margin-right: 15rpx;
			flex: 1;
			display: flex;
			align-items: center;
		}

		/deep/.input {
			color: #8cc7b5;
		}

		.icon {
			width: 30rpx;
			height: 30rpx;
			margin-right: 15rpx;
		}

		.btn {
			color: #b3b3b3;
		}
	}

	.list-box {
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: flex-start;
		padding: 20rpx;
		width: 95%;

		.item {
			display: flex;
			flex-direction: column;
			background: white;
			border-radius: 10rpx;
			margin: 20rpx 0rpx;
			width: 100%;
			box-shadow: 0rpx 7rpx 33rpx 16rpx rgba(188, 226, 204, 0.15);

			.item-content {
				display: flex;
				flex-direction: row;
				align-items: center;
				padding: 10rpx;

				image {
					width: 200rpx;
					height: 130rpx;
				}

				.item-box {
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: flex-start;
					width: 100%;
					margin-left: 20rpx;

					.item-title {
						color: #747474;

					}

					.item-person {
						color: #747474;
					}

					.item-bottom {
						display: flex;
						flex-direction: row;
						width: 95%;
						justify-content: space-between;
						color: #747474;

						.item-status {
							color: #00821E;
							border: 1rpx #8cc7b5 solid;
							font-size: 20rpx;
							text-align: center;
							padding: 0rpx 10rpx;
							align-self: center;
						}
					}
				}
			}



			.title {
				font-size: 30rpx;
				display: -webkit-box;
				text-overflow: ellipsis;
				-webkit-line-clamp: 1;
				-webkit-box-orient: vertical;
				overflow: hidden;
				padding: 10rpx;
				color: #000;
			}


		}

	}

	.empty-data {
		align-items: center;
		align-self: center;
	}
</style>
