<template>
	<div class="province-order-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="ReceiveProvinceNo">
					<el-input v-model="state.queryParams.ReceiveProvinceNo" placeholder="省编码" clearable @keyup.enter="handleQuery" class="keyword-input"/>
				</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="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="">
					<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
					:print-config="{}"
					:scrollY="{ enabled: true, gt: 100 }"
					id="provinceOrder"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
					:params="{ exportMethod: exportOrder, 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="receiveProvinceNo" title="省编码" width="240"></vxe-column>
					<vxe-column field="no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="reTransactNum" title="重办次数" width="90"></vxe-column>
					<vxe-column field="isUrgentText" title="是否紧急" width="90">
						<template #default="{ row }">
							<span class="color-danger font-bold">{{ row.isUrgentText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="currentStepName" title="当前节点" width="120"></vxe-column>
					<vxe-column field="statusText" title="工单状态" width="100">
						<template #default="{ row }">
							<el-text type="danger" tag="b" v-if="[1, 2, 3, 9, 101, 102, 103, 104, 105, 200].includes(row.status)">{{ row.statusText }}</el-text>
							<span v-else>{{ row.statusText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="title" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="startTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.startTime, '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="actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="counterSignTypeText" title="是否会签" width="110"></vxe-column>
					<vxe-column field="sourceChannel" title="来源渠道" width="110"></vxe-column>
					<vxe-column field="hotspotSpliceName" title="热点全称" width="150"></vxe-column>
					<vxe-column field="hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="acceptorName" title="受理人" width="120"></vxe-column>
					<vxe-column title="操作" fixed="right" width="80" align="center">
						<template #default="{ row }">
							<!-- 省工单退回条件 是省工单（工单未归档且当前节点处于热线中心办理节点时）工单状态为办理中（归档之前） 9代表有退回申请 -->
							<el-button
								link
								type="danger"
								@click="onReturn(row)"
								title="省工单退回"
								v-auth="'province:order:return'"
								v-if="
								row.isProvince &&
								(row.actualHandleOrgCode === '001' || row.actualHandleOrgCode === null || row.actualHandleOrgCode === '' || row.status === 0) &&
								row.status < 300 && row.status !== 9 && !row.provinceSendBack
							"
							>
								退回</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>
		<!--	更多查询	-->
		<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="No">
					<el-input v-model="state.queryParams.No" 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="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="IsUrgent">
					<el-select v-model="state.queryParams.IsUrgent" placeholder="请选择是否紧急" clearable class="w100" @change="handleQuery">
						<el-option :value="true" label="紧急" />
						<el-option :value="false" label="不紧急" />
					</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="Status">
					<el-select v-model="state.queryParams.Status" 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="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="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="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
						:props="{ checkStrictly: true, value: 'id', label: 'name', emitPath: false }"
						placeholder="请选择接办部门"
						clearable
						class="w100"
						v-model="state.queryParams.OrgId"
						ref="cascadeRef"
						@change="handleQuery"
					>
					</el-cascader>
				</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="FromPhone">
					<el-input v-model="state.queryParams.FromPhone" 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="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-item label="期满时间" prop="exTime">
					<el-date-picker
						v-model="state.queryParams.exTime"
						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="ActualHandlerName">
					<el-input v-model="state.queryParams.ActualHandlerName" placeholder="接办人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="是否甄别" prop="IsScreen">
					<el-select v-model="state.queryParams.IsScreen" placeholder="请选择是否甄别" class="w100" clearable @change="handleQuery">
						<el-option label="是" :value="true" />
						<el-option label="否" :value="false" />
					</el-select>
				</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="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="IsOverTime">
					<el-select v-model="state.queryParams.IsOverTime" placeholder="请选择是否超期" class="w100" clearable @change="handleQuery">
						<el-option label="是" :value="true" />
						<el-option label="否" :value="false" />
					</el-select>
				</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="ProvinceChannel">
					<el-select v-model="state.queryParams.ProvinceChannel" placeholder="请选择省来源分类 " class="w100" clearable @change="handleQuery">
						<el-option label="政民互动直派" value="1" />
						<el-option label="政民互动" value="2" />
						<el-option label="省12345" value="3" />
					</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>
		<!-- 工单省退回 -->
		<order-return ref="orderReturnRef" @updateList="queryList" />
	</div>
</template>
<script setup lang="tsx" name="provinceOrder">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { formatDate } from '@/utils/formatTime';
import { exportOrder, listBaseData, orderList, orderListCount } from '@/api/business/order';
import { treeArea } from '@/api/auxiliary/area';
import Other from '@/utils/other';
import { exportAssignment } from '@/utils/tools';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const OrderReturn = defineAsyncComponent(() => import('@/views/business/return/components/Apply.vue')); // 工单退回
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		// 查询条件
		No: null, // 工单编码
		ReceiveProvinceNo: null, // 省编码
		ActualHandlerName: null, // 接办人
		IsScreen: null, // 是否甄别
		CurrentStepCode: null, // 办理节点
		IsOverTime: null, // 是否超期
		FromName: null, // 来电人姓名
		AreaCode: null, // 事发地址
		FromPhone: null, // 来电号码
		Keyword: null, // 关键字
		Content: null, // 工单内容
		AcceptType: null, // 受理类型
		Channels: null, // 渠道
		Hotspot: null, //  热点分类名称
    OrgId: null, // 接办部门
		NameOrNo: null, // 受理坐席
		crTime: [], // 生成时间
		CreationTimeStart: null, // 创建时间 开始
		CreationTimeEnd: null, // 创建时间 结束
		Status: null, // 工单状态
		TransferPhone: null, // 转接来源
		exTime: [], // 过期时间
		ExpiredTimeStart: null, //办理期限 开始
		ExpiredTimeEnd: null, //办理期限 结束
		PhoneNo: null, // 手机号
		doneTime: [], // 办结时间
		ActualHandleTimeStart: null,
		ActualHandleTimeEnd: null,
		PushTypeCode: null, //推送类型
		IsProvinceOrder: true, // 省市工单
		IsUrgent: null, // 是否加急
		ProvinceChannel: null, //省来源分类
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	acceptTypeOptions: [], //受理类型
	channelOptions: [], // 来源频道
	orderStatusOptions: [], // 工单状态
	currentStepOptions: [], // 办理节点
	identityTypeOptions: [], // 来电主体
	orgsOptions: [], // 部门
	pushTypeOptions: [], //推送分类
	orgData: [], // 机构数据
	areaOptions: [], // 省市区数据
});
// 获取查询条件基础信息
const getBaseData = async () => {
	try {
		const res: any = await listBaseData();
		const mappings: any = {
			acceptTypeOptions: 'acceptTypeOptions',
			channelOptions: 'channelOptions',
			orgsOptions: 'orgsOptions',
			pushTypeOptions: 'pushTypeOptions',
			orderStatusOptions: 'orderStatusOptions',
			identityTypeOptions: 'identityTypeOptions',
			currentStepOptions: 'currentStepOptions',
		};
		for (const key in mappings) {
			state[key] = res.result?.[mappings[key]] ?? [];
		}
		const area = await treeArea();
		state.areaOptions = area?.result ?? []; //省市区数据
	} catch (error) {
		console.log(error);
	}
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
	getTotal();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	requestParams.value = Other.deepClone(state.queryParams);
	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.ExpiredTimeStart = state.queryParams.exTime === null ? null : state.queryParams.exTime[0]; // 期满时间
	requestParams.value.ExpiredTimeEnd = state.queryParams.exTime === null ? null : state.queryParams.exTime[1];
	Reflect.deleteProperty(requestParams.value, 'exTime'); // 删除无用的参数
	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'); // 删除无用的参数
	state.loading = true;
	orderList(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 查询总数
const getTotal = () => {
	orderListCount(requestParams.value)
		.then((res) => {
			state.total = res.result ?? 0;
		})
		.catch(() => {
		});
};
/** 重置按钮操作 */
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
	getTotal();
};
// 工单省退回
const orderReturnRef = ref<RefType>(); // 工单退回ref
const onReturn = (row: any) => {
	orderReturnRef.value.openDialog(row);
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
	getTotal();
});
</script>