<template>
	<view class="container">
		<page-title @searchClick='searchClick' :showSearch='true'>企业信息</page-title>
<!-- 		<view class="back-btn">
			<u-icon name="arrow-left" color="#fff" size="20" customStyle="margin:0 auto" @click="backToBefore()"></u-icon>

			<text class="back-text" @click="backToBefore()">企业信息</text>

			<u-icon name="search" color="#fff" size="30" customStyle="position:absolute;top:0;right:0"
				@click="goToPage('/pages/enterpriseInfo/search/index')"></u-icon>
		</view> -->
		<view class="cards-list">
			<view class="card" v-for="(item,index) in enterpriseList" :key="index">
				<card-title :numerator="index+1" :denominator="listTotal"></card-title>

				<view class="card-name">
					<view class="card-name-title">
						<text class="card-name-description">企业名称</text>
					</view>

					<text class="card-name-text">{{item.title || "--"}}</text>
				</view>

				<view class="card-item">
					<view class="card-item-name">所在地</view>

					<view class="card-item-content">{{item.area || "--"}}</view>
				</view>

				<view class="card-item">
					<view class="card-item-name">当前状态</view>

					<view class="card-item-content">{{item.status || "--"}}</view>
				</view>

				<view class="card-item">
					<view class="card-item-name">注册资金</view>

					<view class="card-item-content">{{(item.amt!= null)? item.amt : "--"}}</view>
				</view>

				<view class="card-item">
					<view class="card-btn fat-btn" @click="gotoDetail(item.id,item.title)">企业查看</view>
				</view>
			</view>

			<empty-show v-if="enterpriseList.length===0"></empty-show>
		</view>

		<u-back-top :scroll-top="scrollTop"></u-back-top>

		<u-loading-page :loading="loading"></u-loading-page>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'

	import {
		onLoad,
		onUnload,
		onPullDownRefresh,
		onReachBottom,
		onPageScroll
	} from "@dcloudio/uni-app"

	import {
		getEnterpriseInfoList,
	} from "@/api/work/enterpriseInfo.js"

	// 返回上一级
	function backToBefore() {
		uni.reLaunch({
			url: "/pages/index"
		});
	};

	function goToPage(url) {
		uni.navigateTo({
			url
		})
	}

	let scrollTop = ref(0);
	let loading = ref(true);

	// 参数
	let searchInfo = ref({
		pageNo: 1,
		pageSize: 10,
	})
	
	function searchClick () {
		goToPage('/pages/enterpriseInfo/search/index')
	}

	// 获取列表
	function getList() {
		let params = {
			pageNo: searchInfo.value.pageNo,
			pageSize: searchInfo.value.pageSize,
			title: searchInfo.value.title,
			status: searchInfo.value.status
		}

		if(searchInfo.value.pageNo == 1){
			loading.value = true
		}
		getEnterpriseInfoList(params).then(res => {
			loading.value = false
			enterpriseList.value = enterpriseList.value.concat(res.data.list)
			listTotal.value = res.data.total

			if (res.data.total == searchInfo.value.pageNo*searchInfo.value.pageSize-(10 - res.data.list.length)) moreListFlag = false;
		}).catch(() => {
			loading.value = false
		})
	}

	// 主要列表
	let enterpriseList = ref([]);
	let listTotal = ref(0);

	// 去详情
	function gotoDetail(id, subName) {
		uni.navigateTo({
			url: `/pages/enterpriseInfo/detail/index?id=${id}&subName=${subName}`
		})
	}

	// 触底加载flag
	let moreListFlag = true

	onLoad(options => {
		// searchInfo.value = Object.assign(searchInfo.value, options)
		// let filterArr = ["null", "undefined", ""]
		// for (let i in searchInfo.value) {
		// 	if (filterArr.includes(searchInfo.value[i])) {
		// 		searchInfo.value[i] = null
		// 	}
		// }

		uni.$on('enterpriseInfoSearch', resolve => {
			searchInfo.value = Object.assign(searchInfo.value, resolve);
			searchInfo.value.pageNo = 1;
			enterpriseList.value = [];
			listTotal.value = 0;
			moreListFlag = true;

			getList();
		})

		getList();
	})

	onUnload(() => {
		uni.$off('enterpriseInfoSearch');
	});

	onPageScroll((e) => {
		scrollTop.value = e.scrollTop
	})

	onPullDownRefresh(() => {
		searchInfo.value.pageNo = 1;
		enterpriseList.value = [];
		moreListFlag = true;
		try {
			getList();
		} finally {
			uni.stopPullDownRefresh()
		}
	})

	onReachBottom(() => {
		if (!moreListFlag) {
			return uni.showToast({
				title: "已经到底了。",
				icon: "none",
				duration: 2000
			})
		}
		searchInfo.value.pageNo++;
		getList();
	})
</script>

<style lang="scss" scoped>
	// .back-btn {
	// 	position: absolute;
	// 	top: 8%;
	// 	left: 4%;
	// 	display: flex;
	// 	width: 92%;
	// 	font-size: 40rpx;
	// 	font-weight: 500;
	// 	color: #FFF;

	// 	.back-text {
	// 		margin-left: 28rpx;
	// 	}
	// }
</style>