<template>
	<view class="">
		<view class="listBlock1" v-for="(i,index) in classList" :key="index+'a'"  @click="gotoDetail" :data-aid="i.acId">
			<view class="img cell-img">
				<image :src="i.pic" mode="aspectFill"></image>
			</view>

			<view class="right">
				<view class="">
					<view class="title">
						<view class="tag bqZ-font">类型</view>

						<text class="btn-font" style="">
							{{i.acName}}
						</text>
					</view>
					<view class="introduce fwb-font">
						{{ i.brief }}
					</view>

				</view>

				<view class="operate">
					<view class="left">
						<view class="opr" style="padding-right: 25rpx" @click.stop="coll" :data-e="i">
							<view class="icon">
								<image :src="collArr.includes(i.acId) ? pic_coll2 : pic_coll "
									style="width: 28rpx; height: 26rpx" mode="aspectFill"></image>
							</view>
							<text class="fz-font">收藏</text>
						</view>
						<view class="opr" @click.stop="subaaa" :data-e="i">
							<view class="icon">
								<image :src="subArr.includes(i.acId) ? pic_sub2 : pic_sub "
									style="width: 22rpx; height: 26rpx" mode="aspectFill"></image>
							</view>
							<text class="fz-font">订阅</text>
						</view>
					</view>
					<view class="">
						<button class="btn sBtn-font"  @click="join" :data-index="index">
							立即参与
						</button>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		name: "listBlock1",
		props: {
			classList: {
				type: Array,
				default () {
					return [];
				},
			},
			indexId: {
				type: Number,
			},
		},
		// props:['classList'],
		data() {
			return {
				collArr: [],
				subArr: [],
				pic_coll: 'https://teacherapi.cocorobo.cn/teaching-file/static/yym/Star 1 (Stroke).png',
				pic_coll2: 'https://teacherapi.cocorobo.cn/teaching-file/static//mine/Collect_yellow.png',
				pic_sub: 'https://teacherapi.cocorobo.cn/teaching-file/static/yym/Vector (Stroke).png',
				pic_sub2: '../../static/mine/dingyue_blue.png'
			};
		},

		methods: {
			// 收藏事件
			coll(event) {
				const value = this.$store.state.user.openid;
				if (value == '') {
					uni.navigateTo({
						url: "/pages/login_Wechat/login_Wechat",
					});
					return
				}
				// return	console.log(event);
				let aId = event.currentTarget.dataset.e.acId

				//判断是否收藏
				let iscoll = null

				// 查询是否收藏过
				let data = {
					acid: aId,
					oid: this.$store.state.user.openid, //用户id
					ty: 0
				}
				this.$request('/selectOneOperator', 'POST', data).then(res => {
					// console.log('查询是否收藏过',res);
					res[0].length ? iscoll = true : iscoll = false;

					// iscoll为true表示已经收藏过，执行删除  为true则收藏
					if (iscoll) {
						console.log('执行删除');
						this.$request('/deleteOperator', 'POST', data).then(res => {
							console.log(res);
							this.getdata()
						})
					} else {
						console.log('执行添加');
						this.$request('/insertOperator', 'POST', data).then(res => {
							console.log(res);
							this.getdata()
						})
					}
				})
			},

			// 订阅事件
			subaaa(event) {
				const value = this.$store.state.user.openid;
				if (value == '') {
					uni.navigateTo({
						url: "/pages/login_Wechat/login_Wechat",
					});
					return
				}
				let aId = event.currentTarget.dataset.e.acId

				//判断是否订阅
				let iscoll = null

				// 查询是否订阅过
				let data = {
					acid: aId,
					oid: this.$store.state.user.openid, //用户id
					ty: 1
				}
				this.$request('/selectOneOperator', 'POST', data).then(res => {
					// console.log('查询是否订阅过',res);
					res[0].length ? iscoll = true : iscoll = false;

					// iscoll为true表示已经订阅过，执行删除  为true则订阅
					if (iscoll) {
						console.log('执行删除');
						this.$request('/deleteOperator', 'POST', data).then(res => {
							console.log(res);
							this.getSub()
						})
					} else {
						console.log('执行添加');
						this.$request('/insertOperator', 'POST', data).then(res => {
							console.log(res);
							this.getSub()
						})
					}
				})
			},

			join(e) {
				let num = e.currentTarget.dataset['index'];

				const value = this.$store.state.user.openid;
				if (value == '') {
					uni.navigateTo({
						url: "/pages/login_Wechat/login_Wechat",
					});
					return 1;
				} else {
					//推荐课程
					const urls = [
						"https://szptxnjys.yuketang.cn/pro/portal/about/szpt-99Q00351166273N",
						"https://szptxnjys.yuketang.cn/pro/portal/about/szpt-99Q00330324841N",
						"https://szptxnjys.yuketang.cn/pro/portal/about/szpt-99Q00231455802N",
						"https://szptxnjys.yuketang.cn/pro/portal/about/szpt-99Q00406339964N",
					];
					if (num > 3) return uni.showToast({
						title: '暂时无第三方链接',
						icon: 'none'
					})

					uni.navigateTo({
						url: "/pages/skipone/skipone?url=" + encodeURIComponent(urls[num]),
					});
				}
			},
			// 跳转到详情页
			gotoDetail(e){
				let aid = e.currentTarget.dataset.aid;
				const value = this.$store.state.user.openid;
				if (value == '') return this.goLog()
				
				uni.navigateTo({
					url: `/pages/teachingDetail/teachingDetail?acId=${aid}`
				});
			},
			// 获取收藏事件
			getdata() {
				// console.log(this.$store.state.user.openid);
				let data = {
					oid: this.$store.state.user.openid, //用户id
					type: 0
				}
				this.$request('/selectOperator', 'POST', data).then(res => {
					// console.log('获取收藏', res);
					// 每次调用前清零，防止push叠加错误
					this.collArr = []
					// 将acid遍历到collArr中
					res[0].forEach(i => {
						this.collArr.push(i.acId)
					})

				})
			},
			// 获取订阅事件
			getSub() {

				// console.log(this.$store.state.user.openid);
				let data = {
					oid: this.$store.state.user.openid, //用户id
					type: 1
				}
				this.$request('/selectOperator', 'POST', data).then(res => {
					// console.log('获取订阅', res);
					// 每次调用前清零，防止push叠加错误
					this.subArr = []
					// 将acid遍历到collArr中
					res[0].forEach(i => {
						this.subArr.push(i.acId)
					})

				})
			}
		},

	}
