<template>
	<view class="content">
		<view class="top">
			<u-search shape="square" :show-action="false" placeholder="品牌/车系" @focus="toChange"></u-search>
		</view>
		<view class="bottom">
			<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
				<view class="list-scroll-view">
					<view class="list" v-for="(item, index) in list" :key="index">
						<view class="name textOver">
							<text>{{item.brand||'暂无'}} {{item.bank||'暂无'}} {{item.type||'暂无'}}</text>
						</view>
						<view class="other textOver" v-if="item.start">
							<text>上牌日期：</text>
							<text>{{item.start||'暂无'}}</text>
						</view>
						<view class="other textOver" v-if="item.course">
							<text>行驶里程：</text>
							<text>{{item.course||'暂无'}}</text>
						</view>
						<view class="other textOver" v-if="item.city">
							<text>上牌城市：</text>
							<text>{{item.city||'暂无'}}</text>
						</view>
						<view class="other textOver" v-if="item.place">
							<text>估值地区：</text>
							<text>{{item.place||'暂无'}}</text>
						</view>
						<view class="other textOver" v-if="item.estimate">
							<text>预估价格：</text>
							<text style="color: red;">{{item.estimate||'暂无'}}万元</text>
						</view>
						<view class="other textOver" v-if="item.status">
							<text>状态：</text>
							<text style="color: red;">{{getDict(item.status,'status')}}</text>
						</view>
					</view>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottom_title||'没有更多了！'}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { getCurrentInstance, computed, ref } from 'vue';
	//该依赖已内置不需要单独安装
	import { onLoad } from "@dcloudio/uni-app";
	// 请求接口
	const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
	const $config = getCurrentInstance()?.appContext.config.globalProperties.$config;
	// openid
	const openid = computed(() => {
		return uni.getStorageSync('openid');
	})
	// 查询
	const searchInfo = ref({});
	// 基本信息
	const config = ref({ logoUrl: [] });
	// 列表
	const list = ref([]);
	const total = ref(0);
	const skip = ref(0);
	const limit = ref(6);
	const page = ref(0);
	// 数据是否触底
	const is_bottom = ref(false);
	const scrollTop = ref(0);
	// 字典表
	const statusList = ref([]);
	onLoad(async () => {
		await searchOther();
		await searchConfig();
		await search();
	})
	// 查询其他信息
	const searchOther = async () => {
		let res;
		// 状态
		res = await $api(`dictData`, 'GET', { code: 'valuation', is_use: '0' });
		if (res.errcode === 0) statusList.value = res.data;
	};
	// config信息
	const searchConfig = async () => {
		config.value = uni.getStorageSync('config');
	};
	// 查询
	const search = async () => {
		const info = {
			skip: skip.value,
			limit: limit.value,
			// openid: openid.value
		}
		const res = await $api('estimate', 'GET', {
			...info,
			...searchInfo.value
		});
		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 list;
		switch (model) {
			case 'status':
				list = statusList.value;
				break;
			default:
				break;
		}
		if (!list) return;
		const res = list.find((f) => f.value == data);
		return res?.label || '暂无';
	};
	// 搜索
	const toChange = () => {
		uni.navigateTo({
			url: `/pagesHome/type/index`
		})
	};
	// 分页
	const toPage = () => {
		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;
		width: 100vw;
		height: 100vh;

		.top {
			margin: 2vw;
		}

		.bottom {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);

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

				.name {
					font-size: var(--font16Size);
					margin: 0 0 1vw 0;
				}

				.other {
					font-size: var(--font14Size);

					text:first-child {
						color: var(--f85Color);
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

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

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