<template>
	<div class="statistics-department-dp-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="hfTime">
					<statistical-time v-model="state.queryParams.hfTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading"/>
				</el-form-item>
				<el-form-item label="工单标题" prop="Keyword">
					<el-input v-model="state.queryParams.Keyword" placeholder="工单标题" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="工单编码" prop="No">
					<el-input v-model="state.queryParams.No" 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-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }]"
			>
				<template #buttons>
					<el-button type="primary" @click="onJbExport" :loading="state.loading" :disabled="isChecked"
					><SvgIcon name="iconfont icon-daochu" 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>
							<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>
				</template>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:sort-config="{ remote: true }"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
					ref="tableRef"
					@sort-change="sortChange"
					@checkbox-all="selectAllChangeEvent"
					@checkbox-change="selectChangeEvent"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="statisticsDepartmentDpSatisfied"
					:custom-config="{ storage: true }"
					:params="{ exportMethod: departmentSatisfactionListExport, exportParams: requestParams }"
				>
					<vxe-column type="checkbox" width="60" align="center"></vxe-column>
					<vxe-column field="no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="reTransactNum" title="重办次数" width="90"></vxe-column>
					<vxe-column field="orderScreenStatusText" title="甄别" width="90"></vxe-column>
					<vxe-column field="visitContent" title="回访信息" width="150"></vxe-column>
					<vxe-column field="hotspotSpliceName" title="热点全称" width="150"></vxe-column>
					<vxe-column field="orgLevelOneName" title="一级部门" width="140"></vxe-column>
					<vxe-column field="actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="creationTime" title="受理时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="title" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="{...row,id:row.orderId}" @updateList="queryList">{{ row.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="visitUser" title="回访人" width="120"></vxe-column>
					<vxe-column field="visitOrgName" title="回访部门" width="140"></vxe-column>
					<vxe-column field="visitTypeText" title="回访方式" width="110"></vxe-column>
					<vxe-column field="visitTime" title="回访时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.visitTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="orgProcessingResults" title="满意度" width="120"></vxe-column>
					<vxe-column field="content" title="受理内容" width="150"></vxe-column>
					<vxe-column field="fileOpinion" title="承办意见" width="150"></vxe-column>
					<vxe-column field="filedTime" title="办结时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.filedTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" fixed="right" width="90" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="visitDetail(row)" title="查看回访详情"> 回访详情 </el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
		<!-- 回访详情 -->
		<visit-detail-com ref="visitDetailRef" @updateList="queryList" />
		<!--	更多查询	-->
		<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="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"
					/>
				</el-form-item>
				<el-form-item label="部门分类" prop="OrgVisitStatisticsType">
					<el-select v-model="state.queryParams.OrgVisitStatisticsType" placeholder="请选择部门分类" @change="handleQuery" clearable>
						<el-option label="中心归档" value="10" />
						<el-option label="部门归档" value="20" />
					</el-select>
				</el-form-item>
				<el-form-item label="来源渠道" prop="Channel">
					<el-select v-model="state.queryParams.Channel" placeholder="请选择来源渠道" clearable @change="handleQuery">
						<el-option v-for="item in state.channelOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item label="回访人姓名" prop="VisitUser">
					<el-input v-model="state.queryParams.VisitUser" placeholder="回访人姓名" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="查询类型" prop="AttitudeType">
					<el-select v-model="state.queryParams.AttitudeType" placeholder="请选择查询类型" @change="selectType">
						<el-option v-for="item in state.attitudeType" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="办件结果" prop="OrgProcessingResults" v-if="state.queryParams.AttitudeType === 1">
					<el-select v-model="state.queryParams.OrgProcessingResults" placeholder="请选择办件结果" multiple clearable>
						<el-option  v-for="item in state.visitSatisfaction" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="办件态度" prop="OrgProcessingResults" v-if="state.queryParams.AttitudeType === 2">
					<el-select v-model="state.queryParams.OrgProcessingResults" placeholder="请选择办件态度" multiple clearable>
						<el-option  v-for="item in state.visitMananer" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName"></el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="热点分类" prop="Hotspot">
					<el-input v-model="state.queryParams.Hotspot" placeholder="热点分类名称" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="回访部门" prop="OrgId">
					<el-cascader
						:options="state.orgsOptions"
						filterable
						:show-all-levels="false"
						:props="{ checkStrictly: true, value: 'id', label: 'name', emitPath: false }"
						placeholder="请选择回访部门"
						clearable
						class="w100"
						v-model="state.queryParams.OrgId"
						@change="handleQuery"
					>
					</el-cascader>
				</el-form-item>
				<el-form-item label="办结时间" prop="doneTime">
					<el-date-picker
						v-model="state.queryParams.doneTime"
						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"
					/>
				</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>
			<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="statisticsDepartmentDpSatisfied">
import { onMounted, reactive, ref, defineAsyncComponent, computed } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentSatisfactionDetailBase, departmentSatisfactionList, departmentSatisfactionListExport } from '@/api/statistics/department';
import { formatDate } from '@/utils/formatTime';
import { defaultDate, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';
import {  exportAssignment } from '@/utils/tools';

// 引入组件
const VisitDetailCom = defineAsyncComponent(() => import('@/views/business/visit/components/Visit-detail.vue')); // 回访
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		hfTime: defaultDate, // 回访事件
		AttitudeType:1, // 默认办件结果
		VisitTimeStart: null,
		VisitTimeEnd: null,
		crTime: [],
		CreationTimeStart: null,
		CreationTimeEnd: null,
		doneTime: [], // 办结时间
		ActualHandleTimeStart: null,
		ActualHandleTimeEnd: null,
		VisitUser: null,
		OrgProcessingResults: null,
		OrgId: null,
		TypeCode: 0,
		SortField:null,
		SortRule:null,
		OrgVisitStatisticsType:null,
		Channel:null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	attitudeType:[],
	channelOptions:[],
	visitSatisfaction:[], // 办件结果
	visitMananer:[], // 办件态度
});
// 排序
const sortChange = (val: any) => {
	state.queryParams.SortField = val.order ? val.field : null;
	// 0 升序 1 降序
	state.queryParams.SortRule = val.order ? (val.order == 'desc' ? 1 : 0) : null;
	queryList();
};
// 选择查询类型
const selectType = ()=>{
	drawerRuleFormRef.value.resetFields('OrgProcessingResults');
}
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.VisitTimeStart = state.queryParams.hfTime === null ? null : state.queryParams.hfTime[0]; // 回访时间
	requestParams.value.VisitTimeEnd = state.queryParams.hfTime === null ? null : state.queryParams.hfTime[1];
	Reflect.deleteProperty(requestParams.value, 'hfTime'); // 删除无用的参数
	requestParams.value.CreationTimeStart = state.queryParams.crTime === null ? null : state.queryParams.crTime[0]; // 受理时间
	requestParams.value.CreationTimeEnd = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime'); // 删除无用的参数
	requestParams.value.ActualHandleTimeStart = state.queryParams.doneTime === null ? null : state.queryParams.doneTime[0]; // 生成时间
	requestParams.value.ActualHandleTimeEnd = state.queryParams.doneTime === null ? null : state.queryParams.doneTime[1];
	Reflect.deleteProperty(requestParams.value, 'doneTime'); // 删除无用的参数
	departmentSatisfactionList(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			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 visitDetailRef = ref<RefType>();
const visitDetail = (row: any) => {
	visitDetailRef.value.openDialog({ ...row, id: row.visitId }, '回访详情');
};
// 获取查询条件基础信息
const getBaseData = async () => {
	try {
		const res: any = await departmentSatisfactionDetailBase();
		const mappings: any = {
			visitSatisfaction: 'visitSatisfaction',
			orgsOptions: 'orgsOptions',
			attitudeType: 'attitudeType',
			channelOptions:'channelOptions',
			visitMananer:'visitMananer',
		};
		for (const key in mappings) {
			state[key] = res.result?.[mappings[key]] ?? [];
		}
	} catch (error) {
		console.log(error);
	}
};
// 交办单导出
const onJbExport = () => {
	const ids = checkTable.value.map((item: any) => item.id);
	exportAssignment(ids);
};
const tableRef = ref<RefType>();
const checkTable = ref<EmptyArrayType>([]);
const selectAllChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !Boolean(checkTable.value.length);
});
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
