<template>
	<div class="statistics-order-visit-discontent-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<div class="table-search-content">
				<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
					<el-form-item prop="crTime">
						<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading" />
					</el-form-item>
					<el-form-item label="部门名称" prop="OrgName">
						<el-input v-model="state.queryParams.OrgName" placeholder="部门名称" clearable @keyup.enter="handleQuery" class="keyword-input" />
					</el-form-item>
					<el-form-item label="回访结果" prop="OrgProcessingResults" v-if="['YiBin'].includes(themeConfig.appScope)">
						<el-radio-group v-model="state.queryParams.OrgProcessingResults" @change="handleQuery" :disabled="state.loading">
							<el-radio value="">全部</el-radio>
							<el-radio value="-1">视为满意</el-radio>
							<el-radio value="2">不满意</el-radio>
						</el-radio-group>
					</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>
			</div>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-grid v-bind="gridOptions"> </vxe-grid>
			</div>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsVisitDiscontent">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentUnsatisfied, departmentUnsatisfiedExport } from '@/api/statistics/order';
import { defaultDate } from '@/utils/constants';
import { useRouter } from 'vue-router';
import Other from '@/utils/other';
import XEUtils from 'xe-utils';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		OrgName: null, // 关键词
		crTime: defaultDate, // 时间默认今天开始到今天结束
		OrgProcessingResults:'',
	},
	loading: false, // 加载
	tableData: [],
	total: 0, // 总数
});
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	printConfig: {},
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				queryList();
			},
		},
		tools:[
			{ toolRender: { name: 'exportAll' } }
		]
	},
	customConfig: {
		storage: true
	},
	id: 'statisticsVisitDiscontent',
	rowConfig: { isHover: true, height: 30,isCurrent:true,useKey:true },
	height: 'auto',
	columns: [],
	data: [],
	showFooter: true,
	footerMethod: ({ columns, data }) => {
		return [
			columns.map((column: any, columnIndex: number) => {
				if (columnIndex === 0) {
					return '合计';
				}
				return XEUtils.sum(data, column.property);
			}),
		];
	},
	params:{
		exportMethod: departmentUnsatisfiedExport, exportParams: requestParams,isSpecialExport:true
	}
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime');
	departmentUnsatisfied(requestParams.value)
		.then((res: any) => {
			const columns = res.result?.dicReason ?? [];
			gridOptions.columns = columns.map((item: any) => {
				return {
					prop: item.dicDataValue,
					label: item.dicDataName,
					field: item.dicDataValue,
					title: item.dicDataName,
					minWidth: 120,
					align: 'center',
					slots: {
						default (scope:any) {
							return (
								<el-button type="primary" onClick={() => linkDetail(scope)} link>
									{scope.row[item.dicDataValue]}
								</el-button>
							)
						}
					}
				};
			});
			gridOptions.columns.unshift({
				prop: 'OrgName',
				field: 'OrgName',
				label: '部门名称',
				title: '部门名称',
				minWidth: 140,
			});
			const tableData = res.result?.data ?? [];
			// 判断部门名称是否重复 重复的合并数据 并去除重复数据
			const arr = [];
			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);
				}
			});
			gridOptions.data = arr;
			state.loading = false;
			gridOptions.loading = false;
		})
		.catch(() => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
// 查看详情
const router = useRouter();
const linkDetail = (scope: any) => {
	const columnKey = Object.keys(scope.row).find((item) => item === scope.column.property);
	const startTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	const endTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	router.push({
		path: '/statistics/order/detailVisitDiscontent',
		query: {
			startTime: startTime,
			endTime: endTime,
			orgId: scope.row?.OrgCode,
			columnKey,
			OrgProcessingResults: state.queryParams.OrgProcessingResults,
		},
	});
};
onMounted(() => {
	queryList();
});
</script>
