<template>
	<uni-popup ref="popupRef" type="bottom" background-color="#fff" :is-mask-click="false" :mask-click="false">
		<view class="ht-qm-popup">
				<view class="phone-navBar-box">
				<view @click="goback2" class="nav-bar-icon"></view>
				<text>选择客户</text>
				<uni-icons class="nav-bar-right-icon bar-ml10" type="search" size="20"
					@click="toggle('top')"></uni-icons>
			</view>
			<view>
				<search-dialog ref="searchDialogRef" shenfen="kh" @search-btn="dialogSearchBtn"
					@reset-search="dialogSearchReset"></search-dialog>
			</view>
			
			<view class="jz-new-btn-box">
				<button type="default" class="phone-green-btn" @click="handleAddKehu">新增客户</button>
			</view>
			
			<!-- 无限滚动容器 -->
			<view>
				<scroll-view scroll-y="true" refresher-enabled="true" :refresher-triggered="data.loading"
					:refresher-threshold="50" @scrolltolower="onScrolltolower" refresher-background="transparent"
					@refresherrefresh="onRefresh" class="jz-scroll-view">
					<uni-list class="admin-list-box">
						<radio-group @change="radioChange">
							<uni-list-item v-for="item in data.list" class="jz-list-item-box">
								<template v-slot:body>

									<view>
										<view>{{item.realName}}</view>
										<view>{{item.idCard}}</view>
										<view>{{item.tel}}</view>
										<view>
											<radio :value="item.value" :checked="index === current" />
										</view>
									</view>
								</template>
							</uni-list-item>
						</radio-group>
						<uni-load-more :status="data.state" @click="getMore(0)"
							:contentText="data.contentText"></uni-load-more>
					</uni-list>
				</scroll-view>
			</view>
			<view>
				<button @click="handleSelect">选择此客户</button>
			</view>
		</view>
	</uni-popup>
</template>

<script setup>
	import {
		ref,
		reactive,
		nextTick
	} from "vue";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import * as httpApi from "@/api/sanfang.js"
	import searchDialog from "./search.vue";

	const emits = defineEmits(['select'])

	function goback2() {
		popupRef.value.close();
	}

	const searchDialogRef = ref(null);
	const popupRef = ref(null)
	const data = reactive({
		status: 0,
		statusText: '待签字',
		state: 'more',
		list: [], // 考试列表
		loading: false,
		realName: '',
		page: 0,
		size: 10,
		activeData: null
	})

	function toggle() {
		searchDialogRef.value.handleShow();
	}

	function dialogSearchBtn(opt, searchInput) {
		data.realName = searchInput
		onRefresh()
	}

	function dialogSearchReset() {
		data.realName = '';
	}

	function onScrolltolower() {
		getMore()
	}

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

	function refreshData() {
		const opt = {
			realName: data.realName,
			page: data.page,
			size: data.size,
			status: data.status
		}
		data.list = [];
		// 数学
		data.state = 'loading';
		data.page++;
		opt.page = data.page;

		httpApi.getKehuList(opt).then(res => {
			data.list = data.list.concat(res.data.data);
			data.loading = false;

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

	function getMore() {
		const opt = {
			realName: data.realName,
			page: data.page,
			size: data.size,
			status: data.status
		}
		if (data.state == 'no-more') return;
		data.state = 'loading';
		data.page++;
		opt.page = data.page;
		httpApi.getKehuList(opt).then(res => {
			data.list = data.list.concat(res.data.data);
			data.loading = false;
			if (res.data.total > data.list.length) {
				data.state = 'more';
			} else {
				data.state = 'no-more';
			}
		}).catch(err => {
			data.state = 'more';
		}).finally(() => {
			data.loading = false;
		})
	}

	function radioChange(opt) {
		data.activeData = opt.id
	}
	
	function handleAddKehu() {}


	function handleSelect() {
		emits('select', data.activeData)
	}

	function handleShow() {
		data.realName = '';
		popupRef.value.open();
		onRefresh();
	}
	
	defineExpose({
		handleShow
	})
</script>

<style>
</style>