<template>
	<div class="business-delay-audit-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<el-tabs v-model="state.queryParams.IsApply" @tab-change="handleClick">
				<el-tab-pane name="false" label="延期待办"></el-tab-pane>
				<el-tab-pane name="true" label="延期已办"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<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>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
			<!-- 表格 -->
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:total="state.total"
				v-model:page-index="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:key="Math.random()"
			>
				<template #expiredStatus="{ row }">
					<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
				</template>
				<template #isProvince="{ row }">
					<span>{{ row.order?.isProvince ? '省工单' : '市工单' }}</span>
				</template>
				<template #title="{ row }">
					<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</template>
				<template #employeeName="{ row }">
					<span
						>{{ row.order?.acceptorName }} <span v-if="row.order?.acceptorStaffNo">[{{ row.acceptorStaffNo }}]</span>
					</span>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onDetail(row)" title="延期详情"> 延期详情 </el-button>
					<order-detail :order="row.order" @updateList="queryList" />
				</template>
			</ProTable>
		</el-card>
		<!--  延期详情  -->
		<delay-detail-com ref="delayDetailRef" @updateList="queryList" />
		<!--  延期修改  -->
		<delay-edit ref="delayEditRef" @updateList="queryList" />
	</div>
</template>
<script setup lang="tsx" name="businessDelayAudit">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import { delayList } from '@/api/todo/delay';
import { delayDetail } from '@/api/business/delay';
// 引入组件
const DelayDetailCom = defineAsyncComponent(() => import('@/views/business/delay/components/Delay-detail.vue')); // 延期详情
const DelayEdit = defineAsyncComponent(() => import('@/views/business/delay/components/Delay-edit.vue')); // 延期修改
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情

// 定义变量内容
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键字
		IsApply: 'false', // 待办/已办
	},
	tableData: [], // 表格
	loading: false, // 加载
	total: 0, // 总数
});
const ruleFormRef = ref<RefType>(); // 表单ref
const router = useRouter(); // 路由
const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 55 },
	{ prop: 'expiredStatus', label: '超期状态', align: 'center' },
	{ prop: 'no', label: '工单编码', width: 150 },
	{ prop: 'isProvince', label: '省/市工单', width: 100 },
	{ prop: 'title', label: '工单标题', width: 300 },
	{ prop: 'delayStateText', label: '延期审批状态', width: 120 },
	{
		prop: 'order.startTime',
		label: '受理时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'employeeName', label: '受理人', width: 120 },
	{ prop: 'order.hotspotName', label: '热点分类', width: 200 },
	{ prop: 'order.acceptType', label: '受理类型', width: 150 },
	{ prop: 'order.orgLevelOneName', label: '一级部门', width: 150 },
	{ prop: 'order.actualHandleOrgName', label: '接办部门', width: 150 },
	{
		prop: 'order.actualHandleTime',
		label: '接办时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.actualHandleTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'creationTime',
		label: '延期申请时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'creatorOrgName', label: '延期申请部门', width: 120 },
	{ prop: 'creatorName', label: '延期申请人', width: 150 },
	{ prop: 'delayNum', label: '延期申请时限', width: 120 },
	{ prop: 'delayUnitText', label: '延期申请单位', width: 120 },
	{ prop: 'delayReason', label: '申请理由', width: 120 },
	{
		prop: 'beforeDelay',
		label: '申请前期满时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.beforeDelay, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'afterDelay',
		label: '通过后期满时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.afterDelay, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 160, align: 'center' },
]);
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	delayList(state.queryParams)
		.then((res: any) => {
			const index = columns.value.findIndex((item) => item.prop === 'delayStateText');
			if (state.queryParams.IsApply === 'true') {
				if (!columns.value.find((item) => item.prop === 'actualHandleStepName')) {
					columns.value.splice(
						index,
						0,
						{ prop: 'actualHandleStepName', label: '当前节点', width: 150 },
						{ prop: 'actualHandlerName', label: '当前审批人', width: 150 }
					);
				}
			} else {
				if (columns.value.find((item) => item.prop === 'actualHandleStepName')) {
					columns.value = columns.value.filter((item) => item.prop !== 'actualHandleStepName' && item.prop !== 'actualHandlerName');
				}
			}
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 切换tab 查询列表
const handleClick = () => {
	handleQuery();
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 延期详情
const delayDetailRef = ref<RefType>();
const delayEditRef = ref<RefType>();
const onDetail = async (row: any) => {
	try {
		const { result } = await delayDetail(row.id);
		if (result.handle) {
			// 退回到了开始 需要重新打开编辑页面在发起流程
			delayEditRef.value.openDialog(row);
		} else {
			delayDetailRef.value.openDialog(row);
		}
	} catch (e) {
		console.log(e);
	}
};
onMounted(() => {
	queryList();
});
</script>
