<template>
	<view class="admin-jiazheng-list">
		<view class="phone-navBar-box">
			<view @click="goUpPage" class="nav-bar-icon"></view>
			<text class="nav-bar-title">家政人员</text>
			<view class="nav-bar-icon-box">
				<uni-icons class="nav-bar-right-icon" type="plusempty" size="18" color="#666"
					@click="addJiazhengRenyuan"></uni-icons>
				<uni-icons class="nav-bar-right-icon" type="search" size="18" color="#666"
					@click="toggle('top')"></uni-icons>
			</view>
		</view>
		<view class="jiazheng-search-box">
			<view @click.stop="clickAlltype" class="select-item-box">
				<text>{{data.leixing}}</text>
				<icon :class="allJtClass" />
			</view>

			<view class="jiazheng-search-right">
				<!--  当前点击 增加 saixuan-active-->
				<view @click="updateTimeClick" :class="shaixuanStyleUpdate">
					<text>更新时间</text>
					<icon :class="updataJtClass" />
				</view>
				<view @click="luruTimeClick" :class="shaixuanStyleluru">
					<!-- class切换  saixuan-jt-default  saixuan-jt-click-->
					<text>录入时间</text>
					<icon :class="luruJtClass" />
				</view>
			</view>
		</view>
		<view class="all-type-box" v-show="isOpen" @click="clickAlltype">
			<dataChecked :list="allType" mode="single" :defaultCount="99" :showAdd="false"
				@change="selectChangeSkill" />
		</view>
		<view>
			<scroll-view scroll-y="true" refresher-enabled="true" :refresher-triggered="data.loading"
				:refresher-threshold="50" refresher-background="transparent" @refresherrefresh="onRefresh"
				class="jz-scroll-view">
				<uni-list class="admin-list-box">
					<uni-list-item v-for="item in data.list" class="jz-list-item-box">
						<template v-slot:body>
							<view class="card-head-row">
								<view class="head-name">{{item.realName}}</view>
								<button type="default" class="phone-green-btn bz-tel-btn"
									@click.stop="telephone(item)">打电话</button>
							</view>
							<view @click="lookUserInfo(item)" class="card-body-row">
								<view class="card-img-box">
									<img :src="item.icon" v-if="item.icon">
									<icon class="phone-default-userImg" v-else></icon>
								</view>
								<view class="body-content-row">
									<view class="content-text-row">
										<view> {{item.age}}岁<text v-if="item.jingyan"> ｜ {{item.jingyan}}年经验</text>
										</view>
										<view class="text-status">{{item.zhuangtai}}</view>
									</view>
									<view class="content-text-row">{{item.jiguan}}</view>
								</view>
							</view>
						</template>
					</uni-list-item>
					<uni-load-more :status="data.state" @click="getMore(0)"
						:contentText="data.contentText"></uni-load-more>
				</uni-list>
			</scroll-view>
		</view>
		<view>
			<search-dialog ref="searchDialogRef"  @search-btn="dialogSearchBtn"
				@reset-search="dialogSearchReset"></search-dialog>
		</view>
		<customTabbarAdminVue :current-tab="1"></customTabbarAdminVue>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive
	} from "vue";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		getJiazhengList,
		getJiazhengLeixing
	} from "@/api/jiazheng.js"
	import dataChecked from './common/dataChecked.vue';
	import searchDialog from "./common/search.vue";
	import customTabbarAdminVue from "@/components/custom-tabbar/custom-tabbar-admin.vue";
	const searchDialogRef = ref(null);
	let allType = ref([])
	let data = reactive({
		leixing: '全部类型',
		page: 0,
		size: 10,
		realName: '',
		sortRule: '', //排序规则（1降序，2升序
		sortRule: '', //排序类型（1更新时间，2录入时间）
		userName: '',
		list: [], // 考试列表
		loading: false,
		state: 'more',
		contentText: {
			contentdown: '查看更多',
			contentrefresh: '加载中',
			contentnomore: '没有更多'
		}
	})
	let isOpen = ref(false)
	let defauleImg = ref('@/static/images/my/user-default-img.png')
	const allJtClass = 'select-jt-default';
	let updataJtClass = ref('');
	let luruJtClass = ref('');
	let shaixuanStyleUpdate = ref('saixuan-item-box');
	let shaixuanStyleluru = ref('saixuan-item-box');
	const isOpenSelect = ref(false);
	const selectedOption = ref('');
	let isFirstClickUpdate = ref('true')
	let isFirstClickluru = ref('true')

	function updateTimeClick() {
		data.sortType = 1
		const isFirst = isFirstClickUpdate.value == 'true';
		const isSecond = isFirstClickUpdate.value == 'false';
		if (isFirst) {
			shaixuanStyleUpdate.value = 'saixuan-item-box saixuan-active';
			updataJtClass.value = 'saixuan-jt-default';
			isFirstClickUpdate.value = 'false';
			data.sortRule = 1
		} else if (isSecond) {
			shaixuanStyleUpdate.value = 'saixuan-item-box saixuan-active';
			updataJtClass.value = 'saixuan-jt-click';
			isFirstClickUpdate.value = 'empty';
			data.sortRule = 2
		} else {
			shaixuanStyleUpdate.value = 'saixuan-item-box';
			updataJtClass.value = '';
			isFirstClickUpdate.value = 'true';
			data.sortRule = 0
		}
		data.page = 0;
		refreshData()
	}

	function luruTimeClick() {
		data.sortType = 2
		const isFirst = isFirstClickluru.value == 'true';
		const isSecond = isFirstClickluru.value == 'false';
		if (isFirst) {
			shaixuanStyleluru.value = 'saixuan-item-box saixuan-active';
			luruJtClass.value = 'saixuan-jt-default';
			isFirstClickluru.value = 'false';
			data.sortRule = 1
		} else if (isSecond) {
			shaixuanStyleluru.value = 'saixuan-item-box saixuan-active';
			luruJtClass.value = 'saixuan-jt-click';
			isFirstClickluru.value = 'empty';
			data.sortRule = 2
		} else {
			shaixuanStyleluru.value = 'saixuan-item-box';
			luruJtClass.value = '';
			isFirstClickluru.value = 'true';
			data.sortRule = 0
		}
		data.page = 0;
		refreshData()
	}

	function dialogSearchBtn(name, searchData) {
		switch (name) {
			case '姓名':
				dialogSearchReset();
				data.realName = searchData.value;
				break;
			case '手机号':
				data.userName = searchData.value;
				break;
		}
		data.page = 0;
		refreshData();

	}

	function dialogSearchReset() {
		data.userName = '';
		data.realName = '';
	}
	const clickAlltype = (data) => {
		isOpen.value = !isOpen.value;
	}

	const toggle = (data, index) => {
		searchDialogRef.value.handleShow();
	}
	const addJiazhengRenyuan = () => {
		let pageInfo = {
			status: 'add'
		}
		uni.redirectTo({
			url: "/pages/admin/Jiazheng/jiazhengUserManager?pageInfo=" + JSON.stringify(pageInfo)
		})
	}
	const lookUserInfo = (data) => {
		let pageInfo = {
			status: 'edit',
			id: data.id,
			userId: data.userId
		}
		uni.redirectTo({
			url: `/pages/admin/Jiazheng/jiazhengUserInfo?pageInfo=` + JSON.stringify(pageInfo)
		})
	}

	function goUpPage() {
		uni.redirectTo({
			url: `/pages/admin/ShouYe/shouye`
		})
	}

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

	function telephone(data) {
		console.log('data', data);
		if (data.userName) {
			uni.makePhoneCall({
				phoneNumber: data.userName
			});
		} else {
			uni.showToast({
				title: `无电话号`,
				icon: 'none',
				duration: 2000
			})
			return false
		}

	}


	function selectChangeSkill(item) {
		data.leixing = item[0].name
		console.log('selectChangeSkill', item);
		data.page = 0;
		refreshData()
	}

	function refreshData() {
		const opt = {
			"leixing": data.leixing == '全部类型' ? '' : data.leixing,
			"page": 1,
			"realName": data.realName,
			"size": 10,
			"sortRule": data.sortRule,
			"sortType": data.sortType,
			"userName": data.userName
		}
		data.list = [];
		// 数学
		data.state = 'loading';
		data.page++;
		opt.page = data.page;

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

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

	function getMore() {
		const opt = {
			"leixing": data.leixing == '全部类型' ? '' : data.leixing,
			"page": 1,
			"realName": data.realName,
			"size": 10,
			"sortRule": data.sortRule,
			"sortType": data.sortType,
			"userName": data.userName
		}
		if (data.state == 'no-more') return;
		data.state = 'loading';
		data.page++;
		opt.page = data.page;
		getJiazhengList(opt).then(res => {
			data.list = data.list.concat(res.data.data);
			data.loading = false;

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

	function getLeixing() {
		let req = {}
		getJiazhengLeixing(req).then(res => {
			res.data.unshift("全部类型");
			allType = dataConversionObject(res.data)
			console.log('allType', allType);
		})
	}

	function dataConversionObject(data) {
		let newList
		newList = data.map((name, index) => ({
			id: index,
			name: name
		}));
		return newList
	}
	onLoad(() => {
		getMore()
		getLeixing()
	})
</script>