<template>
	<div class="province-order-screen-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="State">
					<el-segmented
						:options="[
											{
												value: '0',
												label: '申请中',
											},
											{
												value: '1',
												label: '同意',
											},
											{
												value: '2',
												label: '不同意',
											},
										]"
						v-model="state.queryParams.State"
						@change="handleQuery"
						:disabled="state.loading"
					/>
				</el-form-item>
				<el-form-item label="工单标题" prop="Title">
					<el-input v-model="state.queryParams.Title" placeholder="工单标题" clearable @keyup.enter="handleQuery" class="keyword-input"/>
				</el-form-item>
				<el-form-item label="工单编码" prop="No">
					<el-input v-model="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
					:print-config="{}"
					:scrollY="{ enabled: true, gt: 100 }"
					id="provinceScreen"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
				>
					<vxe-column field="order.expiredStatusText" title="超期状态" width="90" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="order.statusText" title="工单状态" width="100">
						<template #default="{ row }">
							<el-text type="danger" tag="b" v-if="[1, 2, 3, 9, 101, 102, 103, 104, 105, 200].includes(row.order.status)">{{ row.order.statusText }}</el-text>
							<span v-else>{{ row.order.statusText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="creatorName" title="申请人" width="120"></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="auditName" title="审核人" width="120"></vxe-column>
					<vxe-column field="auditTime" title="审核时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.auditTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="stateText" title="审核状态" width="100"></vxe-column>
					<vxe-column field="order.currentStepName" title="当前节点" width="120"></vxe-column>
					<vxe-column field="order.isUrgentText" title="是否紧急" width="90">
						<template #default="{ row }">
							<span class="color-danger font-bold">{{ row.order?.isUrgentText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="order.receiveProvinceNo" title="省编码" width="240"></vxe-column>
					<vxe-column field="order.no" title="工单编码" width="140"></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="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.expiredTime" title="工单期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="order.actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="order.acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="order.hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="order.acceptorName" title="受理人" width="120"></vxe-column>
					<vxe-column title="操作" fixed="right" width="80" align="center">
						<template #default="{ row }">
							<el-button type="primary" link @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>
		<detail-drawer ref="detailDrawerRef" />
		<!--	更多查询	-->
		<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="ReceiveProvinceNo">
					<el-input v-model="state.queryParams.ReceiveProvinceNo" placeholder="省编码" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<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="ApplyUserName">
					<el-input v-model="state.queryParams.ApplyUserName" placeholder="申请人姓名" clearable @keyup.enter="handleQuery" />
				</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="provinceScreen">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { formatDate } from '@/utils/formatTime';
import Other from '@/utils/other';
import { provinceIdentifyList } from '@/api/province';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const DetailDrawer = defineAsyncComponent(() => import('@/views/province/components/Detail-drawer.vue')); // 明细
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		No: null, // 工单编码
		Title: null, // 工单标题
		State: '0', // 审核中
		ApplyUserName: null, // 申请人
		ReceiveProvinceNo: null, // 省编码
		crTime: [], // 申请时间
		ApplyStartTime: null,
		ApplyEndTime: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	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'); // 删除无用的参数
	state.loading = true;
	provinceIdentifyList(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 detailDrawerRef = ref<RefType>();
const onDetail = (row: any) => {
	detailDrawerRef.value.openDrawer(row.id, 'screen');
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>