<template>
	<view class="web_box">
		<!-- <u-navbar height="44" leftIcon="arrow-left" leftIconColor="#000" leftText="全部课程" @leftClick="leftClick" :autoBack="true" bgColor="#fff">
		</u-navbar> -->
		<view class="tabs hflex acenter">
			<u-tabs :list="tabs" :activeStyle="{color: '#2988FE',}" @click="changeTab" :scrollable="false"></u-tabs>
		</view>
		<view class="list" v-if="active == 0">
			<block v-for="(item,index) in course" :key="index">
				<view class="list_item hflex acenter" @click="toDetail(index)">
					<image :src="item.img" mode="widthFix" style="width: 322rpx;border-radius: 16rpx;"></image>
					<view class="img_right vflex">
						<view class="img_title">
							{{item.name}}
						</view>
						<view class="vflex ">
							<view class="hours">进度：{{item.study_class_count}}/{{item.class_count}}学时</view>
							<view class="progress">
								<u-line-progress class="line_progress" :percentage="item.progress" :showText="false" height="8" activeColor="#2988FE"></u-line-progress>
							</view>
						</view>
					</view>	
				</view>
			</block>
		</view>
		<view class="list" v-if="active == 1">
			<block v-for="(item,index) in course" :key="index">
				<view class="list_item hflex acenter" @click="toDetail(index)">
					<image :src="item.img" mode="widthFix" style="width: 322rpx;border-radius: 16rpx;"></image>
					<view class="img_right vflex">
						<view class="img_title">
							{{item.name}}
						</view>
						<view class="vflex ">
							<view class="hours">全部完成</view>
							<view class="progress">
								<u-line-progress class="line_progress" :percentage="100" :showText="false" height="8" activeColor="#2988FE"></u-line-progress>
							</view>
						</view>
					</view>	
				</view>
			</block>
		</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	export default {
		data() {
			return {
				tabs: [
					{
						name: '在修课程'
					},
					{
						name: '已修课程'
					}
				],
				active: 0,
				course: [],
				page: 1,
				limit: 10,
				total: 0,
				type: 1
			}
		},
		onLoad() {
			// var type = this.type
			// this.getCourse(type)
		},
		onShow() {
			var type = this.type
			this.getCourse(type)
		},
		methods: {
			leftClick() {
			},
			// 获取课程信息
			getCourse(type) {
				var that = this
				$api.req({
					url: '/api/User/myCourseList',
					data: {
						page: that.page,
						limit: that.limit,
						type: type
					}
				}, function(res) {
					console.log("我的课程1111",res);
					if (res.code == 1) {
						if(that.page == 1) {
							that.course = res.data.list
							that.total = res.data.count
						} else {
							that.course = that.course.concat(res.data.list)
						}
						that.totalProgress()
					} else {
						if (res.is_disable == 1) {
							$api.info(res.msg)
							uni.removeStorageSync('token')
							that.course = {}
							setTimeout(function(){
								$api.jump('/pages/login/login')
							},1000)
						} else if(res.is_login == 0) {
							$api.info('请先登录')
						}
					}
				})
			},
			// 切换tabs
			changeTab(e) {
				this.active = e.index
				this.course = []
				this.page = 0
				this.getCourse(this.active + 1)
			},
			// 计算进度条
			totalProgress() {
				var course = this.course
				for(var i = 0; i<course.length;i++) {
					course[i].progress = course[i].study_class_count / (course[i].class_count) * 100
				}
				console.log(course)
				this.course = course
			},
			// 触底事件
			onReachBottom() {
				console.log("到底了");
				console.log(this.page );
				console.log(this.limit);
				console.log(Number(this.total));
				if (Number(this.page) * Number(this.limit) >= Number(this.total)) {
					$api.info("没有更多了")
				} else {
					this.page++
					this.getCourse(this.type)
				}
			},
			// 进入课程详情
			toDetail(index) {
				$api.jump('/pages/index/course/detail?id=' + this.course[index].id)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.web_box::v-deep {
		
		.u-navbar {
			width: 100%;
			box-sizing: border-box;
			padding: 36px 16px 58rpx 0;
		}
		.tabs {
			width: 100%;
			box-sizing: border-box;
			padding: 12rpx 0;
			background: #FFFFFF;
		}
		.u-tabs {
			width: 100%;
		}
		.list {
			width: 100%;
			box-sizing: border-box;
			padding: 0 46rpx;
		}
		.list_item {
			width: 100%;
			box-sizing: border-box;
			padding: 12rpx 14rpx;
			background: #FFFFFF;
			box-shadow: 0px 2px 6px 0px rgba(213,213,213,0.5);
			border-radius: 24rpx;
			margin-top: 20rpx;
		}
		.img_right {
			width: calc(100% - 322rpx);
			padding-left: 18rpx;
		}
		.img_title {
			font-size: 26rpx;
			font-weight: 600;
			color: #333333;
			padding-bottom: 46rpx;
		}
		.hours {
			font-size: 22rpx;
			font-weight: 400;
			color: #333333;
		}
		.progress {
			width: 100%;
			height: 28rpx;
			padding: 16rpx 0 28rpx;
		}
		.line_progress {
			width: 100% !important;
			height: 16rpx !important;
		}
	}
</style>