<template>
	<div class="business-publish-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="state.loading">
						<el-form-item label="工单编码" prop="Keyword">
							<el-input v-model="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)" class="default-button" :loading="state.loading">
								<SvgIcon name="ele-Refresh" class="mr5" />重置
							</el-button>
						</el-form-item>
					</el-form>
				</template>
				<template #statusText="{ 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>
				<template #order_detail="{ row }">
					<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
				</template>
				<template #action="{ row }">
					<el-button link type="primary" @click="onDetail(row)" title="查看修改明细"> 查看明细 </el-button>
				</template>
				<template #pager>
					<pagination
						@pagination="queryList"
						:total="state.total"
						v-model:current-page="state.queryParams.PageIndex"
						v-model:page-size="state.queryParams.PageSize"
						:disabled="state.loading"
					/>
				</template>
			</vxe-grid>
		</div>
		<edit-record ref="orderPublishEditRef" />
	</div>
</template>
<script setup lang="tsx" name="todoEditRecord">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { publishedList } from '@/api/business/publish';
import { getOrderModifyRecord } from '@/api/todo/center';
import { exportOrder } from '@/api/business/order';
// 引入组件
const EditRecord = defineAsyncComponent(() => import('@/views/todo/edit/components/Edit-record.vue')); // 修改记录详情
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键词
		No: null,
		Resolve: null, // 处理结果
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
	},
	customConfig: {
		storage: true,
	},
	id: 'todoEditRecord',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{
			field: 'expiredStatusText',
			title: '状态',
			width: 60,
			align: 'center',
			slots: {
				default: ({ row }) => {
					return <span class={'overdue-status-' + row.expiredStatus} title={row.expiredStatusText}></span>;
				},
			},
		},
		{
			field: 'statusText',
			title: '工单状态',
			width: 110,
			slots: {
				default: 'statusText',
			},
		},
		{ field: 'no', title: '工单编码', width: 140 },
		{
			field: 'title',
			title: '工单标题',
			minWidth: 200,
			slots: { default: 'order_detail' },
		},
		{ field: 'auditUserName', title: '修改人', width: 140 },
		{
			field: 'creationTime',
			title: '修改时间',
			width: 160,
			formatter: 'formatDate',
		},
		{ title: '操作', width: 90, fixed: 'right', align: 'center', slots: { default: 'action' } },
	],
	data: [],
	sortConfig: {
		remote: true,
	},
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	getOrderModifyRecord(state.queryParams)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			gridOptions.data = state.tableData;
			state.loading = false;
			gridOptions.loading = false;
		})
		.catch((err: any) => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 查看明细
const orderPublishEditRef = ref<RefType>();
const onDetail = (row: any) => {
	orderPublishEditRef.value.openDrawer(row.copyId);
};
onMounted(() => {
	queryList();
});
</script>
