<template>
	<view class="content">
		<u-sticky offsetTop="0" customNavHeight="0">
			<view class="tab-bar">
				<view v-for="(item, index) in tabs" :key="index" class="tab-item"
					:class="{active: tabIndex === item.value}" @click="tabChange(item)">
					{{ item.name }}
				</view>
			</view>
		</u-sticky>
		<view class="item_content">
			<view class="myjt-box" v-if="dataList.length>0">
				<view class="collection-list">
					<view class="item" v-for="(item,index) in dataList" :key="index" @tap="detail(item)">
						<view class="top">
							<view class="img-box">
								<image :src="item.imgUrl" mode="aspectFill"></image>
								<view class="playIcon">
									<image src="../../static/image/course/play_icon.png"></image>
								</view>
								<view v-if="item.isIntegral!=1" class="vip">VIP</view>
							</view>
							<view class="info-box">
								<view class="title ellipsis2">{{item.courseName}}</view>
								<view class="tips ellipsis2">{{item.videoName}}</view>
								<view class="time es-fs-24 es-c-66">{{ $formatDate(item.studyTime) }}</view>
							</view>
							<view class="arrow">
								<image src="/static/images/more_icon_12.png" mode=""></image>
							</view>
						</view>
						<view class="progress-bar">
							<view class="focus-bar" :style="{width:getProgress(item)+'%'}"></view>
							<view class="number">{{ getProgress(item)+'%' }}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="u-f-ajc flex-1" v-else>
				<u-empty class="u-f-ajc" mode="data"></u-empty>
			</view>
		</view>
	</view>

</template>

<script>
	import {
		getAppCourseList,
		getLinkData
	} from '@/api/course'

	export default {
		data() {
			return {
				tabs: [{
					name: '待看课',
					value: 3,
				}, {
					name: '已完课',
					value: 2,
				}],
				tabIndex: 3,
				dataList: []
			}
		},
		onShow() {
			this.upCallback()
		},
		methods: {
			tabChange(e) {
				this.tabIndex = e.value;
				this.upCallback()
			},
			async detail(e) {
				const res = await getLinkData({
					linkId: e.linkId
				})
				if (res.code == 200) {
					uni.navigateTo({
						url: '/pages_im/pages/common/webH5/index?url=' + res.data.url
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			},
			upCallback(page) {
				//联网加载数据
				let that = this;
				let params = {
					logType: this.tabIndex
				};
				getAppCourseList(params, 1).then(res => {
					if (res.code == 200) {
						that.dataList = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: "请求失败",
						});
						that.dataList = null;
					}
				});
			},
			getProgress(item) {
				if (!item.videoDuration || !item.duration) {
					return 0
				}
				let value = (item.duration / item.videoDuration * 1.0 * 100).toFixed(2);
				return value;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.tab-bar {
		display: flex;
		background: #fff;
		border-bottom: 1px solid #eee;
	}

	.tab-item {
		flex: 1;
		text-align: center;
		padding: 24rpx 0;
		font-size: 30rpx;
		color: #666;
		border-bottom: 4rpx solid transparent;
	}

	.tab-item.active {
		color: #007AFF;
		border-bottom-color: #007AFF;
		font-weight: 500;
	}

	.myjt-box {
		margin: 0 20rpx;
		margin-top: 20rpx;
		border-radius: 20rpx;
		padding-right: 0rpx;
		background-color: #fff;

		.title-inner {
			margin-left: 20rpx;
			height: 60rpx;
			line-height: 80rpx;
			font-size: 28rpx;
			color: #666;
		}

		.collection-list {
			display: flex;
			flex-wrap: wrap;
			border-radius: 8rpx;
			margin: 0 16rpx;
			margin-top: 20rpx;

			.item {
				display: flex;
				flex-direction: column;
				// align-items: center;
				justify-content: center;
				width: calc(100%);
				margin-bottom: 20rpx;

				.top {
					display: flex;
					flex: 1;
					flex-direction: row;

					.img-box {
						position: relative;
						width: 40%;
						/* 或者任何你想要的宽度 */
						padding-bottom: 25%;
						/* 高度等于宽度 */
						overflow: hidden;

						/* 防止图片溢出 */
						image {
							position: absolute;
							width: 100%;
							height: 100%;
							top: 50%;
							left: 50%;
							transform: translate(-50%, -50%);
							border-radius: 20rpx;
						}
					}

					.playIcon {
						position: absolute;
						width: 50rpx;
						height: 50rpx;
						background: rgba(0, 0, 0, 0.28);
						border-radius: 50%;
						top: 50%;
						left: 50%;
						transform: translate(-50%, -50%);

						image {
							width: 20rpx;
							height: 24rpx;
							border-radius: 0 !important;
						}
					}

					.vip {
						position: absolute;
						width: 49rpx;
						height: 28rpx;
						background: linear-gradient(94deg, #FFE9D0 0%, #E9BD97 100%);
						border-radius: 6rpx;
						top: 16rpx;
						right: 16rpx;
						color: #333;
						font-size: 18rpx;
						text-align: center;
						font-weight: 500;
						font-style: italic;
					}
				}

				.info-box {
					width: calc(60%);
					padding: 0 20rpx;
					padding-right: 0rpx;
					position: relative;

					.title {
						font-size: 26rpx;
						font-family: PingFang SC;
						font-weight: 470;
						color: #333;
						line-height: 40rpx;
						margin-top: 10rpx;
					}

					.tips,
					.time {
						font-size: 26rpx;
						color: #666;
						line-height: 40rpx;
						margin-top: 10rpx;
					}

					.time {
						position: absolute;
						left: 20rpx;
						// bottom: 20rpx;
					}
				}

				.arrow {
					width: 30rpx;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-right: 10rpx;

					image {
						width: 26rpx;
						height: 22rpx;
					}
				}

			}

			.progress-bar {
				border-radius: 20rpx;
				background: #ebebeb;
				height: 20rpx;
				margin: 20rpx 0rpx;
				display: flex;
				flex-direction: row;
				align-items: center;

				.focus-bar {
					background: #FF233C;
					border-radius: 20rpx;
					height: 100%;

				}

				.number {
					font-size: 22rpx;
					color: #fff;
					background: #FF233C;
					border-radius: 10rpx;
					border-left: 2rpx solid #fff;
					padding: 0rpx 12rpx;
					margin-left: -2rpx;
				}
			}
		}
	}
</style>