<template>
	<div class="statistics-order-dispatch-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item label="来电人身份" prop="IdentityType">
					<el-select v-model="state.queryParams.IdentityType" placeholder="请选择来电人身份" clearable @change="handleQuery">
						<el-option v-for="item in identityTypeOptions" :value="item.value" :key="item.value" :label="item.key" />
					</el-select>
				</el-form-item>
				<el-form-item label="时间段" prop="crTime">
					<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>
					<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-button type="info" @click="onDetail" :loading="state.loading"> <SvgIcon name="ele-PieChart" class="mr5" />图表 </el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="never">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				border
				:pagination="false"
				:toolButton="['refresh', 'setting', 'exportAll']"
				@export-all="exportTable($event, true)"
			>
			</ProTable>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsOrderDetailSource">
import { onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentDispatch, orderSourceExport } from "@/api/statistics/order";
import { defaultDate, shortcuts } from '@/utils/constants';
import { useRouter } from 'vue-router';
import { downloadFileByStream, guid } from "@/utils/tools";
import Other from "@/utils/other";

// 表格配置项
const columns = ref<any[]>([
	{ type: 'index', fixed: 'left', width: 55, label: '序号', align: 'center' },
	{ prop: 'userName', label: '派单人员', align: 'center' },
	{
		prop: 'sendOrderNum',
		label: '派单量',
		align: 'center',
		sortable: true,
		render: (scope) => {
			return (
				<el-button type="primary" link onClick={() => linkDetail(scope)}>
					{scope.row.sendOrderNum}
				</el-button>
			);
		},
	},
	{
		prop: 'noSendOrderNum',
		label: '待派单件',
		align: 'center',
		sortable: true,
		render: (scope) => {
			return (
				<el-button type="primary" link onClick={() => linkDetail(scope)}>
					{scope.row.noSendOrderNum}
				</el-button>
			);
		},
	},
	{
		prop: 'reSendOrderNum',
		label: '重办信件',
		align: 'center',
		sortable: true,
		render: (scope) => {
			return (
				<el-button type="primary" link onClick={() => linkDetail(scope)}>
					{scope.row.reSendOrderNum}
				</el-button>
			);
		},
	},
	{ prop: 'chainRate', label: '派单准确率', align: 'center' },
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		UserName: null,
		// 查询条件
		crTime: defaultDate,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
const identityTypeOptions = [
	{ key: '市民', value: 1 },
	{ key: '企业', value: 2 },
];
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	let StartTime = null;
	let EndTime = null;
	if (state.queryParams?.crTime) {
		StartTime = state.queryParams?.crTime[0];
		EndTime = state.queryParams?.crTime[1];
	}
	const request = {
		StartTime,
		EndTime,
		UserName: state.queryParams.UserName,
	};
	departmentDispatch(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 router = useRouter();
const linkDetail = (scope: any) => {
	router.push({
		name: 'statisticsDetailSourceOrder',
		params: {
			id: guid(),
		},
		state: {
      IdentityType: scope.row.IdentityType,
			startTime: state.queryParams.crTime[0],
			endTime: state.queryParams.crTime[1],
		},
	});
};
// 表格导出
const exportTable = (val: any, isExportAll = false) => {
	let request = Other.deepClone(state.queryParams);
	request.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	request.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(request, 'crTime');
	const columnInfos = val.map((item: any) => {
		return {
			prop: item.prop,
			name: item.label,
		};
	});
	const req = {
		queryDto: request,
		columnInfos,
		isExportAll,
	};
	state.loading = true;
	orderSourceExport(req)
		.then((res: any) => {
			state.loading = false;
			downloadFileByStream(res);
		})
		.catch(() => {
			state.loading = false;
		});
};
// 查看图表
const onDetail = () => {
	router.push({
		name: 'statisticsOrderSource',
	});
};
onMounted(() => {
	queryList();
});
</script>
