<template>
	<div class="todo-order-container layout-pd">
		<el-card shadow="never">
			<el-tabs v-model="state.queryParams.IsHandled" @tab-change="handleQuery">
				<el-tab-pane name="false" label="工单待办"></el-tab-pane>
				<el-tab-pane name="true" label="工单已办"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<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 label="是否省工单" prop="IsProvince">
					<el-select v-model="state.queryParams.IsProvince" placeholder="请选择是否省工单" @change="handleQuery">
						<el-option label="是" value="true" />
						<el-option label="否" value="false" />
					</el-select>
				</el-form-item>
				<el-form-item label="是否会签" prop="IsCounterSign">
					<el-select v-model="state.queryParams.IsCounterSign" placeholder="请选择是否会签" @change="handleQuery">
						<el-option label="是" value="true" />
						<el-option label="否" value="" />
					</el-select>
				</el-form-item>
				<el-form-item label="超期状态" prop="ExpiredOrAlmostOverdue">
					<el-select v-model="state.queryParams.ExpiredOrAlmostOverdue" placeholder="请选择超期状态" @change="handleQuery">
						<el-option label="已超期" value="true" />
						<el-option label="即将超期" value="false" />
					</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="resetQuery(ruleFormRef)" v-waves class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
			<!-- 表格 -->
			<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"
				:key="Math.random()"
			>
				<template #expiredStatus="{ row }">
					<span :class="'overdue-status-' + row.expiredStatus" :title="row.expiredStatusText"></span>
				</template>
				<template #isProvince="{ row }">
					<span>{{ row.isProvince ? '省工单' : '市工单' }}</span>
				</template>
				<template #title="{ row }">
					<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
				</template>
				<template #employeeName="{ row }">
					<span
						>{{ row.acceptorName }} <span v-if="row.acceptorStaffNo">[{{ row.acceptorStaffNo }}]</span>
					</span>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<order-detail :order="row" @updateList="queryList" />
				</template>
			</ProTable>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="todoOrder">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import { orderListTodo } from '@/api/todo/order';
// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
// 定义变量内容
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		IsHandled: 'false', // 工单状态
		Keyword: null, // 关键字
		IsProvince: null, // 是否省工单
		IsCounterSign: null, // 是否会签
		ExpiredOrAlmostOverdue: null, // 超期状态
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
const ruleFormRef = ref<RefType>(); // 表单ref
const router = useRouter(); // 路由
const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'expiredStatus', label: '超期状态', align: 'center',fixed: 'left' },
	{ prop: 'no', label: '工单编码', width: 150 },
	{ prop: 'isProvince', label: '省/市工单', width: 100 },
	{ prop: 'actualHandleStepName', label: '办理节点', width: 150 },
	{ prop: 'statusText', label: '工单状态', width: 100 },
	{ prop: 'title', label: '工单标题', width: 300 },
	{ prop: 'counterSignTypeText', label: '是否会签', width: 100 },
	{
		prop: 'creationTime',
		label: '生成时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'startTime',
		label: '受理时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'expiredTime',
		label: '工单期满时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.expiredTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'acceptType', label: '受理类型', width: 150 },
	{ prop: 'emergencyLevelText', label: '紧急程度', width: 150 },
	{ prop: 'sourceChannel', label: '来源方式', width: 100 },
	{ prop: 'hotspotName', label: '热点分类', width: 200 },
	{ prop: 'employeeName', label: '受理人', width: 120 },
	{
		prop: 'reTransactNum',
		label: '重办次数',
		render: (scope) => {
			return <span>{scope.row.reTransactNum ? scope.row.reTransactNum : 0}</span>;
		},
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 100, align: 'center' },
]);
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = async () => {
	try {
		state.loading = true;
		const index = columns.value.findIndex((item) => item.prop === 'acceptType');
		if (state.queryParams.IsHandled === 'true') {
			if (!columns.value.find((item) => item.prop === 'orgLevelOneName')) {
				columns.value.splice(
					index,
					0,
					{ prop: 'orgLevelOneName', label: '一级部门', width: 150 },
					{ prop: 'actualHandleOrgName', label: '接办部门', width: 150 }
				);
			}
		} else {
			if (columns.value.find((item) => item.prop === 'orgLevelOneName')) {
				columns.value = columns.value.filter((item) => item.prop !== 'orgLevelOneName' && item.prop !== 'actualHandleOrgName');
			}
		}
		const res: any = await orderListTodo(state.queryParams);
		state.tableData = res.result?.items ?? [];
		state.total = res.result?.total ?? 0;
		state.loading = false;
	} catch (e) {
		state.loading = false;
	}
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
const historyParams = history.state;
onMounted(() => {
	if (historyParams.IsCountersign) {
		// 会签待办
		// 先重置其他查询条件
		ruleFormRef.value.resetFields();
		state.queryParams = { ...state.queryParams, IsCountersign: historyParams.IsCountersign };
	}
	if (historyParams.ExpiredOrAlmostOverdue) {
		// 即将超期 已超期
		ruleFormRef.value.resetFields();
		state.queryParams = { ...state.queryParams, ExpiredOrAlmostOverdue: historyParams.ExpiredOrAlmostOverdue };
	}
	queryList();
});
</script>
