<template>
	<view class="container">

		<!-- 设置圆角 -->
		<view style="padding: 10px 30px 10px 30px;">
			<uni-search-bar :radius="100" @input="search" placeholder="搜索考试"></uni-search-bar>
		</view>


		<view style="background: #f5f5f5">
			<uni-segmented-control :current="current" :values="tabs" @clickItem="changeTab" styleType="text"
				activeColor="#007aff">
			</uni-segmented-control>
		</view>


		<yf-more-list url="/api/exam/exam/online-paging" :params="queryParams" ref="moreList">

			<template v-slot:item="{ data }">
				<!-- 				link="navigateTo"
				:to="'/pages/exam/detail?id='+data.id" -->
				 <!-- :clickable="true" @click="funJump(data)" -->
				<uni-list-item :show-arrow="true" :title="data.title"
					:note="'关联培训：'+ (data.associate === 1? '是': '否') +' \n 发布时间：'+data.createTime"
					:rightText="data.timeLimit?'限时': '不限时'" :to="'/pages/exam/detail?id='+data.id" link="navigateTo">
				</uni-list-item>
			</template>
		</yf-more-list>


	</view>
</template>

<script>
	import {
		checkProcess
	} from '@/api/exam.js'
	export default {
		data() {
			return {
				current: 0,
				tabs: ['全部', '考试中', '未开始', '已结束'],
				queryParams: {
					title: '',
					examState: ''
				}
			}
		},
		onPullDownRefresh() {
			// 加载数据
			this.$refs.moreList.loadData()
			this.checkExam()
		},

		onReachBottom() {
			// 加载数据
			this.$refs.moreList.loadData()
		},
		onShow() {
			if (this.$refs.moreList) {
				this.$refs.moreList.initData()
			}
			this.checkExam()
		},

		methods: {
			funJump(obj) {
				if (obj.associate === 1) {
					uni.showModal({
						cancelText: "取消", // 取消按钮的文字  
						confirmText: "确认", // 确认按钮文字 
						title: '培训提示',
						content: '该考试已关联培训，是否跳转培训?',
						success: res => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/course/detail?id=' + obj.courseId
								})
							}else{
								uni.navigateTo({
									url: '/pages/exam/detail?id=' + obj.id
								})
							}
						}
					});
				} else {
					uni.navigateTo({
						url: '/pages/exam/detail?id=' + obj.id
					})
				}
			},

			checkExam() {

				checkProcess().then(data => {
					if (data && data.id) {

						uni.showModal({
							cancelText: "取消", // 取消按钮的文字  
							confirmText: "确认", // 确认按钮文字 
							title: '删除提示',
							content: '您有正在进行的考试，是否继续考试?',
							success: res => {
								if (res.confirm) {
									this.continueExam(data.id)
								}
							}
						});

					}
				})

			},

			// 考试详情页
			search(e) {
				this.queryParams.title = e
			},

			changeTab(e) {

				if (this.current != e.currentIndex) {
					this.current = e.currentIndex;
				}

				let state = null;
				if (e.currentIndex != 0) {
					state = e.currentIndex - 1
				}

				this.queryParams.examState = state
			},

			// 继续考试
			continueExam(id) {
				uni.redirectTo({
					url: '/pages/exam/exam?id=' + id
				});
			},
		}
	}
</script>

<style>
	.container {
		font-size: 14rpx;
	}

	.exam-item {
		display: flex;
		align-items: center;
		line-height: 25px;
	}

	.exam-item .left {
		text-align: left;
		flex-grow: 1;
	}

	.exam-item .left .desc {
		color: #888;
	}

	.exam-item .left .icons {
		font-size: 10px;
		display: flex;
		align-items: center;
		color: #666;
	}

	.exam-item .right {
		text-align: right;
	}

	.pass {
		color: #007AFF !important;
		margin-right: 5px;
	}

	.fail {
		color: #ff0000 !important;
		margin-right: 5px;
	}
</style>
