<template>
	<RefreshView ref="mescrollRef" :hasBack="true" text="队伍报名" :useDownScroll="false" :useUpScroll="false">
		<view class="top">
			<text class="top_lift">选中({{page.selectedMemberAmount}})</text>
			<text class="top_right" @click="enroll()" >比赛报名</text>
		</view>
		<view class="line"></view>
		<view>
			<view v-for="(item,index) in page.listTeamMemberArea " :key="index">
				<view class="item">
					<view class="line" style="height: 1px;"></view>
					<view class="item_content">
						<checkbox-group class="item_check" @change="checkboxChange($event, index)">
							<checkbox :checked="item.checked"></checkbox>
						</checkbox-group>
						<image class="item_img" :src="item.headImage" mode="aspectFill"></image>
						<text class="item_text">{{item.realName}}</text>
					</view>
				</view>
			</view>
		</view>
	</RefreshView>
</template>
<script>
	export default {
		data() {
			return {
				data:{
					activityItemSid:"",    // 活动项目Sid
					teamSid:"",            // 队伍Sid
					listTeamMemberSid:[],  // 队伍成员Sid列表
					paymentMemberSid:""    // 付费会员Sid
				},
				page:{
					type: 1,               // 队伍类型1为我创建的
					listTeamMemberArea:[], // 会员块列表
					selectedMemberAmount:0 // 选中成员数
				}
			}
		},
		onLoad:function(options) {
			// 接收队伍Sid
			this.data.activityItemSid = options.activityItemSid
			// 判断是否登陆
			if (!getApp().globalData.isLogin){
				// 进入登陆页面
			}
			let sysUserSid = getApp().globalData.sysUserSid
			let _teamSid =""
			// 获取我的队伍，如果为一个队伍在当前页，如果多个队伍则进入选择界面
			
			let _this = this
			_this.HTTP({
				url: 'aos/v1/aosUser/getMyTeamAreaPagerList',
				paramsType: "JSON",
				method: "POST",
				data: {
					current: 1,
					size: 10,
					params: {
						sysUserSid: sysUserSid,
						type: 1
					}
				},
				loading: true
			})
			.then((res) => {
				// 未找到队伍返回
				if (!res.success){
					return
				}
				let listTeamArea = res.data.records
				if (listTeamArea.length > 1 ){
					console.log('队伍数大于1支跳转：', listTeamArea)
				}
				// 设置队伍Sid
				_this.data.teamSid = listTeamArea[0].sid
				console.log('最新的队伍Sid', _this.data.teamSid)
				// 获取列表
				_this.HTTP({
					url: 'aos/v1/aosUser/getTeamArea',
					paramsType: "FORM",
					method: "GET",
					data: {
						teamSid: _this.data.teamSid
					},
					toast: true,
					loading: true
				})
				.then((res) => {
					console.log('res', res)
					_this.page.listTeamMemberArea = res.data.listTeamMemberArea
				});
				
				
			}, (err) => {
				// 错误提示
				_this.Toast("出错了：" + err.data.errmsg)
			})
		},
		methods: {
			enroll() {
				// 保存
				let _this = this
				console.log('=====', _this)
				let _activityItemSid = this.data.activityItemSid
				
				let _teamSid = _this.data.teamSid
				let _listTeamMemberSid = _this.data.listTeamMemberSid
				_this.HTTP({
					url: 'aos/v1/activityItem/teamEnroll',
					paramsType: "JSON",
					method: "POST",
					data: {
						'activityItemSid':_activityItemSid,
						'teamSid': _teamSid,
						'listTeamMemberSid': _listTeamMemberSid
					},
					loading: true
				})
				.then((res) => {
					uni.navigateBack({
						delta: 1
					})
				});
			},
			checkboxChange(e, index) {
				// 切换选择
				this.page.listTeamMemberArea[index].checked = !this.page.listTeamMemberArea[index].checked
				console.log("listTeamMemberArea" + JSON.stringify(this.page.listTeamMemberArea[0]))
				// 勾选后添加Sid，取消勾选后删除
				if (this.page.listTeamMemberArea[index].checked) {
					this.data.listTeamMemberSid.push(this.page.listTeamMemberArea[index].sid)
				} 
				else {
					// 删除当前的Sid
					let sid = this.page.listTeamMemberArea[index].sid
					let pos = this.data.listTeamMemberSid.findIndex((x) => {
						return x == sid
					})
					this.data.listTeamMemberSid.splice(pos, 1)
				}
				this.page.selectedMemberAmount = this.data.listTeamMemberSid.length
			}

		}
	}
</script>

<style lang="scss">
	@import url("../../static/master.css");
	.line {
		height: 10rpx;
		background: #eee;
		width: 100%;
	}

	.top {
		display: flex;
		background: #fff;
		flex-direction: row;
		width: 100%;
		padding-top: 26rpx;
		padding-bottom: 26rpx;

		.top_lift {
			margin-left: 30rpx;
			font-weight: 550;
			font-family: sans-serif;
			flex: 1;
			color: #101010;
			font-size: 36rpx;
		}

		.top_right {
			margin-right: 30rpx;
			background-color: #F4CE98;
			color: #fff;
			font-size: 24rpx;
			padding: 10rpx 20rpx;
			text-align: right;
		}
	}

	.item {

		display: flex;
		flex-direction: column;

		.item_content {
			display: flex;
			align-items: center;
			background-color: #fff;
			padding: 20rpx 32rpx;
			flex-direction: row;

			.item_check {
				width: 32rpx;
				height: 32rpx;
			}

			.item_img {
				margin-left: 40rpx;
				width: 80rpx;
				height: 80rpx;
				border-radius: 10rpx;
			}

			.item_text {
				font-size: 28rpx;
				color: #101010;
				margin-left: 40rpx;
			}
		}

	}
</style>
