<template>
	<div class="business-order-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
				<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:pagination="false"
				:toolButton="['refresh', 'setting', 'exportCurrent', 'exportAll']"
				:exportMethod="exportOrder"
				:exportParams="requestParams"
				v-if="['YiBin'].includes(themeConfig.appScope)"
			>
				<template #table-search>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
						<el-row>
							<el-col>
								<el-form-item label="快捷查询" prop="fastSearch">
									<el-segmented
										:options="[
												{
													value: 'all',
													label: '全部',
												},
												{
													value: 'city',
													label: '市工单',
												},
												{
													value: 'province',
													label: '省工单',
												},
											]"
										v-model="fastSearch"
										@change="fastSearchChange"
									/>
									<el-checkbox-group v-model="checkList" @change="changeCheckList" class="ml15">
										<el-checkbox value="IsSensitiveWord" border>敏感类工单</el-checkbox>
									</el-checkbox-group>
								</el-form-item>
								<el-form-item>
									<el-button @click="contentRetrieval"><SvgIcon name="ele-DocumentCopy" class="mr5" />内容检索</el-button>
								</el-form-item>
							</el-col>
						</el-row>
						<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 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>
							<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>
				</template>
				<template #tableHeader="scope">
					<el-button type="primary" @click="onCreateRepeatEvent" :loading="state.loading">
						<SvgIcon name="ele-Plus" class="mr5" />创建重复性事件
					</el-button>
					<el-button type="primary" @click="onRevoke" :loading="state.loading" v-auth="'business:order:revoke'" :disabled="!scope.isSelected" >
						<SvgIcon name="ele-Setting" class="mr5" />设置撤销<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
					</el-button>
					<el-button type="primary" @click="onObserve" v-auth="'business:order:observe'" :disabled="!scope.isSelected" :loading="state.loading"
					>添加关注<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
					</el-button>
					<el-button type="primary" @click="onEnd" v-auth="'business:order:end'" :disabled="!scope.isSelected" :loading="state.loading"
					>设置终结件<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
					</el-button>
					<el-button type="primary" @click="onJbExport" :disabled="!scope.isSelected" :loading="state.loading" v-auth="'business:order:jbdExport'"
					><SvgIcon name="iconfont icon-daochu" class="mr5" />交办单导出<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
					</el-button>
				</template>
				<template #expiredStatusText="{ row }">
					<span :class="'overdue-status-' + row.expiredStatus" :title="row.expiredStatusText"></span>
				</template>
				<template #title="{ row }">
					<order-detail :order="row" @updateList="handleQuery">{{ row.title }}</order-detail>
				</template>
				<template #operation="{ row }">
					<!-- 省工单退回条件 是省工单（工单未归档且当前节点处于热线中心办理节点时）工单状态为办理中（归档之前） 9代表有退回申请 -->
					<el-button
						link
						type="danger"
						@click="onReturn(row)"
						title="省工单退回"
						v-auth="'business:order:return:province'"
						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
					>
					<order-detail :order="row" @updateList="handleQuery" />
				</template>
			</ProTable>
				<ProTable
					ref="proTableRef"
					:columns="columns1"
					:data="state.tableData"
					@updateTable="queryList"
					:loading="state.loading"
					:pagination="false"
					:toolButton="['refresh', 'setting', 'exportCurrent', 'exportAll']"
					:exportMethod="exportOrder"
					:exportParams="requestParams"
					v-else
				>
					<template #table-search>
						<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
							<el-row>
								<el-col>
									<el-form-item label="快捷查询" prop="fastSearch">
										<el-segmented
											:options="[
												{
													value: 'all',
													label: '全部',
												},
												{
													value: 'city',
													label: '市工单',
												},
												{
													value: 'province',
													label: '省工单',
												},
											]"
											v-model="fastSearch"
											@change="fastSearchChange"
										/>
										<el-checkbox-group v-model="checkList" @change="changeCheckList" class="ml15">
											<el-checkbox value="IsSensitiveWord" border>敏感类工单</el-checkbox>
										</el-checkbox-group>
									</el-form-item>
									<el-form-item>
										<el-button @click="contentRetrieval"><SvgIcon name="ele-DocumentCopy" class="mr5" />内容检索</el-button>
									</el-form-item>
								</el-col>
							</el-row>
							<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 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>
								<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>
					</template>
					<template #tableHeader="scope">
						<el-button type="primary" @click="onCreateRepeatEvent" :loading="state.loading">
							<SvgIcon name="ele-Plus" class="mr5" />创建重复性事件
						</el-button>
						<el-button type="primary" @click="onRevoke" :loading="state.loading" v-auth="'business:order:revoke'" :disabled="!scope.isSelected" >
							<SvgIcon name="ele-Setting" class="mr5" />设置撤销<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
						</el-button>
						<el-button type="primary" @click="onObserve" v-auth="'business:order:observe'" :disabled="!scope.isSelected" :loading="state.loading"
							>添加关注<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
						</el-button>
						<el-button type="primary" @click="onEnd" v-auth="'business:order:end'" :disabled="!scope.isSelected" :loading="state.loading"
							>设置终结件<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
						</el-button>
						<el-button type="primary" @click="onJbExport" :disabled="!scope.isSelected" :loading="state.loading" v-auth="'business:order:jbdExport'"
							><SvgIcon name="iconfont icon-daochu" class="mr5" />交办单导出<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
						</el-button>
					</template>
					<template #expiredStatusText="{ row }">
						<span :class="'overdue-status-' + row.expiredStatus" :title="row.expiredStatusText"></span>
					</template>
					<template #title="{ row }">
						<order-detail :order="row" @updateList="handleQuery">{{ row.title }}</order-detail>
					</template>
					<template #operation="{ row }">
						<!-- 省工单退回条件 是省工单（工单未归档且当前节点处于热线中心办理节点时）工单状态为办理中（归档之前） 9代表有退回申请 -->
						<el-button
							link
							type="danger"
							@click="onReturn(row)"
							title="省工单退回"
							v-auth="'business:order:return:province'"
							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
						>
						<order-detail :order="row" @updateList="handleQuery" />
					</template>
				</ProTable>
				<div class="flex-end mt20" style="align-items: center">
					<!--					<el-button class="default-button" @click="onChangeTotal">展示总数</el-button>-->
					<span v-loading="totalLoading" class="mr10">共 {{ totalCount }} 条</span>
					<el-pagination
						layout="prev, pager, next"
						:total="state.total"
						@current-change="handleCurrentChange"
						:page-size="state.queryParams.PageSize"
						:current-page="state.queryParams.PageIndex"
					/>
				</div>
		</div>
		<!-- 编辑重复性事件 -->
		<repeat-event-edit ref="repeatEventEditRef" @updateList="handleQuery" />
		<!-- 工单省退回 -->
		<order-return ref="orderReturnRef" @updateList="handleQuery" />
		<!--	更多查询	-->
		<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="SensitiveWord" v-show="checkList.includes('IsSensitiveWord')">
					<el-input v-model="state.queryParams.SensitiveWord" 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
						v-model="state.queryParams.AreaCode"
						@change="handleQuery"
						class="w100"
					>
					</el-cascader>
				</el-form-item>
				<el-form-item label="是否紧急" prop="IsUrgent">
					<el-select v-model="state.queryParams.IsUrgent" placeholder="请选择是否紧急" clearable @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 @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 @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 @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="ActualHandleOrgName">
					<!--					<el-cascader
											:options="state.orgsOptions"
											filterable
											:props="{ checkStrictly: true, value: 'id', label: 'name', emitPath: false }"
											placeholder="请选择接办部门"
											clearable
											v-model="state.queryParams.OrgId"
											ref="cascadeRef"
											@change="handleQuery"
											class="w100"
										>
										</el-cascader>-->
					<el-input v-model="state.queryParams.ActualHandleOrgName" placeholder="请填写接办部门名称" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="一级部门" prop="OrgLevelOneName">
					<el-input v-model="state.queryParams.OrgLevelOneName" 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="ProvinceNo">
					<el-input v-model="state.queryParams.ProvinceNo" 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 @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="OrderTagCode" v-if="['ZiGong'].includes(themeConfig.appScope)">
					<el-select v-model="state.queryParams.OrderTagCode" placeholder="请选择推送分类" clearable @change="handleQuery">
						<el-option v-for="item in state.orderTagOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item label="归档类型" prop="FiledType">
					<el-select v-model="state.queryParams.FiledType" placeholder="请选择归档类型" @change="handleQuery" clearable>
						<el-option label="中心归档" value="10" />
						<el-option label="部门归档" value="20" />
					</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="请选择是否甄别" 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 @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="IsSgin">
					<el-select v-model="state.queryParams.IsSgin" placeholder="请选择受理情况" clearable @change="handleQuery">
						<el-option label="已签收" :value="true" />
						<el-option label="未签收" :value="false" />
					</el-select>
				</el-form-item>
				<el-form-item label="是否超期" prop="IsOverTime">
					<el-select v-model="state.queryParams.IsOverTime" placeholder="请选择是否超期" 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 @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>
			<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>
		<el-dialog
			v-model="state.dialogVisible"
			draggable
			destroy-on-close
			:close-on-click-modal="false"
			modal-class="modal_class"
			class="dialog_class"
			append-to-body
			:modal="false"
			title="内容检索"
			width="500px"
		>
			<el-form :model="state.ruleForm" ref="ruleFormContentRef" label-width="90px" class="show-info-form" @submit.native.prevent>
				<el-form-item label="信件内容">
					<el-input v-model="state.ruleForm.content" placeholder="请填写信件内容" :autosize="{ minRows: 4, maxRows: 10 }" type="textarea"></el-input>
				</el-form-item>
				<el-form-item label="检索提示">检索条件为词组检索，如“热线中心”</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialogVisible = false" class="default-button">取 消</el-button>
					<el-button type="primary" @click="onSearch(ruleFormContentRef)">查 询</el-button>
				</span>
			</template>
		</el-dialog>
		<!-- 工单撤销 -->
		<order-revoke ref="orderRevokeRef" @updateList="handleQuery" />
	</div>
