<template>
	<div class="judicial-statistics-satisfaction-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<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="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
						:clearable="false"
					/>
				</el-form-item>
				<!--				<el-form-item label="类型" prop="TypeId">
					<el-select v-model="state.queryParams.TypeId" placeholder="类型" @change="handleQuery">
						<el-option label="办件结果" value="1" />
						<el-option label="办件态度" value="2" />
					</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>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ toolRender: { name: 'exportAll' } }]"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30,useKey:true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="judicialStatisticsSatisfaction"
					:custom-config="{ storage: true }"
					:params="{ exportMethod: departmentSatisfactionExport, exportParams: requestParams }"
					showHeaderOverflow
					show-footer
					:footer-method="footerMethod"
				>
					<vxe-column field="orgName" title="部门名称" min-width="200" fixed="left"></vxe-column>
					<vxe-column field="orgTypeText" title="部门类别" width="140"></vxe-column>
					<vxe-column field="totalSumCount" title="小计" width="90"></vxe-column>
					<vxe-column field="totalSumRateText" title="总满意率" width="90"></vxe-column>
					<vxe-column field="verySatisfiedCount" title="非常满意" width="110">
						<template #default="{ row }">
							<el-button type="primary" link @click="onDetail(row.verySatisfiedKey, row)">
								{{ row.verySatisfiedCount }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="verySatisfiedRateText" title="非常满意率" width="110"></vxe-column>
					<vxe-column field="satisfiedCount" title="满意" width="110">
						<template #default="{ row }">
							<el-button type="primary" link @click="onDetail(row.satisfiedCount, row)">
								{{ row.satisfiedCount }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="satisfiedRateText" title="满意率" width="110"></vxe-column>
					<vxe-column field="regardedAsSatisfiedCount" title="视为满意" width="110">
						<template #default="{ row }">
							<el-button type="primary" link @click="onDetail(row.regardedAsSatisfiedCount, row)">
								{{ row.regardedAsSatisfiedCount }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="regardedAsSatisfiedRateText" title="视为满意率" width="110"></vxe-column>
					<vxe-column field="defaultSatisfiedCount" title="默认满意" width="110">
						<template #default="{ row }">
							<el-button type="primary" link @click="onDetail(row.defaultSatisfiedCount, row)">
								{{ row.defaultSatisfiedCount }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="defaultSatisfiedRateText" title="默认满意率" width="110"></vxe-column>
					<vxe-column field="noSatisfiedCount" title="不满意" width="110">
						<template #default="{ row }">
							<el-button type="primary" link @click="onDetail(row.noSatisfiedCount, row)">
								{{ row.noSatisfiedCount }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="noSatisfiedRateText" title="不满意率" width="110"></vxe-column>
					<vxe-column field="noEvaluateCount" title="未作评价" width="110">
						<template #default="{ row }">
							<el-button type="primary" link @click="onDetail(row.noEvaluateCount, row)">
								{{ row.noEvaluateCount }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="noEvaluateRateText" title="未作评价率" width="110"></vxe-column>
					<vxe-column field="noPutThroughCount" title="未接通" width="110">
						<template #default="{ row }">
							<el-button type="primary" link @click="onDetail(row.noPutThroughCount, row)">
								{{ row.noPutThroughCount }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="noPutThroughRateText" title="未接通率" width="110"></vxe-column>
				</vxe-table>
			</div>
		</div>
	</div>
</template>
<script setup lang="tsx" name="judicialStatisticsSatisfaction">
import { onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDateTime, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { departmentSatisfaction, departmentSatisfactionExport } from '@/api/judicial';
import { useRouter } from 'vue-router';
import XEUtils from 'xe-utils';

// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		crTime: defaultDateTime, // 时间默认今天开始到今天结束
		TypeId: '1',
	},
	tableData: [],
	loading: false, // 加载
	total: 0, // 总数
	totalCount: {},
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref({});
const queryList = () => {
	state.loading = true;
	const StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	const EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	requestParams.value = {
		StartTime,
		EndTime,
		TypeId: '1',
	};
	departmentSatisfaction(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.dataList ?? [];
			state.totalCount = res.result.sumModel;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 计算合计
const footerMethod = ({ columns, data }) => {
	return [
		columns.map((column: any, columnIndex: number) => {
			if (columnIndex === 0) {
				return '合计';
			}
			// 后端返回了数据集合 state.totalCount 所以不需要计算 直接进行赋值
			return XEUtils.get(state.totalCount, column.property);
		}),
	];
};
// 点击数字
const router = useRouter();
const onDetail = (key: string, row: any) => {
	const StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	const EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	router.push({
		name: 'judicialStatisticsDetailSatisfied',
		query: {
			StartTime,
			EndTime,
			DateValue: key,
			TypeId: state.queryParams.TypeId,
			orgCode: row.orgCode,
		},
	});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
