<template>
	<div class="statistics-center-data-list-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="Title">
					<el-input v-model="state.queryParams.Title" 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 label="受理时间" prop="slTime">
					<el-date-picker
						v-model="state.queryParams.slTime"
						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>
					<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' } }]"
			>
			</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="statisticsCenterDataList"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
					:params="{ exportMethod: centerDataListExport, exportParams: requestParams }"
				>
					<vxe-column field="expiredStatusText" title="超期状态" width="90" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.expiredStatus" :title="row.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="orderStatusText" title="工单状态" width="100"></vxe-column>
					<vxe-column field="transferPhone" title="转接来源" width="100"></vxe-column>
					<vxe-column field="currentStepName" title="当前节点" width="100"></vxe-column>
					<vxe-column field="reTransactNum" title="重办次数" width="100"></vxe-column>
					<vxe-column field="orderScreenStatusText" title="甄别状态" width="100"></vxe-column>
					<vxe-column field="no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="provinceNo" title="省本地编码" width="230"></vxe-column>
					<vxe-column field="title" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="row" @updateList="queryList(true)">{{ row.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="creationTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="expiredTime" title="工单期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="filedTime" title="办结时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.filedTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="orgLevelOneName" title="一级部门" width="140"></vxe-column>
					<vxe-column field="orgLevelTwoName" title="二级部门" width="140"></vxe-column>
					<vxe-column field="actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="sourceChannel" title="来源渠道" width="100"></vxe-column>
					<vxe-column field="hotspotSpliceName" title="热点全称" width="150"></vxe-column>
					<vxe-column field="hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="areaName" title="区域" width="100"></vxe-column>
					<vxe-column field="fullAreaName" title="区域全称" width="100"></vxe-column>
					<vxe-column field="acceptorName" title="受理人" width="100"></vxe-column>
					<vxe-column field="fromName" title="来电人姓名" width="100"></vxe-column>
					<vxe-column field="contact" title="联系电话" width="100"></vxe-column>
					<vxe-column field="fromPhone" title="来电号码" width="100"></vxe-column>
					<vxe-column field="address" title="事发地址" width="100"></vxe-column>
					<vxe-column field="identityTypeText" title="来电人主体" width="100"></vxe-column>
					<vxe-column field="fromGenderText" title="性别" width="100"></vxe-column>
					<vxe-column field="seatEvaluateText" title="坐席满意度" width="100"></vxe-column>
					<vxe-column field="orgProcessingResults" title="满意度" width="100"></vxe-column>
					<vxe-column field="pushType" title="推送分类" width="100"></vxe-column>
					<vxe-column field="content" title="受理内容" width="200"></vxe-column>
					<vxe-column field="fileOpinion" title="承办意见" width="200"></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>
		<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="OrderStatus">
					<el-select v-model="state.queryParams.OrderStatus" placeholder="请选择工单状态" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.orderStatusOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="超期状态" prop="ExpiredStatus">
					<el-select v-model="state.queryParams.ExpiredStatus" placeholder="请选择超期状态" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.expiredStatusOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="来源渠道" prop="Channel">
					<el-select v-model="state.queryParams.Channel" placeholder="请选择来源渠道" clearable class="w100" @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="TransferPhone">
					<el-input v-model="state.queryParams.TransferPhone" placeholder="转接来源" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="当前节点" prop="CurrentStepCode">
					<el-select v-model="state.queryParams.CurrentStepCode" placeholder="请选择当前节点" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.currentStepOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="甄别状态" prop="ScreenStatus">
					<el-select v-model="state.queryParams.ScreenStatus" placeholder="请选择甄别状态" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.screenStatusOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="省本地编码" prop="ProvinceNo">
					<el-input v-model="state.queryParams.ProvinceNo" placeholder="省本地编码" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="工单期满时间" prop="qmTime">
					<el-date-picker
						v-model="state.queryParams.qmTime"
						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="OrgLevelOneName">
					<el-input v-model="state.queryParams.OrgLevelOneName" placeholder="一级部门" clearable @keyup.enter="handleQuery" class="keyword-input"/>
				</el-form-item>
				<el-form-item label="二级部门" prop="OrgLevelTwoName">
					<el-input v-model="state.queryParams.OrgLevelTwoName" placeholder="二级部门" clearable @keyup.enter="handleQuery" class="keyword-input"/>
				</el-form-item>
				<el-form-item label="接办部门" prop="CurrentHandleOrgId">
					<el-cascader
						:options="state.orgsOptions"
						filterable
						:props="{ checkStrictly: true, value: 'id', label: 'name', emitPath: false }"
						placeholder="请选择接办部门"
						clearable
						class="w100"
						v-model="state.queryParams.CurrentHandleOrgId"
						@change="handleQuery"
					>
					</el-cascader>
				</el-form-item>
				<el-form-item label="办结时间" prop="bjTime">
					<el-date-picker
						v-model="state.queryParams.bjTime"
						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="AcceptType">
					<el-select v-model="state.queryParams.AcceptType" placeholder="请选择受理类型" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.acceptTypeOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</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="NameOrNo">
					<el-input v-model="state.queryParams.NameOrNo" placeholder="受理人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="来电人姓名" prop="FromName">
					<el-input v-model="state.queryParams.FromName" placeholder="来电人姓名" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="联系电话" prop="PhoneNo">
					<el-input v-model="state.queryParams.PhoneNo" placeholder="联系电话" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="来电号码" prop="FromPhone">
					<el-input v-model="state.queryParams.FromPhone" placeholder="来电号码" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="事发地址" prop="AreaCode">
					<el-cascader
						:options="state.areaOptions"
						filterable
						:props="{ checkStrictly: true, value: 'id', label: 'areaName', emitPath: false }"
						placeholder="请选择事发地址"
						clearable
						class="w100"
						v-model="state.queryParams.AreaCode"
						@change="handleQuery"
					>
					</el-cascader>
				</el-form-item>
				<el-form-item label="来电人主体" prop="IdentityType">
					<el-select v-model="state.queryParams.IdentityType" placeholder="请选择来电人主体" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.identityTypeOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="性别" prop="FromGender">
					<el-select v-model="state.queryParams.FromGender" placeholder="请选择性别" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.fromGenderOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="推送分类" prop="PushTypeCode">
					<el-select v-model="state.queryParams.PushTypeCode" placeholder="请选择推送分类" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.pushTypeOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</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="statisticsCenterDataList">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue';
import { FormInstance } from 'element-plus';
import { centerDataLis, centerDataListBasicData, centerDataListExport } from '@/api/statistics/center';
import { defaultDate, defaultDateTime, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';
import { formatDate } from '@/utils/formatTime';
import { treeArea } from '@/api/auxiliary/area';
import { exportOrder, listBaseData } from '@/api/business/order';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Title: null, // 标题
		No: null, // 工单编码
		OrderStatus: null, // 工单状态
		ExpiredStatus:null, // 超期状态
		Channel: null, // 来源渠道
		TransferPhone: null, // 转接来源
		ActualHandleStepName:null, // 当前节点
		ScreenStatus:null,// 甄别状态
		ProvinceNo:null, // 省编号
		OrgLevelOneName:null, // 一级部门
		OrgLevelTwoName:null, // 二级部门
		CurrentHandleOrgId:null, // 接办部门
		AcceptType:null, // 受理类型
		Hotspot:null, // 热点分类
		HotspotSplice:null, // 热点全称
		AreaCode:null, // 区域
		FromName:null, // 来电人姓名
		IdentityType:null, // 来电人主体
		FromGender:null, // 性别
		FromPhone:null, // 来电号码
		NameOrNo:null, // 受理人
		PhoneNo:null, // 联系电话
		PushTypeCode:null, // 推送和分类
		slTime: defaultDateTime, // 受理时间
		StartTime: null,
		EndTime: null,
		qmTime:[],// 期满时间
		ExpiredTimeStart:null,
		ExpiredTimeEnd:null,
		bjTime:[],// 办结时间
		FiledStartTime: null,
		FiledEndTime: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	acceptTypeOptions: [], //受理类型
	channelOptions: [], // 来源频道
	orderStatusOptions: [], // 工单状态
	currentStepOptions: [], // 办理节点
	identityTypeOptions: [], // 来电主体
	pushTypeOptions: [], //推送分类
	areaOptions: [], // 省市区数据
	expiredStatusOptions:[], // 超期状态
	orgsOptions:[],// 部门数据
});
/** 搜索按钮操作 */
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.slTime === null ? null : state.queryParams.slTime[0];
	requestParams.value.EndTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[1];
	Reflect.deleteProperty(requestParams.value, 'slTime'); //受理时间
	requestParams.value.FiledStartTime = state.queryParams.bjTime === null ? null : state.queryParams.bjTime[0];
	requestParams.value.FiledEndTime = state.queryParams.bjTime === null ? null : state.queryParams.bjTime[1];
	Reflect.deleteProperty(requestParams.value, 'bjTime'); // 办结时间
	requestParams.value.ExpiredTimeStart = state.queryParams.qmTime === null ? null : state.queryParams.qmTime[0];
	requestParams.value.ExpiredTimeEnd = state.queryParams.qmTime === null ? null : state.queryParams.qmTime[1];
	Reflect.deleteProperty(requestParams.value, 'qmTime'); // 期满时间
	centerDataLis(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<RefType>();
const ruleFormRef = ref<RefType>(null); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
};
// 获取查询条件基础信息
const getBaseData = async () => {
	try {
		const res: any = await centerDataListBasicData();
		const mappings: any = {
			acceptTypeOptions: 'acceptTypeOptions',
			channelOptions: 'channelOptions',
			pushTypeOptions: 'pushTypeOptions',
			orderStatusOptions: 'orderStatusOptions',
			identityTypeOptions: 'identityTypeOptions',
			currentStepOptions: 'currentStepOptions',
			screenStatusOptions:'screenStatusOptions',
			fromGenderOptions:'fromGenderOptions',
			expiredStatusOptions:'expiredStatusOptions',
			orgsOptions:'orgsOptions',
		};
		for (const key in mappings) {
			state[key] = res.result?.[mappings[key]] ?? [];
		}
		const area = await treeArea();
		state.areaOptions = area?.result ?? []; //省市区数据
	} catch (error) {
		console.log(error);
	}
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
