<template>
	<div class="statistics-department-satisfied-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 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>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="drawer = true" class="default-button"> <SvgIcon name="ele-Search" class="mr5" />更多查询</el-button>
					<el-popover :width="500" trigger="click">
						<template #reference>
							<el-button type="primary" title="口径说明"><SvgIcon name="ele-QuestionFilled" class="mr5"/>口径说明</el-button>
						</template>
						<el-descriptions title="" :column="1" border style="max-height: 400px; overflow: auto">
							<el-descriptions-item label="部门名称">工单新建时用户所在的部门</el-descriptions-item>
							<el-descriptions-item label="部门类别">部门类型</el-descriptions-item>
							<el-descriptions-item label="小计">部门发起工单的总数量</el-descriptions-item>
							<el-descriptions-item label="总满意率">（小计-不满意）/小计</el-descriptions-item>
							<el-descriptions-item label="非常满意">已回访-部门评价-办件结果为非常满意</el-descriptions-item>
							<el-descriptions-item label="非常满意率">非常满意/小计</el-descriptions-item>
							<el-descriptions-item label="满意">已回访-部门评价-办件结果为满意</el-descriptions-item>
							<el-descriptions-item label="满意率">满意/小计</el-descriptions-item>
							<el-descriptions-item label="视为满意">甄别申请-不满意件-修改为满意件</el-descriptions-item>
							<el-descriptions-item label="视为满意率">视为满意/小计</el-descriptions-item>
							<el-descriptions-item label="一般">已回访-部门评价-办件结果为一般</el-descriptions-item>
							<el-descriptions-item label="一般率">一般/小计</el-descriptions-item>
							<el-descriptions-item label="不满意">已回访-部门评价-办件结果为不满意</el-descriptions-item>
							<el-descriptions-item label="不满意率">不满意/小计</el-descriptions-item>
							<el-descriptions-item label="非常不满意">已回访-部门评价-办件结果为非常不满意</el-descriptions-item>
							<el-descriptions-item label="非常不满意率">非常不满意/小计</el-descriptions-item>
							<el-descriptions-item label="未做评价">已回访-部门评价-办件结果为未做评价</el-descriptions-item>
							<el-descriptions-item label="未做评价率">未做评价/小计</el-descriptions-item>
							<el-descriptions-item label="未接通">已回访-部门评价-办件结果为未接通</el-descriptions-item>
							<el-descriptions-item label="未接通率">未接通/小计</el-descriptions-item>
						</el-descriptions>
					</el-popover>
					<el-button type="primary" @click="onDetailList" :loading="state.loading"> <SvgIcon name="ele-List" 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="statisticsDepartmentSatisfied"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
					:params="{ exportMethod: departmentSatisfactionExport, exportParams: requestParams }"
					show-footer
					:footer-method="footerMethod"
				>
					<vxe-column field="orgName" title="部门名称" min-width="200" fixed="left">
						<template #default="scope">
							<el-button type="primary" link @click="onDetailOrg(scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
								{{ scope.row.orgName }}
							</el-button>
							<span v-else>{{ scope.row.orgName }}</span>
						</template>
					</vxe-column>
          <vxe-column field="orgTypeText" title="部门类别" min-width="100" fixed="left"> </vxe-column>
          <vxe-column field="totalSumRateText" title="总满意率" min-width="120"> </vxe-column>
          <vxe-column field="totalSumCount" title="小计" min-width="90" fixed="left"> </vxe-column>
					<vxe-column field="verySatisfiedCount" title="非常满意"  min-width="100">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope.row.verySatisfiedKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
								{{ scope.row.verySatisfiedCount }}
							</el-button>
							<span v-else>{{ scope.row.verySatisfiedCount }}</span>
						</template>
					</vxe-column>
					<vxe-column field="verySatisfiedRateText" title="非常满意率" min-width="120"> </vxe-column>
					<vxe-column field="satisfiedCount" title="满意"  min-width="100">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope.row.satisfiedKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
								{{ scope.row.satisfiedCount }}
							</el-button>
							<span v-else>{{ scope.row.satisfiedCount }}</span>
						</template>
					</vxe-column>
					<vxe-column field="satisfiedRateText" title="满意率" min-width="120"> </vxe-column>
					<!--	自贡字段		-->
					<template v-if="['ZiGong'].includes(themeConfig.appScope)">
						<vxe-column field="normalCount" title="一般"  min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row.normalKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
									{{ scope.row.normalCount }}
								</el-button>
								<span v-else>{{ scope.row.normalCount }}</span>
							</template>
						</vxe-column>
						<vxe-column field="normalRateText" title="一般率" min-width="120"> </vxe-column>
					</template>
					<template v-if="['ZiGong'].includes(themeConfig.appScope)">
						<vxe-column field="regardedAsSatisfiedCount" title="甄别为满意"  min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row.regardedAsSatisfiedKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
									{{ scope.row.regardedAsSatisfiedCount }}
								</el-button>
								<span v-else>{{ scope.row.regardedAsSatisfiedCount }}</span>
							</template>
						</vxe-column>
						<vxe-column field="regardedAsSatisfiedRateText" title="甄别为满意率" min-width="120"> </vxe-column>
					</template>
					<template v-else>
						<vxe-column field="regardedAsSatisfiedCount" title="视为满意"  min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row.regardedAsSatisfiedKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
									{{ scope.row.regardedAsSatisfiedCount }}
								</el-button>
								<span v-else>{{ scope.row.regardedAsSatisfiedCount }}</span>
							</template>
						</vxe-column>
						<vxe-column field="regardedAsSatisfiedRateText" title="视为满意率" min-width="120"> </vxe-column>
					</template>
					<vxe-column field="defaultSatisfiedCount" title="默认满意"  min-width="100">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope.row.defaultSatisfiedKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
								{{ scope.row.defaultSatisfiedCount }}
							</el-button>
							<span v-else>{{ scope.row.defaultSatisfiedCount }}</span>
						</template>
					</vxe-column>
					<vxe-column field="defaultSatisfiedRateText" title="默认满意率" min-width="120"> </vxe-column>
					<vxe-column field="noSatisfiedCount" title="不满意"  min-width="100">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope.row.noSatisfiedKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
								{{ scope.row.noSatisfiedCount }}
							</el-button>
							<span v-else>{{ scope.row.noSatisfiedCount }}</span>
						</template>
					</vxe-column>
					<vxe-column field="noSatisfiedRateText" title="不满意率" min-width="120"> </vxe-column>
					<!--	自贡字段		-->
					<template v-if="['ZiGong'].includes(themeConfig.appScope)">
						<vxe-column field="veryNoSatisfiedCount" title="非常不满意"  min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row.veryNoSatisfiedKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
									{{ scope.row.veryNoSatisfiedCount }}
								</el-button>
								<span v-else>{{ scope.row.veryNoSatisfiedCount }}</span>
							</template>
						</vxe-column>
						<vxe-column field="veryNoSatisfiedRateText" title="非常不满意率" min-width="120"> </vxe-column>
					</template>
					<vxe-column field="noEvaluateCount" title="未作评价"  min-width="100">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope.row.noEvaluateKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
								{{ scope.row.noEvaluateCount }}
							</el-button>
							<span v-else>{{ scope.row.noEvaluateCount }}</span>
						</template>
					</vxe-column>
					<vxe-column field="noEvaluateRateText" title="未作评价率" min-width="120"> </vxe-column>
					<vxe-column field="noPutThroughCount" title="未接通"  min-width="100">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope.row.noPutThroughKey, scope.row)" v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)">
								{{ scope.row.noPutThroughCount }}
							</el-button>
							<span v-else>{{ scope.row.noPutThroughCount }}</span>
						</template>
					</vxe-column>
					<vxe-column field="noPutThroughRateText" title="未接通率" min-width="120"> </vxe-column>
				</vxe-table>
			</div>
		</div>
		<!--	更多查询	-->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px">
				<el-form-item label="归档类型" prop="TypeId" v-if="['ZiGong','LuZhou'].includes(themeConfig.appScope)">
					<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 label="热线号码" prop="LineNum">
					<el-select v-model="state.queryParams.LineNum" 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 label="回访方式" prop="VisitType">
					<el-select v-model="state.queryParams.VisitType" placeholder="请选择回访方式" clearable @change="handleQuery">
						<el-option v-for="item in state.visitType" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="来电主体" prop="TypeCode">
					<el-select v-model="state.queryParams.TypeCode" placeholder="请选择来电主体" @change="handleQuery">
						<el-option :value="0" label="全部" />
						<el-option :value="1" label="市民" />
						<el-option :value="2" label="企业" />
					</el-select>
				</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>
			<template #footer>
				<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				<el-button @click="resetQuery(drawerRuleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
			</template>
		</el-drawer>
	</div>
