<template>
	<div class="business-terminate-apply-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent class="mb10" inline :disabled="state.loading">
				<el-form-item label="工单标题" prop="Title">
					<el-input v-model.trim="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,
				}"
			>
			</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="businessTerminateApply"
					:custom-config="{ storage: true }"
					showHeaderOverflow
				>
					<vxe-column field="expiredStatusText" title="状态" width="60" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.expiredStatus" :title="row.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="currentStepName" title="当前节点" width="120"></vxe-column>
					<vxe-column field="no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="title" title="工单标题" min-width="200">
						<template #default="{ row }">
							<order-detail :order="row" @updateList="refreshList">{{ 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="actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="actualHandleTime" title="接办时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.actualHandleTime, '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="acceptType" title="受理类型" width="120"></vxe-column>
					<vxe-column field="hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column title="操作" fixed="right" width="240" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onDetail(row)" title="终止明细" v-if="row.orderTerminates?.length"> 终止明细 </el-button>
							<el-button link type="primary" @click="onProcessDetail(row)" title="流程明细" v-if="row.orderTerminates?.length"> 流程明细 </el-button>
							<el-button
								link
								type="primary"
								@click="onReApply(row)"
								title="重提申请"
								v-auth="'business:terminate:apply:apply'"
								v-if="row.orderTerminates?.length"
							>
								重提申请
							</el-button>
							<el-button link type="primary" @click="onApply(row)" title="终止申请" v-auth="'business:terminate:apply:apply'" v-else>
								终止申请
							</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" :disabled="state.loading">
				<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="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>
			<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>
		<!-- 审核记录 -->
		<audit-record ref="auditRecordRef" />
		<!-- 终止详情 -->
		<terminate-detail ref="terminateDetailRef" @updateList="refreshList" />
		<!-- 重提申请 -->
		<terminate-edit ref="terminateEditRef" @orderProcessSuccess="refreshList" />
		<!-- 终止申请 -->
		<terminate-apply ref="terminateApplyRef" @orderProcessSuccess="refreshList" />
	</div>
</template>
<script setup lang="tsx" name="businessTerminateApply">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { commonEnum, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import other from '@/utils/other';
import { formatDate } from '@/utils/formatTime';
import { terminatesApplyList } from '@/api/business/terminate';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const AuditRecord = defineAsyncComponent(() => import('@/components/AuditRecord/index.vue')); // 流程明细
const TerminateDetail = defineAsyncComponent(() => import('@/views/business/terminate/components/Terminate-detail.vue')); // 终止详情
const TerminateEdit = defineAsyncComponent(() => import('@/views/business/terminate/components/Terminate-edit.vue')); // 重提申请
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const TerminateApply = defineAsyncComponent(() => import('@/views/business/terminate/components/Terminate-apply.vue')); // 终止申请

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		Title: null, // 标题
		No: null, // 工单编号
		crTime: [], // 申请时间
		ApplyStartTime: null, // 申请时间
		ApplyEndTime: null, // 申请时间
		slTime: [], // 受理时间
		StartTime: null, // 受理开始时间
		EndTime: null, // 受理结束时间
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 刷新列表
const refreshList = () => {
  queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	requestParams.value = other.deepClone(state.queryParams);
	requestParams.value.ApplyStartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.ApplyEndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime');
	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');
	state.loading = true;
	terminatesApplyList(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			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 auditRecordRef = ref<RefType>();
const onProcessDetail = (row: any) => {
	const params = {
		dialogTitle: `终止记录 （${row?.title}）`,
		workflowId: row.orderTerminates[0].workflowId,
	};
	auditRecordRef.value.openDialog(params);
};
// 终止申请
const terminateApplyRef = ref<RefType>();
const onApply = (row: any) => {
	const params = {
		id: row.id,
		commonEnum: commonEnum.OrderCirculation,
		processType: '终止申请',
		orderDetail: row,
		extra: {
			dialogTitle: '终止申请',
			inputPlaceholder: '请填写终止理由',
			annexName: '终止附件',
		},
	};
	terminateApplyRef.value.openDialog(params)
};
// 终止详情
const terminateDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	const req = {
		id: row.orderTerminates[0].id,
	};
	terminateDetailRef.value.openDialog(req);
};
// 重提申请
const terminateEditRef = ref<RefType>();
const onReApply = (row: any) => {
	const req = {
		workflowId: row.orderTerminates[0].workflowId,
		id: row.orderTerminates[0].id,
	};
	terminateEditRef.value.openDialog(req);
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
