<template>
	<div class="province-order-screen-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<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"
			>
				<template #table-search>
					<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"
									/>
								</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>
				</template>
				<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 type="primary" link @click="onDetail(row)" title="省工单甄别明细"> 明细</el-button>
				</template>
			</ProTable>
		</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 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, // 总数
});
const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'order.expiredStatusText', label: '超期状态', align: 'center', minWidth: 80 },
	{ prop: 'order.statusText', label: '工单状态', minWidth: 100 },
	{ prop: 'creatorName', label: '申请人', minWidth: 120 },
	{
		prop: 'creationTime',
		label: '申请时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'auditName', label: '审核人', minWidth: 120 },
	{
		prop: 'auditTime',
		label: '审核时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.auditTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'statusText', label: '审核状态', minWidth: 100 },
	{ prop: 'order.currentStepName', label: '当前节点', minWidth: 120 },
	{
		prop: 'order.isUrgentText',
		label: '是否紧急',
		render: (scope) => {
			return <span class="color-danger font-bold">{scope.row.order?.isUrgentText}</span>;
		},
	},
	{ prop: 'order.receiveProvinceNo', label: '省编码', minWidth: 240 },
	{ prop: 'order.no', label: '工单编码', minWidth: 140 },
	{
		prop: 'order.startTime',
		label: '受理时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'order.title', label: '工单标题', minWidth: 200 },
	{
		prop: 'order.expiredTime',
		label: '工单期满时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.expiredTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'order.actualHandleOrgName', label: '接办部门', minWidth: 140 },
	{ prop: 'order.acceptType', label: '受理类型', minWidth: 100 },
	{ prop: 'order.hotspotName', label: '热点分类', minWidth: 150 },
	{ prop: 'order.acceptorName', label: '受理人', minWidth: 120 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 80, align: 'center' },
]);
// 手动查询，将页码设置为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');
};
onMounted(() => {
	queryList();
});
</script>