<route lang="json5" type="page">
	{
	layout: 'default',
	style: {
	navigationStyle: 'custom',
	navigationBarTitleText: '请假信息',
	},
	}
</route>
<template>
	<PageLayout navTitle="请假信息">
		<wd-card>
			<wd-row>
				<wd-col :span="12"><uni-title title="所属单位" align="left" type="h5"></uni-title></wd-col>
				<wd-col :span="12"><uni-title title="重置" align="right" type="h5" color="#666666"
						@click="reset"></uni-title></wd-col>
			</wd-row>
			<wd-row>
				<wd-col :span="24">

				</wd-col>
			</wd-row>
			<wd-row>
				<wd-col :span="12"><uni-title title="姓名" align="left" type="h5"></uni-title></wd-col>
				<wd-col :span="12"><uni-title title="劳动合同号" align="left" type="h5"></uni-title></wd-col>
			</wd-row>
			<wd-row>
				<wd-col :span="12">
					<uni-easyinput v-model="realname" placeholder="姓名模糊查询" @change="Search" @clear="Search" />
				</wd-col>
				<wd-col :span="12">
					<uni-easyinput v-model="contractNumber" placeholder="劳动合同号查询" @change="Search" @clear="Search" />
				</wd-col>
			</wd-row>
			<wd-row>
				<wd-col :span="24"><uni-title title="请假时间" align="left" type="h5"></uni-title></wd-col>
			</wd-row>
			<wd-row>
				<wd-col :span="24">
					<uni-datetime-picker v-model="range" type="daterange" rangeSeparator="至" @input="queryLeave" />
				</wd-col>
			</wd-row>
			<l-echart ref="chart" />
		</wd-card>
		<view v-for="(item, i) in list" :key="i">
			<wd-card :title="item.username_dictText+'的'+item.type+'申请'">
				<view class="card-content">
					<view class="meta-info">
						<wd-icon name="usergroup" size="14px" color="#999"></wd-icon>
						<text class="meta-text">{{item.sysOrgCode_dictText}}</text>
						<wd-icon name="time" size="14px" color="#999" style="margin-left: auto;"></wd-icon>
						<text class="meta-text">
							{{item.begintime}}至{{item.endtime}}</text>
					</view>
				</view>
			</wd-card>
		</view>
	</PageLayout>
</template>
<script setup>
	import {
		onLoad,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		listApi,
		countByOrgApi
	} from '@/api/humanResource/absence'
	import * as echarts from 'echarts';
	const chart = ref(null);
	const chartOption = ref({});
	const list = ref([]) //请假信息列表
	const realname = ref('') //姓名查询
	const contractNumber = ref('') //劳动合同号查询
	const type = ref('') //图表点击事件 请假类别
	const range = ref([]) //日期查询
	const timeout = ref(null)
	let pageNo = 1
	let pageSize = 10
	let loading = false

	const queryLeave = (e) => {
		let param = {
			// sysOrgCode: orgCode.value,
			begin: range.value[0],
			end: range.value[1],
			type: type.value,
			// username: username.value,
			realname: realname.value,
			contractNumbers: contractNumber.value
		}
		if (e == 1) { // 下拉刷新时，pageNo++
			pageNo++;
		} else { // list置空，pageNo置1
			list.value = [];
			pageNo = 1;
			if (e != 2) {
				// 如果不是图表点击事件，更新图表
				setChartOption(param);
			}
		}
		// 更新数据
		getList(param);
	}

	const getList = (params = {}) => {
		setChartOption(params)
		loading = true
		listApi({
			...params,
			pageNo,
			pageSize
		}).then((res) => {
			if (res.success) {
				list.value = [...list.value, ...res.result.records]
			}
			loading = false
		}).catch((err) => {})
	}

	const setChartOption = (e) => {
		countByOrgApi(e).then(res => {
			chartOption.value = {
				tooltip: {
					trigger: 'item'
				},
				series: [{
					type: 'pie',
					radius: '50%',
					data: res.result
				}]
			}
			initChart()
		})
	}

	// 初始化图表
	const initChart = () => {
		setTimeout(async () => {
			if (!chart.value) return;
			const myChart = await chart.value.init(echarts);
			myChart.setOption(chartOption.value);
			myChart.on('click', (params) => {
				// 根据 params 处理点击事件
				type.value = params.name
				queryLeave(2);
			});
		}, 300);
	};

	function Search() {
		if (timeout.value) {
			clearTimeout(timeout.value);
		}
		timeout.value = setTimeout(() => {
			queryLeave()
		}, 300); // 300ms 防抖时间
	}

	function reset() {
		// orgCode.value = null
		range.value = []
		type.value = ''
		// username.value = ''
		realname.value = ''
		contractNumber.value = ''
		queryLeave()
	}

	onLoad((options) => {
		getList()
	});
	onReachBottom(() => {
		if (loading) return
		queryLeave(1); //下拉刷新传1
	})
</script>

<style lang="scss">
	.card-content {
		padding: 8px 0;

		.meta-info {
			display: flex;
			align-items: center;
			font-size: 12px;
			color: #666;

			.meta-text {
				margin-left: 4px;
			}
		}
	}
</style>