<template>
	<view class="phone-list-page">
		<view class="icon-title-bjcolor-navBar-box">
			<view @click="goUpPage" class="nav-bar-icon"></view>
			<text class="nav-bar-title">练习管理</text>
		</view>
		<!-- 考试列表 -->
		<scroll-view scroll-y="true" refresher-enabled="true" :refresher-triggered="data.loading"
			:refresher-threshold="50" refresher-background="transparent" @refresherrefresh="onRefresh" @scrolltolower="onScrolltolower"
			class="phone-scroll-view">
			<uni-list class="admin-list-box">
				<uni-list-item v-for="item in data.list" class="admin-list-item-box">
					<template v-slot:body>
						<!-- 考试项 -->
						<view class="item-card-row">
							<!-- 考试名 + 等级 -->
							<view class="ks-item-top">
								<view class="ks-name">{{item.zyName}}</view>
								<view class="ks-zyLevelName">{{item.zyLevelName}}</view>
							</view>
							<!-- 时间 -->
							<!-- <view class="ks-totalTm">
									<icon class="phone-time-icon" />时间：{{item.totalTm}}分钟
								</view> -->
							<!-- 分数 -->
							<view class="ks-score-content">
								<view class="ks-score">
									<icon class="phone-zongfen-icon" />总分：<text>{{item.ksScore}}</text>
								</view>
								<view class="ks-okScore">
									<icon class="phone-jigefen-icon" />及格分：<text>{{item.okScore}}</text>
								</view>
							</view>
							<button type="primary" size="mini" @click="checkKsXz(item)"
								class="item-view-btn">进行练习</button>
						</view>
					</template>
				</uni-list-item>
				<uni-load-more :status="data.state" @click="getMore(0)" :contentText="data.contentText"></uni-load-more>
			</uni-list>
		</scroll-view>
		<!-- 页面底端 -->
		<customTabbarClientVue></customTabbarClientVue>
		<!-- 考试须知 -->
		<lianxixuzhi ref="lxxzRef" @confirm="handleConfirmKs"></lianxixuzhi>
	</view>
</template>

<script setup>
	import * as lianxiApi from "@/api/lianxi.js";
	import customTabbarClientVue from "@/components/custom-tabbar/custom-tabbar-client.vue";
	import lianxixuzhi from "@/components/kaoshixuzhi/lianxixuzhi.vue";
	import {
		ref,
		reactive
	} from "vue";
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app";

	const lxxzRef = ref(null);

	const data = reactive({
		zyName: '', // 职业名称
		list: [], // 考试列表
		loading: false,
		page: 0,
		size: 8,
		state: 'more',
		contentText: {
			contentdown: '查看更多',
			contentrefresh: '加载中',
			contentnomore: '没有更多'
		},
		from: ''
	})

	function onScrolltolower() {
		getMore()
	}

	function checkKsXz(data) {
		lxxzRef.value.showDialog(data)
	}

	function handleConfirmKs(data) {
		checkKaoshi(data)
	}

	function goUpPage() {
		const pages = getCurrentPages();
		if (pages.length > 1) {
			uni.navigateBack()
		} else {
			/* uni.redirectTo({
				url: '/pages/client/ShouYe/shouye'
			}) */
			history.back();
		}

	}

	function handleSearch() {
		data.page = 0;
		refreshData();
	}

	function checkKaoshi(item) {
		uni.navigateTo({
			url: `/pages/client/Lianxi/exam?lxId=${item.lxId}&from=lianxiList`
		})
	}

	function onRefresh() {
		data.page = 0;
		data.list = [];
		data.loading = true;
		refreshData();
	}

	function refreshData() {
		const opt = {
			page: 1,
			size: data.size, // 固定查询10条
			zyName: data.zyName
		}
		data.list = [];
		// 数学
		data.state = 'loading';
		data.page++;
		opt.page = data.page;

		lianxiApi.getClientLianxiList(opt).then(res => {
			data.list = data.list.concat(res.data.data);
			data.loading = false;

			if (res.data.total > data.list.length) {
				data.state = 'more';
				data.loading = false;
			} else {
				data.state = 'no-more';
				data.loading = false;

			}
		}).catch(err => {
			data.state = 'more';
			data.loading = false;
		})
	}

	function getMore() {
		const opt = {
			page: 1,
			size: data.size, // 固定查询10条
			zyName: data.zyName
		}
		if (data.state == 'no-more') return;
		data.state = 'loading';
		data.page++;
		opt.page = data.page;
		lianxiApi.getClientLianxiList(opt).then(res => {
			data.list = data.list.concat(res.data.data);
			data.loading = false;

			if (res.data.total > data.list.length) {
				data.state = 'more';
				data.loading = false;
			} else {
				data.state = 'no-more';
				data.loading = false;
			}
		}).catch(err => {
			data.state = 'more';
			data.loading = false;
		})
	}

	onLoad((options) => {
		data.from = options.from;
	})

	onShow(() => {
		getMore()
	})
</script>

<style>

</style>