</template>
<script setup lang="tsx" name="order">
import { defineAsyncComponent, onMounted, reactive, ref, onActivated, onBeforeUnmount } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { formatDate } from '@/utils/formatTime';
import { exportJbOrder, exportOrder, listBaseData, orderList, orderListCount, orderListFixed } from '@/api/business/order';
import { addObserve } from '@/api/query/observe';
import { addEnd } from '@/api/query/end';
import { treeArea } from '@/api/auxiliary/area';
import { downloadZip, getNeedArr } from '@/utils/tools';
import Other from '@/utils/other';
import mittBus from '@/utils/mitt';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const RepeatEventEdit = defineAsyncComponent(() => import('@/views/business/repeatEvent/components/Repeat-event-edit.vue')); // 编辑重复性事件
const OrderReturn = defineAsyncComponent(() => import('@/views/business/return/components/Apply.vue')); // 工单退回
const OrderRevoke = defineAsyncComponent(() => import('@/views/business/order/components/Order-revoke.vue')); // 工单撤销

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 50, // 每页条数
		// 查询条件
		No: null, // 工单编码
		ProvinceNo: 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, // 接办部门
		ActualHandleOrgName: null, // 接办部门
		OrgLevelOneName: 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: null, // 省市工单
		IsSensitiveWord: null, // 是否敏感词工单
		SensitiveWord: null, // 敏感词
		IsUrgent: null, // 是否加急
		ContentRetrieval: null, // 内容检索
		IsSgin:null, // 受理情况
		OrderTagCode:null, // 工单标签
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	acceptTypeOptions: [], //受理类型
	channelOptions: [], // 来源频道
	orderStatusOptions: [], // 工单状态
	currentStepOptions: [], // 办理节点
	identityTypeOptions: [], // 来电主体
	orgsOptions: [], // 部门
	pushTypeOptions: [], //推送分类
	orgData: [], // 机构数据
	areaOptions: [], // 省市区数据
	dialogVisible: false,
	ruleForm: {
		content: null,
	},
});
const fastSearch = ref('all'); // tab位置
const fastSearchChange = (val: string) => {
	fastSearch.value = val;
	switch (val) {
		case 'all':
			state.queryParams.IsProvinceOrder = null;
			break;
		case 'city':
			state.queryParams.IsProvinceOrder = false;
			break;
		case 'province':
			state.queryParams.IsProvinceOrder = true;
			break;
	}
	handleQuery();
};
const checkList = ref<EmptyObjectType>([]);
// 多选
const changeCheckList = () => {
	if (checkList.value.includes('IsSensitiveWord')) state.queryParams.IsSensitiveWord = true;
	else state.queryParams.IsSensitiveWord = null;
	handleQuery();
};
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', minWidth: 40, align: 'center' },
	{ prop: 'expiredStatusText', label: '超期状态', align: 'center', minWidth: 80 },
	{ prop: 'no', label: '工单编码', minWidth: 140 },
	{ prop: 'isProvinceText', label: '省/市工单', minWidth: 90 },
	{ prop: 'reTransactNum', label: '重办次数' },
	{
		prop: 'isUrgentText',
		label: '是否紧急',
		render: (scope) => {
			return <span class="color-danger font-bold">{scope.row.isUrgentText}</span>;
		},
	},
	{ prop: 'currentStepName', label: '当前节点', minWidth: 120 },
	{ prop: 'currentStepAcceptText', label: '受理情况' },
	{ prop: 'statusText', label: '工单状态', minWidth: 100 },
	{ prop: 'title', label: '工单标题', minWidth: 200 },
	{
		prop: 'startTime',
		label: '受理时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'expiredTime',
		label: '工单期满时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.expiredTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'filedTime',
		label: '办结时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.filedTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'orgLevelOneName', label: '一级部门', minWidth: 140 },
	{ prop: 'actualHandleOrgName', label: '接办部门', minWidth: 140 },
	{ prop: 'acceptType', label: '受理类型', minWidth: 100 },
	{ prop: 'counterSignTypeText', label: '是否会签', minWidth: 90 },
	{ prop: 'sourceChannel', label: '来源渠道', minWidth: 100 },
	{ prop: 'hotspotName', label: '热点分类', minWidth: 150 },
	{ prop: 'acceptorName', label: '受理人', minWidth: 120 },
	{
		prop: 'sensitiveText',
		label: '敏感词',
		minWidth: 150,
	},
	{
		prop: 'content',
		label: '受理内容',
		isShow: false,
	},
	{
		prop: 'actualOpinion',
		label: '办理结果',
		isShow: false,
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 140, align: 'center' },
]);
const columns1 = ref<any[]>([
	{ type: 'selection', minWidth: 40, align: 'center' },
	{ prop: 'expiredStatusText', label: '超期状态', align: 'center', minWidth: 80 },
	{ prop: 'no', label: '工单编码', minWidth: 140 },
	{ prop: 'isProvinceText', label: '省/市工单', minWidth: 90 },
	{ prop: 'reTransactNum', label: '重办次数' },
	{
		prop: 'isUrgentText',
		label: '是否紧急',
		render: (scope) => {
			return <span class="color-danger font-bold">{scope.row.isUrgentText}</span>;
		},
	},
	{ prop: 'currentStepName', label: '当前节点', minWidth: 120 },
	{ prop: 'currentStepAcceptText', label: '受理情况' },
	{ prop: 'statusText', label: '工单状态', minWidth: 100 },
	{ prop: 'title', label: '工单标题', minWidth: 200 },
	{
		prop: 'startTime',
		label: '受理时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'expiredTime',
		label: '工单期满时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.expiredTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'filedTime',
		label: '办结时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.filedTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'orgLevelOneName', label: '一级部门', minWidth: 140 },
	{ prop: 'actualHandleOrgName', label: '接办部门', minWidth: 140 },
	{ prop: 'acceptType', label: '受理类型', minWidth: 100 },
	{ prop: 'counterSignTypeText', label: '是否会签', minWidth: 90 },
	{ prop: 'sourceChannel', label: '来源渠道', minWidth: 100 },
	{ prop: 'orderTag', label: '工单标签', minWidth: 100 },
	{ prop: 'allDurationHour', label: '办理时长(小时)', minWidth: 120 },
	{ prop: 'hotspotName', label: '热点分类', minWidth: 150 },
	{ prop: 'acceptorName', label: '受理人', minWidth: 120 },
	{
		prop: 'sensitiveText',
		label: '敏感词',
		minWidth: 150,
	},
	{
		prop: 'content',
		label: '受理内容',
		isShow: false,
	},
	{
		prop: 'actualOpinion',
		label: '办理结果',
		isShow: false,
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 140, align: 'center' },
]);
// 获取查询条件基础信息
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',
			orderTagOptions:'orderTags',
		};
		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;
	queryIndex.value = 0;
	queryList(true);
};
// 改变页码
const queryIndex = ref(0); // 数据批次
const totalTable = ref([]); // 数据总数
const handleCurrentChange = (val: number) => {
	state.queryParams.PageIndex = val;
	// 判断当前页是否是数据的最后一页
	if (val === Math.ceil(state.total / state.queryParams.PageSize)) {
		queryList();
	} else {
		state.tableData = getNeedArr(totalTable.value, state.queryParams.PageSize)[state.queryParams.PageIndex - 1]; //当前页的表格数据
	}
};
const requestParams = ref<EmptyObjectType>({});
const totalCount = ref(0);
const totalLoading = ref(false);
const queryList = (isQuery: boolean = false) => {
		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'); // 删除无用的参数
		requestParams.value.QueryIndex = queryIndex.value; // 数据批次
		requestParams.value.ContentRetrieval = state.ruleForm.content;
		state.loading = true;
		totalLoading.value = true;
		orderListFixed(requestParams.value)
			.then((response: any) => {
				if (isQuery) {
					// 如果是查询
					totalTable.value = []; // 先清空
					totalTable.value = response?.result;
					state.total = totalTable.value.length;
					state.tableData = getNeedArr(totalTable.value, state.queryParams.PageSize)[state.queryParams.PageIndex - 1]; //当前页的表格数据
					queryIndex.value = 1; // 请求完成之后，页码加一避免重复
				} else {
					if (response?.result.length) queryIndex.value++;
					totalTable.value = totalTable.value.concat(response?.result);
					state.total = totalTable.value.length;
					state.tableData = getNeedArr(totalTable.value, state.queryParams.PageSize)[state.queryParams.PageIndex - 1]; //当前页的表格数据
				}
				state.loading = false;
			})
			.then(() => {
				// 查询总数
				orderListCount(requestParams.value)
					.then((count) => {
						totalCount.value = count.result;
						totalLoading.value = false;
					})
					.catch(() => {
						totalLoading.value = false;
					});
			})
			.catch(() => {
				state.loading = false;
				totalLoading.value = false;
			});
};
/** 重置按钮操作 */
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.IsSensitiveWord = null;
	state.queryParams.IsProvinceOrder = null;
	fastSearch.value = 'all';
	checkList.value = [];
	queryIndex.value = 0;
	ruleFormRef.value?.resetFields();
	state.ruleForm.content = null;
	queryList(true);
};
// 设置终结件
const onObserve = () => {
	const ids = proTableRef.value.selectedList.map((item: any) => {
		return {
			orderId: item.id,
		};
	});
	ElMessageBox.confirm(`选择的${proTableRef.value.selectedList.length}个工单添加关注吗？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		autofocus: false,
	})
		.then(() => {
			addObserve({ orderIds: ids }).then(() => {
				ElMessage.success('已添加关注');
				queryList(true);
			});
		})
		.catch(() => {});
};
// 设置终结件
const onEnd = () => {
	const ids = proTableRef.value.selectedList.map((item: any) => {
		return {
			orderId: item.id,
		};
	});
	ElMessageBox.confirm(`选择的${proTableRef.value.selectedList.length}个工单设置终结件吗？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		autofocus: false,
	})
		.then(() => {
			addEnd({ orderIds: ids }).then(() => {
				ElMessage.success('操作成功');
				queryList(true);
			});
		})
		.catch(() => {});
};
// 创建重复性事件
const repeatEventEditRef = ref<RefType>();
const onCreateRepeatEvent = () => {
	repeatEventEditRef.value.openDialog();
};
// 设置撤销
const orderRevokeRef = ref<RefType>();
const onRevoke = ()=>{
	const ids = proTableRef.value.selectedList.map((item: any) => item.id);
	orderRevokeRef.value.openDialog(ids);
}
// 工单省退回
const orderReturnRef = ref<RefType>(); // 工单退回ref
const onReturn = (row: any) => {
	orderReturnRef.value.openDialog(row);
};
// 交办单导出
const onJbExport = () => {
	const ids = proTableRef.value.selectedList.map((item: any) => item.id);
	ElMessageBox.confirm(`您确定导出选中的${proTableRef.value.selectedList.length}个工单的交办单，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			state.loading = true;
			exportJbOrder(ids)
				.then((res: any) => {
					downloadZip(res);
					state.loading = false;
					ElMessage.success('导出成功');
				})
				.catch(() => {
					state.loading = false;
				});
		})
		.catch(() => {});
};
//
// 打开内容检索
const contentRetrieval = () => {
	state.dialogVisible = !state.dialogVisible;
};
const ruleFormContentRef = ref<RefType>();
// 内容检索
const onSearch = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		queryIndex.value = 0;
		queryList(true);
		state.dialogVisible = false;
	});
};
onMounted(() => {
	getBaseData();
	queryList();
});
onActivated(() => {
	mittBus.on('clearCachePage', () => {
		//清除缓存
		queryList(true);
	});
});
onBeforeUnmount(() => {
	mittBus.off('clearCachePage');
});
</script>
<style scoped lang="scss">
.business-order-container {
	:deep(.pro-table-main) {
		height: calc(100% - 42px);
	}
}
</style>