</script>

<style lang="scss">
	.listBlock1 {
		display: flex;
		justify-content: space-between;
		// margin-bottom: 10px;
		background-color: #ffffff;
		padding: 20rpx 30rpx;

		.img {
			width: 160rpx;
			height: 160rpx;

			image {
				width: 100%;
				height: 100%;
				border-radius: 10rpx;
			}
		}

		.right {
			padding-left: 20rpx;
			flex: 1;
			// height: 300px;
			width: 100rpx;
			display: flex;
			flex-direction: column;
			// align-content: flex-end ;
			justify-content: space-between;
			// align-content: space-between;

			.title {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				margin-bottom: 15rpx;

				// margin-bottom: 10rpx;


				text {
					font-weight: bold;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}

			.introduce {
				// font-size: 28rpx;
				color: #a7a7a7;
				// padding: 20rpx 0;
				// width: 560rpx;
				height: 40rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.operate {
				display: flex;
				justify-content: space-between;
				// align-items: baseline;
				align-items: flex-end;

				.left {
					display: flex;
					// justify-content: space-between;
					// align-items: center;
					align-items: baseline;

					// width: 200rpx;
					.opr {
						display: flex;
						align-items: center;

						.icon {
							width: 32rpx;
							height: 32rpx;
							display: flex;
							justify-content: center;
							align-items: center;
						}

						text {
							padding-left: 3rpx;
							color: #666666;
						}
					}




				}

				.btn {
					z-index: 5;
					border: none;
					padding: 0;
					text-align: center;
					// padding: 8px, 24px, 8px, 24px;
					background-color: rgba(48, 129, 232, 1);
					color: #fff;
					border-radius: 100rpx;
					float: right;
					width: 144rpx;
					height: 48rpx;
					line-height: 48rpx;
					letter-spacing: 1px;
				}
			}
		}

		.tag {
			border: 1px #00b2b6 solid;
			flex-shrink: 0;
			letter-spacing: 2rpx;
			width: 64rpx;
			height: 32rpx;
			display: flex;
			white-space: nowrap;
			justify-content: center;
			align-items: center;
			border-radius: 4rpx;
			margin-right: 10rpx;
			color: #00b2b6;
			line-height: 32rpx;
		}
	}
</style>