<template>
	<view>
		<!-- swiper -->
		<view class="myswiper flex justify-center align-center" v-if="myhd.length!=0">
			<swiper :indicator-dots="false" style="width: 100%;height: 100%;" :autoplay="true" :interval="3000" :duration="300">
				<swiper-item v-for="(item,index) in myhd" :key="index">
					<view class="swiper-item">
						<image :src="item" style="width: 100%;height: 340rpx;" mode="aspectFill"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 活动列表 -->
		<view :style="myhd.length==0?'margin-top: 20rpx;':'margin-top: -40rpx;'" class="list flex justify-center flex-wrap">
			<view class="list-box flex justify-center" v-if="item.id != 14" v-for="(item,index) in huodongList" :key="index" @click="item.quantity==0 &&item.id != 14?gotoinfo(item.id):''">
				<view class="list-box-c flex justify-between align-center">
					<view class="list-box-c-l flex align-center">
						<image :src="item.image.split(',')[0]" style="width: 100rpx;height: 100rpx;" mode="aspectFill"></image>
						<view class="list-box-c-l-txt">
							{{item.title}}
						</view>
					</view>
					<view class="list-box-c-r" v-if="item.quantity==0" @click.stop="joinHd(item.id,index)">
						加入
					</view>
					<view v-else>
						<!-- <view class="list-box-c-r">
							已加入
						</view> -->
						<view class="list-box-c-t" @click="selectShang(item.activityShopId )" v-if="item.type=='3'">
							选择商品
						</view>
						<view class="list-box-c-t" @click.stop="quitHuo(item.id)">
							退出
						</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="status" v-if="huodongList.length>0" :icon-type="iconType" :load-text="loadText" />
		</view>
		<view class="flex justify-center flex-wrap" v-if="huodongList.length==0">
			<image src="../../static/images/img/empty.png" style="width: 387rpx;height: 341rpx;margin-top: 100rpx;" mode=""></image>
			<view class="" style="width: 100%;text-align: center;margin-top: 20rpx;">
				暂无数据
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				myhd:[],
				page: 1,
				limit: 10,
				huodongList: [],
				total: 1,
				status: 'loadmore',
				iconType: 'flower',
				activityId:'',
				loadText: {
					loadmore: '上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
			};
		},
		onLoad() {
			//我参与的活动
			this.getMyHuoDong()
		},
		onShow() {
			//我参与的活动
			this.getMyHuoDong()
		},
		//加载更多
		onReachBottom() {
			if(this.page < this.total){
				this.page += 1
				this.status = 'loading'
				//我参与的活动
				this.getMyHuoDong()
			}
		},
		//下拉刷新
		onPullDownRefresh() {
			this.page = 1
			this.getMyHuoDong()
		},
		methods: {
			//加入活动
			joinHd(id,index) {
				uni.navigateTo({
					url:'./join_huodong?activityId='+id+'&shunxu='+index
				})
			},
			//退出活动
			quitHuo(row){
				uni.showModal({
					title: '提示',
					content: '确定退出该活动?',
					success: (res) => {
						if(res.confirm) {  
							let data = {
								"shopId":uni.getStorageSync('shopId'),
								"activityId": row
							}
							this.$Request.put("/admin/activity-shop/quit", data).then(res => {
								if(res.code==0){
									uni.showToast({
										title: '操作成功',
										icon: 'success'
									})
									this.getMyHuoDong()
								}else{
									uni.showModal({
									  title: '提示',
									  content: res.msg,
									  success: function (res) {
									    if (res.confirm) {
									    } else if (res.cancel) {
									    }
									  }
									});
								}
							});
						} else {  
						}  
					}
				})
			},
			//选择商品
			selectShang(activityShopId){
				uni.navigateTo({
					url:'./select_shang?actShopid='+activityShopId
				})
			},
			//跳转到活动详情
			gotoinfo(id){
				uni.navigateTo({
					url:'./hdInfo?activityId='+id
				})
			},
			// 我参与的活动
			getMyHuoDong() {
				var shopId=uni.getStorageSync('shopId')
				this.$Request.get(`/admin/activity-shop/shop-activity-list/${shopId}`).then(res => {
					if (res.code == 0) {
						let returnData = res.data
						// this.activityId = res.data.activityId
						if(res.data.length>0){
							this.myhd = res.data[0].activityImage.split(',')
						}
						//活动列表
						this.getHuoDongList(returnData)
					}
				});
			},
			getHuoDongList(returnData) {
				// this.huodongList = []
				let data = {
					page: this.page,
					limit: this.limit
				}
				this.$Request.get("/admin/activity/page", data).then(res => {
					uni.stopPullDownRefresh()
					if (res.code == 0) {
						let returnData2 = res.data
						const newList =returnData2.list.map(item =>{
						return{...item,quantity:0}
						return{...item,activityShopId:0}
						})
						var arr = []
						for(var j in returnData){
						newList.forEach(item => {
							if (item.id === returnData[j].activityId) {
							// 'status'为属性名，'非活动'为修改后的内容
							item.quantity = 1
							item.activityShopId = returnData[j].activityShopId
							}
						})
						}
						arr=newList
						// this.huodongData = arr
												
						// res.data.list.map(item=>{
						// 	if(item.id==14){
						// 		item.no = '是'
						// 	}
						// 	if(item.id==this.activityId){
						// 		item.se = '是'
						// 	}else{
						// 		item.se = '否'
						// 	}
						// })
						this.total = res.data.totalCount
						if (this.page == 1) {
							this.huodongList = arr
						} else {
							this.huodongList = [...this.huodongList, ...arr]
						}
						if(this.page == this.total){
							this.status = 'nomore'
						}else{
							this.status = 'loadmore'
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.myswiper {
		width: 100%;
		height: 340rpx;
		z-index: 0;
	}

	.list {
		width: 100%;
		height: 160rpx;
		

		.list-box {
			width: 686rpx;
			height: 100%;
			border-radius: 24rpx;
			background-color: #ffffff;
			margin-bottom: 20rpx;
			z-index: 999;
			.list-box-c {
				width: 646rpx;
				height: 100%;
			}

			.list-box-c-l {
				image {
					width: 100rpx;
					height: 100rpx;
					border-radius: 16rpx;
				}

				.list-box-c-l-txt {
					width: 50vw;
					height: 100rpx;
					margin-left: 20rpx;
					font-size: 26rpx;
					overflow: hidden;
					line-height: 100rpx;
				}
			}

			.list-box-c-r {
				// border: 1rpx solid #FCD202;
				background: #FCD202;
				padding: 10rpx 30rpx 10rpx 30rpx;
				// color: #ffffff;
				font-size: 26rpx;
				border-radius: 24rpx;

			}
			//退出
			.list-box-c-t{
				background: rgba(252, 126, 126, 1);
				padding: 10rpx 30rpx 10rpx 30rpx;
				// color: #ffffff;
				font-size: 26rpx;
				border-radius: 24rpx;
				margin-top: 5px;
				text-align: center;
			}
		}
	}
</style>
