<template>
	<div class="business-delay-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="数据范围" prop="DataScope" v-auth="'business:delay:DataScope'">
					<el-segmented
						:options="[
							{
								value: 1,
								label: '我的',
							},
							{
								value: 0,
								label: '全部',
							},
						]"
						v-model="state.queryParams.DataScope"
						@change="handleQuery"
						:disabled="state.loading"
					/>
				</el-form-item>
				<el-form-item label="快捷查询" prop="QueryDelayState">
					<el-segmented :options="queryDelayState" v-model="state.queryParams.QueryDelayState" @change="handleQuery" :disabled="state.loading" />
				</el-form-item>
				<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 label="延期状态" prop="DelayState">
					<el-select v-model="state.queryParams.DelayState" placeholder="请选择延期状态" @change="handleQuery">
						<el-option v-for="item in delayStateOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</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="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
					:print-config="{}"
					:scrollY="{ enabled: true, gt: 100 }"
					id="orderDelay"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
				>
					<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="queryList">{{ row.order?.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="automaticDelayNum" title="自动延期次数" width="120" v-if="['ZiGong'].includes(themeConfig.appScope)"></vxe-column>
					<vxe-column field="currentStepName" title="当前节点" width="120"></vxe-column>
					<vxe-column field="actualHandlerName" title="审批人" width="120"></vxe-column>
					<vxe-column field="delayStateText" title="延期申请状态" width="110"></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.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">
						<template #default="{ row }">
							<el-button link type="primary" @click="onDetail(row)" title="查看延期详情"> 延期详情 </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="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="CurrentStepName">
					<!--					<el-select v-model="state.queryParams.CurrentStepName" placeholder="当前节点" @change="handleQuery" clearable>
						<el-option v-for="item in state.visitTypeOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>-->
					<el-input v-model.trim="state.queryParams.CurrentStepName" placeholder="当前节点" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="审批人" prop="ActualHandlerName">
					<el-input v-model.trim="state.queryParams.ActualHandlerName" placeholder="审批人" clearable @keyup.enter="handleQuery" />
				</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="sqTime">
					<el-date-picker
						v-model="state.queryParams.sqTime"
						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>
	</div>
</template>
<script setup lang="tsx" name="orderDelay">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { delayBaseData, delayDetail, delayList } from '@/api/business/delay';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
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 storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键字
		DelayState: null,
		DataScope: 1, // 默认我的
		QueryDelayState: -1, // 快捷查询 默认全部
		CurrentStepName: null, // 当前节点
		ActualHandlerName: null, // 审批人
		OrgLevelOneName: null, // 一级部门
		sqTime: [], // 延期申请时间
		StartCreationTime: null,
		EndCreationTime: null,
	},
	tableData: [], //表格
	loading: false, // 加载
	total: 0, // 总数
	currentRow: {}, // 当前行
});
// 获取查询条件基础信息
const delayStateOptions = ref<EmptyArrayType>([]); // 延期状态
const queryDelayState = ref<EmptyArrayType>([]); // 延期状态快捷查询
const getBaseData = async () => {
	const res: any = await delayBaseData();
	delayStateOptions.value = res.result?.delayState ?? [];
	queryDelayState.value = res.result?.queryDelayState
		.map((item: any) => {
			return {
				label: item.value,
				value: item.key,
			};
		})
		.sort((a, b) => a.value - b.value);
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	return new Promise((resolve, reject) => {
		state.loading = true;
		requestParams.value = Other.deepClone(state.queryParams);
		requestParams.value.StartCreationTime = state.queryParams.sqTime === null ? null : state.queryParams.sqTime[0];
		requestParams.value.EndCreationTime = state.queryParams.sqTime === null ? null : state.queryParams.sqTime[1];
		Reflect.deleteProperty(requestParams.value, 'sqTime');
		delayList(requestParams.value)
			.then((res: any) => {
				state.tableData = res.result?.items ?? [];
				state.total = res.result?.total ?? 0;
				state.loading = false;
				resolve(res);
			})
			.catch((err: any) => {
				state.loading = false;
				reject(err);
			});
	});
};

/** 重置按钮操作 */
const drawerRuleFormRef = ref();
const drawer = ref(false);
const ruleFormRef = ref<RefType>(); // 表单ref
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
};
const delayDetailRef = ref<RefType>();
// 延期详情
const onDetail = async (row: any) => {
	delayDetailRef.value.openDialog(row);
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(async () => {
	queryList().then(() => {
		getBaseData();
	});
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
