<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 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="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="OrgName">
		<el-input v-model="state.queryParams.OrgName" placeholder="部门名称" clearable @keyup.enter="handleQuery" class="keyword-input" />
	</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"
		/>
	</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 label="热线号码" prop="LineNum">
		<el-input v-model="state.queryParams.LineNum" 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="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="statisticsDepartmentSatisfiedOrg"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
					:params="{ exportMethod: departmentSatisfactionOrgExport, exportParams: requestParams }"
					show-footer
					:footer-method="footerMethod"
				>
					<vxe-column field="orgName" title="部门名称" min-width="200" fixed="left">
					</vxe-column>
					<vxe-column field="orgTypeText" title="部门类别" min-width="100" fixed="left"> </vxe-column>
					<vxe-column field="totalSumCount" title="小计" min-width="90" fixed="left"> </vxe-column>
					<vxe-column field="totalSumRateText" title="总满意率" min-width="120"> </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>
					<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>
					<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.defaultSatisfiedCount }}</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>
	</div>
</template>
<script setup lang="tsx" name="statisticsDepartmentSatisfiedOrg">
import { onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentSatisfactionBase, departmentSatisfactionOrg, departmentSatisfactionOrgExport } from '@/api/statistics/department';
import { useRoute, useRouter } from 'vue-router';
import XEUtils from 'xe-utils';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';

// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		TypeId: '1', //
		TypeCode: 0,
		VisitType:null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	totalCount: {},
	visitType:[],
});
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
/** 获取列表 */
const route = useRoute();
const routeQueryParams = route.query;
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = {
		StartTime: routeQueryParams.StartTime,
		EndTime: routeQueryParams.EndTime,
		TypeId: routeQueryParams.TypeId,
		LineNum: routeQueryParams.LineNum,
		OrgCode: routeQueryParams.OrgCode,
		DateValue: routeQueryParams.DateValue,
		TypeCode: state.queryParams.TypeCode,
		VisitType: state.queryParams.VisitType,
	};
	departmentSatisfactionOrg(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 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 linkDetail = (key: string, row: any) => {
	router.push({
		name: 'statisticsDepartmentSatisfiedDetail',
		query: {
			StartTime: routeQueryParams.StartTime,
			EndTime: routeQueryParams.EndTime,
			OrgCode: row.orgCode,
			TypeId: state.queryParams.TypeId,
			DateValue: key,
			IsOnlyMy: true,
			PickOrgCode: routeQueryParams.OrgCode,
		} as any,
	});
};
// 获取查询基础数据
const getBaseData = async () => {
	try {
		const { result } = await departmentSatisfactionBase();
		state.visitType = result.visitType;
	} catch (e) {
		console.log(e);
	}
}
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
