<template>
	<div class="statistics-department-detail-delay-container layout-pd">
		<el-card shadow="never">
			<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"
				:toolButton="['refresh', 'setting', 'exportCurrent', 'exportAll']"
				:exportMethod="departmentDelayDetailExport"
				:exportParams="requestParams"
			>
				<template #expiredStatusText="{ row }">
					<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
				</template>
				<template #title="{ row }">
					<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</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" />
	</div>
</template>
<script setup lang="tsx" name="statisticsDepartmentDetailDelay">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { useRoute, useRouter } from 'vue-router';
import { delayDetail } from '@/api/business/delay';
import { departmentDelayDetail, departmentDelayDetailExport } from '@/api/statistics/department';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const DelayDetailCom = defineAsyncComponent(() => import('@/views/business/delay/components/Delay-detail.vue')); // 延期详情

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'order.expiredStatusText', label: '超期状态', align: 'center', fixed: 'left', width: 80 },
	{ prop: 'order.no', label: '工单编码', width: 150 },
	{ prop: 'order.isProvinceText', label: '省/市工单', width: 100 },
	{ prop: 'order.title', label: '工单标题', width: 300 },
	{ prop: 'order.actualHandleStepName', label: '办理节点', width: 120 },
	{ 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: 'order.acceptorName', label: '受理人', width: 120 },
	{ prop: 'order.hotspotName', label: '热点分类', width: 120 },
	{ prop: 'order.acceptType', label: '受理类型', width: 120 },
	{ prop: 'order.orgLevelOneName', label: '一级部门', width: 120 },
	{ prop: 'order.actualHandleOrgName', label: '接办部门', width: 120 },
	{
		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: 'creatorName', label: '延期申请人', width: 120 },
	{ prop: 'creatorOrgName', label: '延期申请部门', width: 120 },
	{ 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: 170, align: 'center' },
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const router = useRouter(); // 路由
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键字
	},
	tableData: [{}], //表单
	loading: false, // 加载
	total: 0, // 总数
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const route = useRoute();
const routeQueryParams = route.query;
const requestParams = ref({});
const queryList = () => {
	state.loading = true;
	requestParams.value = {
		PageIndex: state.queryParams.PageIndex,
		PageSize: state.queryParams.PageSize,
		StartTime: routeQueryParams?.StartTime,
		EndTime: routeQueryParams?.EndTime,
		OrgCode: routeQueryParams?.OrgCode,
		Type: routeQueryParams?.Type,
	};
	departmentDelayDetail(requestParams.value)
		.then((res) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch((err) => {
			console.log(err);
		})
		.finally(() => {
			state.loading = false;
		});
};

/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 延期详情
const delayDetailRef = ref<RefType>();
const onDetail = async (row: any) => {
	try {
		delayDetailRef.value.openDialog(row);
	} catch (e) {
		console.log(e);
	}
};
onMounted(async () => {
	queryList();
});
</script>
