<template>
	<view class="content">
		<view class="one">
			<view class="one_1">
				<input type="text" v-model="name" @input="toInput" placeholder="搜索课程名称">
			</view>
			<view class="one_2">
				<button size="mini" class="button" type="primary" @click="toAdd">添加</button>
			</view>
		</view>
		<view class="two" v-if="total>0">
			<up-list @scrolltolower="scrolltolower">
				<up-list-item v-for="(item, index) in list" :key="index">
					<view class="list">
						1111
					</view>
				</up-list-item>
			</up-list>
		</view>
		<up-empty v-else mode="list" icon="/static/list.png">
		</up-empty>
		<view class="is_bottom" v-if="is_bottom">
			<text>{{config.bottom_title||'没有更多了！'}}</text>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { inject, computed, ref } from 'vue';
	//该依赖已内置不需要单独安装
	import { onShow, onPullDownRefresh } from "@dcloudio/uni-app";
	// 请求接口
	const $api = inject('$api');
	// 基本信息
	const config = ref({ logo: [], file: [] });
	// 列表
	const list = ref([]);
	const total = ref(0);
	const skip = ref(0);
	const limit = ref(5);
	const page = ref(0);
	// 数据是否触底
	const is_bottom = ref(false);
	const name = ref('');
	// user
	const user = computed(() => {
		return uni.getStorageSync('user');
	})
	onShow(async () => {
		await searchConfig();
		await searchOther();
		await clearPage();
		await search();
	})
	onPullDownRefresh(async () => {
		await clearPage();
		await search();
		uni.stopPullDownRefresh();
	})
	// config信息
	const searchConfig = async () => {
		config.value = uni.getStorageSync('config');
	};
	// 其他查询信息
	const searchOther = async () => { };
	// 查询
	const search = async () => {
		const info : any = {
			skip: skip.value,
			limit: limit.value,
			teacher: user.value._id,
			is_show: '0'
		}
		if (name.value) info.name = name.value
		const res = await $api('course', 'GET', info);
		if (res.errcode === 0) {
			list.value = list.value.concat(res.data)
			total.value = res.total
		} else {
			uni.showToast({
				title: res.errmsg || '',
				icon: 'error',
			});
		}
	};
	// 搜索课程
	const toInput = async (e) => {
		await clearPage();
		await search();
	};
	// 新增课程
	const toAdd = async () => {
		uni.navigateTo({
			url: `/pagesMy/course/add`
		})
	};
	const scrolltolower = () => {
		if (total.value > list.value.length) {
			uni.showLoading({
				title: '加载中',
				mask: true
			})
			page.value = page.value + 1;
			skip.value = page.value * limit.value;
			search();
			uni.hideLoading();
		} else is_bottom.value = true
	};
	// 清空列表
	const clearPage = () => {
		list.value = []
		skip.value = 0
		limit.value = 6
		page.value = 0
	};
</script>
<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		background-color: var(--f1Color);

		.one {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vw;
			background-color: var(--mainColor);

			.one_1 {
				padding: 0 2vw;
				width: 75vw;

				input {
					padding: 2vw;
					background-color: var(--f1Color);
					font-size: var(--font14Size);
					border-radius: 5px;
				}
			}

			.button {
				background-color: var(--f3CColor);
				color: var(--mainColor);
			}
		}

		.one {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vw;

			.one_1 {
				padding: 0 2vw;
				width: 75vw;

				input {
					padding: 2vw;
					background-color: var(--f1Color);
					font-size: var(--font14Size);
					border-radius: 5px;
				}
			}

			.button {
				background-color: var(--3c9Color);
				color: var(--mainColor);
			}
		}

	}
</style>