<template>
	<div class="business-second-handle-apply-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.trim="工单标题" 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.trim="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</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
					:scrollY="{ enabled: true, gt: 100 }"
					id="businessSecondHandleApply"
					:custom-config="{ storage: true }"
					showHeaderOverflow
					:params="{ exportMethod: secondHandleExport, exportParams: requestParams }"
				>
					<vxe-column field="order.expiredStatusText" title="超期状态" width="90" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="order.currentStepName" title="当前节点" width="120"></vxe-column>
					<vxe-column field="order.no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="title" title="工单标题" min-width="200">
						<template #default="{ row }">
							<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="order.startTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.startTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="order.expiredTime" title="工单期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="visitOrgName" title="回访部门" width="140"></vxe-column>
					<vxe-column field="order.actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="order.actualHandleTime" title="接办时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.actualHandleTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="order.filedTime" title="办结时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.filedTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="order.acceptType" title="受理类型" width="120"></vxe-column>
					<vxe-column field="order.hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column title="操作" fixed="right" width="200" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onApply(row)" title="申请二次办理" v-auth="'business:secondHandle:apply'">
								申请二次办理
							</el-button>
							<el-button link type="primary" @click="onDetail(row)" title="办理明细" v-if="row.secondaryHandling"> 办理明细 </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>
		<!--  二次办理申请  -->
		<second-handle-apply ref="secondHandleApplyRef" @updateList="queryList" />
		<!--  办理明细  -->
		<second-handle-detail ref="secondHandleDetailRef" @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="SourceChannel">
					<el-select v-model="state.queryParams.SourceChannel" placeholder="请选择来源渠道" clearable @change="handleQuery">
						<el-option v-for="item in state.sourceChannelOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</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="CounterSignType">
					<el-select v-model="state.queryParams.CounterSignType" placeholder="会签类型" @change="handleQuery" clearable>
						<el-option v-for="item in state.counterSignTypeOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</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="ActualHandleOrgName">
					<el-input v-model="state.queryParams.ActualHandleOrgName" placeholder="接办部门名称" clearable @keyup.enter="handleQuery" />
				</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"
					/>
				</el-form-item>
				<el-form-item label="接办时间" prop="jbTime">
					<el-date-picker
						v-model="state.queryParams.jbTime"
						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="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="hfTime">
					<el-date-picker
						v-model="state.queryParams.hfTime"
						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="VisitOrgName">
					<el-input v-model="state.queryParams.VisitOrgName" placeholder="被回访部门名称" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="部门办件结果" prop="OrgProcessingResults">
					<el-select v-model="state.queryParams.OrgProcessingResults" placeholder="请选择部门办件结果" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.visitSatisfaction" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item label="不满意原因" prop="OrgNoSatisfiedReason">
					<el-select v-model="state.queryParams.OrgNoSatisfiedReason" placeholder="请选择不满意原因" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.dissatisfiedReason" :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="businessSecondHandleApply">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { secondHandleBase, secondHandleExport, secondHandleList } from '@/api/business/secondHandle';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import other from '@/utils/other';
// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const SecondHandleDetail = defineAsyncComponent(() => import('@/views/business/secondHandle/components/Second-handle-detail.vue')); // 办理明细
const SecondHandleApply = defineAsyncComponent(() => import('@/views/business/secondHandle/components/Second-handle-apply.vue')); // 二次办理申请
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键字
		slTime: [], // 受理时间
		CreationTime: null, // 受理时间开始
		EndCreationTime: null, // 受理时间结束
		jbTime: [], // 接办时间
		ActualHandleTime: null, // 接办时间开始
		EndActualHandleTime: null, // 接办时间结束
		bjTime: [], // 办结时间
		FiledTime: null, // 办结时间开始
		EndFiledTime: null, // 办结时间结束
		hfTime: [], // 回访时间
		VisitTime: null, // 回访时间开始
		EndVisitTime: null, // 回访时间结束
		ActualHandleOrgName: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	screenStatus: [], // 甄别状态
	screenType: [], // 甄别类型
	counterSignTypeOptions: [], // 会签类型
	acceptTypeOptions: [], // 受理类型
	sourceChannelOptions: [], // 来源渠道
	dissatisfiedReason: [], // 不满意原因
	visitSatisfaction: [], // 满意度
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取基础数据
const getBaseData = async () => {
	try {
		const { result } = await secondHandleBase();
		state.screenStatus = result?.screenStatus ?? [];
		state.screenType = result?.screenType ?? [];
		state.counterSignTypeOptions = result?.counterSignType ?? [];
		state.acceptTypeOptions = result?.acceptType ?? [];
		state.sourceChannelOptions = result?.sourceChannel ?? [];
		state.visitSatisfaction = result?.visitSatisfaction ?? [];
		state.dissatisfiedReason = result?.dissatisfiedReason ?? [];
	} catch (e) {
		console.log(e);
	}
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	requestParams.value = other.deepClone(state.queryParams);
	requestParams.value.CreationTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[0];
	requestParams.value.EndCreationTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[1];
	Reflect.deleteProperty(requestParams.value, 'slTime'); // 受理时间
	requestParams.value.ActualHandleTime = state.queryParams.jbTime === null ? null : state.queryParams.jbTime[0];
	requestParams.value.EndActualHandleTime = state.queryParams.jbTime === null ? null : state.queryParams.jbTime[1];
	Reflect.deleteProperty(requestParams.value, 'jbTime'); // 接办时间
	requestParams.value.FiledTime = state.queryParams.bjTime === null ? null : state.queryParams.bjTime[0];
	requestParams.value.EndFiledTime = state.queryParams.bjTime === null ? null : state.queryParams.bjTime[1];
	Reflect.deleteProperty(requestParams.value, 'bjTime'); // 办结时间
	requestParams.value.VisitTime = state.queryParams.hfTime === null ? null : state.queryParams.hfTime[0];
	requestParams.value.EndVisitTime = state.queryParams.hfTime === null ? null : state.queryParams.hfTime[1];
	Reflect.deleteProperty(requestParams.value, 'hfTime'); // 回访时间
	state.loading = true;
	secondHandleList(requestParams.value)
		.then((res) => {
			state.tableData = res?.result.items ?? [];
			state.total = res?.result.total;
			state.loading = false;
		})
		.finally(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
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();
};
// 申请二次办理
const secondHandleApplyRef = ref<RefType>();
const onApply = (row: any) => {
	if (row.secondaryHandling) {
		// 如果有值表示修改
		secondHandleApplyRef.value.openDialog(row, true);
	} else {
		// 没有表示新增
		secondHandleApplyRef.value.openDialog(row);
	}
};
// 办理明细
const secondHandleDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	secondHandleDetailRef.value.openDialog(row.secondaryHandling?.id);
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
