<template>
	<div class="business-delay-audit-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-tabs v-model="state.queryParams.IsApply" @tab-change="handleClick">
				<el-tab-pane name="false" label="延期待审批" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane name="true" label="延期已审批" :disabled="state.loading"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="state.loading">
				<el-form-item label="关键字" prop="Keyword">
					<el-input v-model.trim="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>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }]"
			>
				<template #buttons>
					<el-button
						type="primary"
						@click="onAuditBatch"
						:disabled="isChecked"
						v-if="state.queryParams.IsApply === 'false'"
						:loading="state.loading"
						v-auth="'business:delay:audit:batch'"
						><SvgIcon name="ele-Edit" class="mr5" />批量审批<span v-if="checkTable.length">({{ checkTable.length }})</span>
					</el-button>
				</template>
			</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"
					@checkbox-all="selectAllChangeEvent"
					@checkbox-change="selectChangeEvent"
					auto-resize
					show-overflow
					:print-config="{}"
					:scrollY="{ enabled: true, gt: 100 }"
					id="businessDelayAudit"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
					:params="{ exportMethod: waitDelayListExport, exportParams: requestParams }"
				>
					<vxe-column type="checkbox" width="50" align="center"></vxe-column>
					<vxe-column field="order.expiredStatusText" title="状态" width="60" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="order.no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="order.isProvinceText" title="省/市工单" width="90"></vxe-column>
					<vxe-column field="order.title" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="row.order" @updateList="refreshList">{{ row.order?.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="currentStepName" title="当前节点" width="120" v-if="state.queryParams.IsApply === 'true'"></vxe-column>
					<vxe-column field="actualHandlerName" title="当前审批人" width="120" v-if="state.queryParams.IsApply === 'true'"></vxe-column>
					<vxe-column field="delayStateText" title="延期审批状态" width="110"></vxe-column>
					<vxe-column field="order.creationTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="order.acceptorName" title="受理人" width="120"></vxe-column>
					<vxe-column field="order.hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="order.acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="order.orgLevelOneName" 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="creationTime" title="延期申请时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="creatorName" title="延期申请人" width="120"></vxe-column>
					<vxe-column field="creatorOrgName" title="延期申请部门" width="140"></vxe-column>
					<vxe-column field="delayNum" title="延期申请时限" width="110"></vxe-column>
					<vxe-column field="delayUnitText" title="延期申请单位" width="110"></vxe-column>
					<vxe-column field="delayReason" title="申请理由" width="150"></vxe-column>
					<vxe-column field="beforeDelay" title="申请前期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.beforeDelay, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<!--					<vxe-column field="afterDelay" title="通过后期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.afterDelay, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>-->
					<vxe-column title="操作" fixed="right" width="90" align="center" :show-overflow="false">
						<template #default="{ row }">
							<el-button link type="primary" @click="onDetail(row)" title="延期详情" v-if="state.queryParams.IsApply === 'true'">
								延期详情
							</el-button>
							<el-button
								link
								type="primary"
								@click="onAudit(row)"
								title="审批"
								v-auth="'business:delay:audit:todo'"
								v-if="state.queryParams.IsApply === 'false'"
							>
								审批
							</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>
		<!--  延期详情  -->
		<delay-detail-com ref="delayDetailRef" @updateList="refreshList" />
		<!--  延期审批  -->
		<delay-audit ref="delayAuditRef" @updateList="refreshList" />
		<!--  延期审批批量  -->
		<delay-audit-batch ref="delayAuditBatchRef" @updateList="refreshList" />
	</div>
</template>
<script setup lang="tsx" name="businessDelayAudit">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { delayList, waitDelayListExport } from '@/api/todo/delay';
import Other from '@/utils/other';
// 引入组件
const DelayDetailCom = defineAsyncComponent(() => import('@/views/business/delay/components/Delay-detail.vue')); // 延期详情
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const DelayAudit = defineAsyncComponent(() => import('@/views/business/delay/components/Delay-audit.vue')); // 延期审批
const DelayAuditBatch = defineAsyncComponent(() => import('@/views/business/delay/components/Delay-audit-batch.vue')); // 延期审批 批量

// 定义变量内容
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键字
		IsApply: 'false', // 待办/已办
	},
	tableData: [], // 表格
	loading: false, // 加载
	total: 0, // 总数
});
const ruleFormRef = ref<RefType>(); // 表单ref
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 刷新列表
const refreshList = () => {
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.IsApply = state.queryParams.IsApply === 'true';
	delayList(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			tableRef.value.clearCheckboxRow();
			checkTable.value = [];
			state.loading = false;
		})
		.catch(() => {
			tableRef.value.clearCheckboxRow();
			checkTable.value = [];
			state.loading = false;
		});
};
// 切换tab 查询列表
const handleClick = () => {
	handleQuery();
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 延期详情
const delayDetailRef = ref<RefType>();
const onDetail = async (row: any) => {
	delayDetailRef.value.openDialog(row);
};
const toolbarRef = ref<RefType>();
const checkTable = ref<EmptyArrayType>([]);
const selectAllChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !Boolean(checkTable.value.length);
});
// 延期审批
const delayAuditRef = ref<RefType>();
const onAudit = (row: any) => {
	delayAuditRef.value.openDialog(row);
};
// 批量审批
const delayAuditBatchRef = ref<RefType>();
const onAuditBatch = () => {
	delayAuditBatchRef.value.openDialog(checkTable.value);
};
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
