<template>
	<div class="province-zmhd-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="IsProvinceZmhd">
					<el-segmented
						:options="[
							{
								value: '',
								label: '全部',
							},
							{
								value: 'false',
								label: '未处理',
							},
							{
								value: 'true',
								label: '已公开',
							},
						]"
						v-model="state.queryParams.IsProvinceZmhd"
						@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="provinceZmhd"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
				>
					<vxe-column field="expiredStatusText" title="超期状态" width="90" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.expiredStatus" :title="row.order?.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="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.status)">{{ row.statusText }}</el-text>
							<span v-else>{{ row.statusText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="isProvinceZmhd" title="公开状态" width="100">
						<template #default="{ row }">
							<span>{{ row.isProvinceZmhd ? '已公开' : '未处理' }}</span>
						</template>
					</vxe-column>
					<vxe-column field="currentStepName" title="当前节点" width="120"></vxe-column>
					<vxe-column field="isUrgentText" title="是否紧急" width="160">
						<template #default="{ row }">
							<span class="color-danger font-bold">{{ row.isUrgentText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="receiveProvinceNo" title="省编码" width="240"></vxe-column>
					<vxe-column field="no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="startTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.startTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="order" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="expiredTime" title="工单期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="acceptorName" title="受理人" width="120"></vxe-column>
					<vxe-column title="操作" fixed="right" width="80" align="center">
						<template #default="{ row }">
							<el-button link type="primary" v-if="row.isProvinceZmhd" @click="onDetail(row)">明细</el-button>
							<el-button link type="primary" v-else @click="onPublic(row)">公开</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>
		<!--  公开  -->
		<public-handle ref="publicHandleRef" @updateList="queryList" />
		<public-detail ref="publicDetailRef" />
		<!--	更多查询	-->
		<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="AnswerOu">
					<el-input v-model="state.queryParams.AnswerOu" placeholder="答复部门名称" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="初审人姓名" prop="AuditFirstName">
					<el-input v-model="state.queryParams.AuditFirstName" placeholder="初审人姓名" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="审核时间" prop="auditTime">
					<el-date-picker
						v-model="state.queryParams.auditTime"
						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="provinceZmhd">
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 { provinceZmhdList } from '@/api/province';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const PublicHandle = defineAsyncComponent(() => import('@/views/province/zmhd/components/Public-handle.vue')); // 公开
const PublicDetail = defineAsyncComponent(() => import('@/views/province/zmhd/components/Public-detail.vue')); // 公开详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		// 查询条件
		No: null, // 工单编码
		ReceiveProvinceNo: null, // 省编码
		Title: null, // 标题
		AnswerOu: null, // 回答部门名称
		AuditFirstName: null, // 初审人姓名
		auditTime: [], // 审核时间
		StartTime: null, // 审核开始时间
		EndTime: null, // 审核结束时间
		IsProvinceZmhd: '', // 是否政民互动公开
	},
	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.StartTime = state.queryParams.auditTime === null ? null : state.queryParams.auditTime[0]; // 审核时间
	requestParams.value.EndTime = state.queryParams.auditTime === null ? null : state.queryParams.auditTime[1];
	Reflect.deleteProperty(requestParams.value, 'auditTime'); // 删除无用的参数
	state.loading = true;
	provinceZmhdList(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();
	state.queryParams.IsProvinceZmhd = '';
	ruleFormRef.value?.resetFields();
	queryList();
};
// 查看明细
const publicDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	publicDetailRef.value.openDrawer(row);
};
// 公开
const publicHandleRef = ref<RefType>();
const onPublic = (row: any) => {
	publicHandleRef.value.openDrawer(row);
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
