<template>
	<div class="statistics-order-sub-regional-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item prop="dateType" label="时间单位">
					<el-select v-model="state.queryParams.dateType" placeholder="部门名称" @change="handleQuery">
						<el-option label="年" value="year" />
						<el-option label="月" value="month" />
						<el-option label="日" value="date" />
						<el-option label="自定义" value="daterange" />
					</el-select>
				</el-form-item>
				<el-form-item prop="crTime" v-if="state.queryParams.dateType === 'daterange'" label="时间段">
					<el-date-picker
						v-model="state.queryParams.crTime"
						type="daterange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD"
						:clearable="false"
					/>
				</el-form-item>
				<el-form-item prop="time" v-else label="时间">
					<el-date-picker
						v-model="state.queryParams.time"
						:type="state.queryParams.dateType"
						placeholder="选择时间"
						@change="handleQuery"
						:value-format="valueFormat"
						:clearable="false"
					/>
				</el-form-item>
				<el-form-item label="来电人身份" prop="TypeId">
					<el-select v-model="state.queryParams.TypeId" placeholder="请选择来电人身份" @change="handleQuery">
            <el-option value="" label="全部" />
            <el-option value="1" label="市民" />
            <el-option value="2" label="全部" />
					</el-select>
				</el-form-item>
				<el-form-item label="热线号码" prop="Line">
					<el-select v-model="state.queryParams.Line" placeholder="请选择热线号码" clearable @change="handleQuery">
						<el-option v-for="item in state.callForwardingSource" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="never">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				:loading="state.loading"
				row-key="areaCode"
				lazy
				:load="load"
				:tree-props="{ children: 'children', hasChildren: 'hasChild' }"
				show-summary
				border
				:pagination="false"
			>
			</ProTable>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsOrderSubRegional">
import { computed, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { statisticsOrderArea } from '@/api/statistics/order';
import { defaultDate, shortcuts } from '@/utils/constants';
import dayjs from 'dayjs';
import { callPeriodBase } from '@/api/statistics/call';

const columns = ref<any>([
	{ prop: 'areaName', label: '区域名称' },
	{ prop: 'sumCount', label: '分类统计' },
]); // 表头
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive({
	queryParams: {
		// 查询条件
		dateType: 'date', // 关键词
		time: dayjs(new Date()).format('YYYY-MM-DD'), // 时间默认今天
		crTime: defaultDate, // 时间默认今天开始到今天结束
		hbTime: defaultDate, // 时间默认今天开始到今天结束
		TypeId: '',
		StartTime: null,
		EndTime: null,
		Line: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	callForwardingSource: [],
});
const valueFormat = computed(() => {
	return state.queryParams.dateType === 'date' ? 'YYYY-MM-DD' : state.queryParams.dateType === 'month' ? 'YYYY-MM' : 'YYYY';
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
  state.tableData = [];
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	let StartTime = null;
	let EndTime = null;
	switch (state.queryParams.dateType) {
		case 'date':
			StartTime = dayjs(state.queryParams.time).startOf('date').format('YYYY-MM-DD');
			EndTime = dayjs(state.queryParams.time).endOf('date').format('YYYY-MM-DD');
			break;
		case 'month':
			StartTime = dayjs(state.queryParams.time).startOf('month').format('YYYY-MM-DD');
			EndTime = dayjs(state.queryParams.time).endOf('month').format('YYYY-MM-DD');
			break;
		case 'year':
			StartTime = dayjs(state.queryParams.time).startOf('year').format('YYYY-MM-DD');
			EndTime = dayjs(state.queryParams.time).endOf('year').format('YYYY-MM-DD');
			break;
		case 'daterange':
			StartTime = state.queryParams?.crTime[0];
			EndTime = state.queryParams?.crTime[1];
			break;
	}
	const request = {
		TypeId: state.queryParams.TypeId,
		StartTime,
		EndTime,
		Line: state.queryParams.Line,
	};
	statisticsOrderArea(request)
		.then((res: any) => {
			state.tableData = res.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 懒加载
const load = (row: any, treeNode: unknown, resolve: (date: any[]) => void) => {
	let StartTime = null;
	let EndTime = null;
	switch (state.queryParams.dateType) {
		case 'date':
			StartTime = dayjs(state.queryParams.time).startOf('date').format('YYYY-MM-DD');
			EndTime = dayjs(state.queryParams.time).endOf('date').format('YYYY-MM-DD');
			break;
		case 'month':
			StartTime = dayjs(state.queryParams.time).startOf('month').format('YYYY-MM-DD');
			EndTime = dayjs(state.queryParams.time).endOf('month').format('YYYY-MM-DD');
			break;
		case 'year':
			StartTime = dayjs(state.queryParams.time).startOf('year').format('YYYY-MM-DD');
			EndTime = dayjs(state.queryParams.time).endOf('year').format('YYYY-MM-DD');
			break;
		case 'daterange':
			StartTime = state.queryParams?.crTime[0];
			EndTime = state.queryParams?.crTime[1];
			break;
	}
	const request = {
		StartTime,
		EndTime,
		AreaCode: row.areaCode,
		TypeId: state.queryParams.TypeId,
		Line: state.queryParams.Line,
	};
	statisticsOrderArea(request)
		.then((res: any) => {
			resolve(res.result ?? []);
		})
		.catch(() => {});
};
// 获取基础信息
const getBaseInfo = async () => {
	try {
		const { result } = await callPeriodBase();
		state.callForwardingSource = result.callForwardingSource ?? [];
	} catch (e) {
		console.log(e);
	}
};
onMounted(() => {
	getBaseInfo();
	queryList();
});
</script>