</template>
<script setup lang="tsx" name="statisticsDepartmentSatisfied">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentSatisfaction, departmentSatisfactionBase, departmentSatisfactionExport } from '@/api/statistics/department';
import { defaultDate } from '@/utils/constants';
import { useRouter } from 'vue-router';
import { callPeriodBase } from '@/api/statistics/call';
import Other from '@/utils/other';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';
import XEUtils from 'xe-utils';


const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		OrgName: null,
		LineNum: null,
		crTime: defaultDate, // 时间默认今天开始到今天结束
		StartTime: null,
		EndTime: null,
		TypeId: '1',
		TypeCode: 0,
		VisitType:null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	totalCount: {},
	callForwardingSource: [],
	visitType:[],
});
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.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');
	departmentSatisfaction(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.dataList ?? [];
			if (res.result.dataList.length) {
				state.tableData.push(res.result.citySumModel);
				state.tableData.push(res.result.countySumModel);
			}
			state.totalCount = res.result.sumModel;
			state.loading = false;
		})
		.catch((err: any) => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	statisticalTimeRef.value.reset();
	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 onDetailOrg = (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: 'statisticsDepartmentSatisfiedOrg',
		query: {
			StartTime,
			EndTime,
			OrgCode: row.orgCode,
			TypeId: state.queryParams.TypeId,
			LineNum: state.queryParams.LineNum,
		},
	});
};
// 点击数字
const linkDetail = (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: 'statisticsDepartmentSatisfiedDetail',
		query: {
			StartTime,
			EndTime,
			OrgCode: row.orgCode,
			TypeId: state.queryParams.TypeId,
			LineNum: state.queryParams.LineNum,
			DateValue: key,
		},
	});
};
// 获取基础信息
const getBaseInfo = async () => {
	try {
		const { result } = await callPeriodBase();
		state.callForwardingSource = result.callForwardingSource ?? [];
	} catch (e) {
		console.log(e);
	}
};
// 获取查询基础数据
const getBaseData = async () => {
	try {
		const { result } = await departmentSatisfactionBase();
		state.visitType = result.visitType;
	} catch (e) {
		console.log(e);
	}
}
// 跳转列表明细
const onDetailList = () => {
	router.push({
		name: 'statisticsDepartmentSatisfiedDetailList',
	});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseInfo();
	getBaseData();
});
</script>