<template>
	<view class="content">
		<view class="one" v-if="total>0">
			<up-list @scrolltolower="scrolltolower">
				<up-list-item v-for="(item, index) in list" :key="index">
					<view class="list">
						<view class="value">
							<view class="title">问题类型：</view>
							<view class="label">{{getDict(item.type,'type')}}</view>
						</view>
						<view class="value">
							<view class="title">问题描述：</view>
							<view class="label">{{item.brief||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">时间：</view>
							<view class="label">{{item.time||'暂无'}}</view>
						</view>
						<view class="value">
							<view class="title">状态：</view>
							<view class="label" :class="[item.status=='0'?'red':'']">{{getDict(item.status,'status')}}
							</view>
						</view>
					</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 = inject('$config');
	// 基本信息
	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 typeList = ref([]);
	const statusList = ref([]);
	// user
	const user = computed(() => {
		return uni.getStorageSync('user');
	})
	onShow(async () => {
		await searchConfig();
		await searchOther();
		await search();
	})
	onPullDownRefresh(async () => {
		await clearPage();
		await search();
		uni.stopPullDownRefresh();
	})
	// config信息
	const searchConfig = async () => {
		config.value = uni.getStorageSync('config');
	};
	// 其他查询信息
	const searchOther = async () => {
		let res;
		// 问题类型
		res = await $api(`dictData`, 'GET', { code: 'opinionType', is_use: '0' });
		if (res.errcode === 0) typeList.value = res.data;
		// 问题状态
		res = await $api(`dictData`, 'GET', { code: 'opinionStatus', is_use: '0' });
		if (res.errcode === 0) statusList.value = res.data;
	};
	// 查询
	const search = async () => {
		const info = {
			skip: skip.value,
			limit: limit.value,
			user: user.value._id
		}
		const res = await $api('opinion', '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 getDict = (data, model) => {
		let res
		if (model == 'status') res = statusList.value.find((f) => f.value == data)
		else if (model == 'type') res = typeList.value.find((f) => f.value == data)
		return res.label || '暂无'
	}
	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 {
			margin: 0 2vw;
			border-radius: 5px;

			.list {
				background-color: var(--mainColor);
				border-bottom: 1px solid var(--f5Color);
				margin: 2vw 0 0 0;
				border-radius: 4px;
				padding: 2vw;

				.value {
					display: flex;
					padding: 1vw 2vw;

					.title {
						font-size: var(--font16Size);
						font-weight: bold;
						margin: 0 2vw 0 0;
					}

					.label {
						width: 70%;
						font-size: var(--font14Size);
						color: var(--f85Color);
					}

					.red {
						color: var(--ff0Color);
					}
				}
			}

		}

		.is_bottom {
			width: 100%;
			text-align: center;

			text {
				padding: 2vw 0;
				display: inline-block;
				color: var(--f85Color);
				font-size: var(--font12Size);
			}
		}
	}
</style>