<template>
	<view class="teaching_case">
		<view class="cont" @click="gotoWeb" v-for="(item, index) in acList" :key="index"
			:data-kid="item.href">
			<image class="star" :src="collArr.includes(item.cid) ? pic_coll2 : pic_coll " @click.stop="coll" :data-e="item"
				mode="aspectFill"></image>
			<image class="img" :src="item.img" mode="aspectFill" />
			<view class="tit btn-font">{{ item.ctit }}</view>
			<view class="teacher fz-font">{{ item.school }}</view>
			<view class="mon" v-if="indexId">
				￥<text class="cost"></text>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		name: "teaching-case",
		props: {
			acList: {
				type: Array,
				default () {
					return [];
				},
			},
			indexId: {
				type: Number,
				default () {
					return 0;
				},
			},
		},
		data() {
			return {
				// 收藏列表
				collArr: [],
				// 收藏星星
				pic_coll: 'https://teacherapi.cocorobo.cn/teaching-file/static/Stara.png',
				pic_coll2: 'https://teacherapi.cocorobo.cn/teaching-file/static/mine/Collect_yellow.png',
			};
		},
		methods: {
			// 收藏事件
			coll(event) {
				const value = this.$store.state.user.openid;
				if(!value){
					uni.showToast({title: '未登录，请登录',icon: 'none'})
					setTimeout(this.goLog,1000)
					return
				}
				
				// return	console.log(event);
				let cId = event.currentTarget.dataset.e.cid

				// let type= event.currentTarget.dataset.e.type

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

				// 查询是否收藏过
				let data = {
					cid: cId,
					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 => {
							this.getdata()
						})
					} else {
						console.log('执行添加');
						this.$request('/insertOperator', 'POST', data).then(res => {
							this.getdata()
						})
					}
				})
			},

			// 收藏
			// coll() {
			// 	uni.showToast({
			// 		title:'暂时无法收藏哦',
			// 		icon:"none"
			// 	})
			// },
			// 查看课程内容
			gotoWeb(e) {
				const value = this.$store.state.user.openid;
				if(!value){
					uni.showToast({title: '未登录，请登录',icon: 'none'})
					setTimeout(this.goLog,1000)
					return
				}
				
				// return console.log('aaaaaa', e.currentTarget.dataset);
				let acid = e.currentTarget.dataset.kid
				uni.navigateTo({
					url: "/pages/skipone/skipone?url=" + encodeURIComponent(acid),
				});

			},
			// 获取课程收藏事件
			getdata() {
				const value = this.$store.state.user.openid;
				if(!value) return this.collArr = []
				
				
				// 触发收藏事件
				// console.log('触发收藏事件');
				let data = {
					oid: this.$store.state.user.openid, //用户id
					type: 1
				}
				this.$request('/selectCollClassList', 'POST', data).then(res => {
					console.log('获取收藏', res);
					// 每次调用前清零，防止push叠加错误
					this.collArr = []
					// 将acid遍历到collArr中
					res[0].forEach(i => {
						this.collArr.push(i.cid)
					})
					// console.log(this.collArr);
				})
			},
			goLog() {
				uni.navigateTo({
					url: "/pages/login_Wechat/login_Wechat",
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.teaching_case {
		width: 100%;
		padding: 10px 30rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		// margin-bottom: 50px;

		// background-color: #000;
		.cont {
			background-color: #ffffff;
			width: 332rpx;
			padding: 20rpx 15rpx;
			border-radius: 10px;
			margin-bottom: 20rpx;
			position: relative;

			.star {
				position: absolute;
				right: 30rpx;
				top: 30rpx;
				width: 30rpx;
				height: 30rpx;
			}

			.img {
				width: 300rpx;
				height: 170rpx;
				border-radius: 10rpx;
			}

			.tit {
				width: 257rpx;
				margin: 10rpx 0;
				font-weight: bold;
				white-space: normal;
				display: -webkit-box;
				word-break: break-all;
				text-overflow: ellipsis;
				overflow: hidden;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				/*设置 需要显示的行数*/
			}

			.teacher {
				color: #999999;
			}

			.mon {
				font-size: 34rpx;
				color: rgba(246, 135, 23, 1);
				font-weight: 34rpx;
				margin-top: 10rpx;

				.cost {
					font-weight: 40rpx;
				}
			}
		}

		// }
	}
</style>