<template>
	<div class="statistics-order-visit-discontent-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item label="部门名称" prop="OrgName">
					<el-input v-model="state.queryParams.OrgName" placeholder="部门名称" clearable @keyup.enter="queryList" class="keyword-input" />
				</el-form-item>
				<el-form-item label="时间段" prop="crTime">
					<el-date-picker
						v-model="state.queryParams.crTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="queryList"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :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"
				@updateTable="queryList"
				:loading="state.loading"
				border
				:pagination="false"
				:key="Math.random()"
				show-summary
			>
			</ProTable>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsVisitDiscontent">
import { onMounted, reactive, ref } from 'vue';
import { ElButton, ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { departmentUnsatisfied } from '@/api/statistics/order';
import { shortcuts } from '@/utils/constants';
import dayjs from 'dayjs';
import { useRouter } from 'vue-router';
// 定义变量内容

const columns = ref([]);
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		OrgName: null, // 关键词
		crTime: [dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'), dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')], // 时间默认今天开始到今天结束
	},
	loading: false, // 加载
	tableData: [],
	total: 0, // 总数
});
/** 获取列表 */
const queryList = throttle(() => {
	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,
		OrgName: state.queryParams.OrgName,
		Keyword: state.queryParams.Keyword,
		SortField: state.queryParams.SortField,
		SortRule: state.queryParams.SortRule,
	};
	departmentUnsatisfied(request)
		.then((res: any) => {
			columns.value = res.result?.dicReason ?? [];
			columns.value = columns.value.map((item: any) => {
				return {
					prop: item.dicDataValue,
					label: item.dicDataName,
					minWidth: 120,
					align: 'center',
					render: (scope) => {
						return (
							<el-button type="primary" onClick={() => handleDetail(scope)} link>
								{scope.row[item.dicDataValue]}
							</el-button>
						);
					},
				};
			});
			columns.value.unshift({
				prop: 'OrgName',
				label: '部门名称',
				minWidth: 140,
			});
			state.tableData = res.result?.data ?? [];
			// 判断部门名称是否重复 重复的合并数据 并去除重复数据
			const arr = [];
			state.tableData.forEach((item: any) => {
				const index = arr.findIndex((v: any) => v.OrgName === item.OrgName);
				if (index === -1) {
					arr.push(item);
				} else {
					arr[index] = Object.assign(arr[index], item);
				}
			});
			state.tableData = arr;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
}, 300);
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
}, 300);
// 查看详情
const router = useRouter();
const handleDetail = (row: any) => {
	const columnKey = Object.keys(row.row).find((item) => item === row.column.property);
	const columnName = columns.value.find((item) => item.prop === columnKey)?.label;
	const columnValue = row.row[columnKey];
	if (!columnValue) return ElMessage.warning('暂无数据');
	let startTime = null;
	let endTime = null;
	if (state.queryParams?.crTime) {
		startTime = state.queryParams?.crTime[0];
		endTime = state.queryParams?.crTime[1];
	}
	router.push({
		name: 'statisticsOrderVisitTable',
		params: {
			id: columnKey,
			startTime: startTime ?? 'null',
			endTime: endTime ?? 'null',
			orgId: row.row?.OrgCode,
			tagsViewName: `满意度统计明细(${row.row?.OrgName}-${columnName})`,
		},
	});
};
onMounted(() => {
	queryList();
});
</script>
