<template>
	<div class="business-discern-todo-container layout-pd">
		<el-card shadow="never">
			<!-- 功能按钮 -->
			<el-tabs v-model="state.queryParams.Status" class="demo-tabs" @tab-change="queryList">
				<el-tab-pane name="0" label="甄别待办"></el-tab-pane>
				<el-tab-pane name="4" 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="queryList" class="keyword-input" />
				</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="timeStartChangeCr"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :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.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
				</template>
				<template #isProvince="{ row }">
					<span>{{ row.order?.isProvince ? '省工单' : '市工单' }}</span>
				</template>
				<template #title="{ row }">
					<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</template>
				<template #acceptorName="{ row }">
					<span
						>{{ row.order?.acceptorName }} <span v-if="row.order?.acceptorStaffNo">[{{ row.order?.acceptorStaffNo }}]</span>
					</span>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onDetail(row)" title="查看甄别详情"> 甄别详情 </el-button>
					<order-detail :order="row.order" @updateList="queryList" />
				</template>
			</ProTable>
		</el-card>
		<!-- 甄别详情 -->
		<discern-detail ref="discernDetailRef" @updateList="queryList" />
		<!-- 甄别修改 -->
		<discern-edit ref="discernEditRef" @updateList="queryList" />
	</div>
</template>
<script setup lang="tsx" name="businessDiscernTodo">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { shortcuts } from '@/utils/constants';
import { formatDate } from '@/utils/formatTime';
import { screenBaseData, screenDetail, screenList } from '@/api/business/discern';
import other from '@/utils/other';

// 引入组件
const DiscernDetail = defineAsyncComponent(() => import('@/views/business/discern/components/Discern-detail.vue')); // 甄别详情
const DiscernEdit = defineAsyncComponent(() => import('@/views/business/discern/components/Discern-edit.vue'));
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 55 },
	{ prop: 'expiredStatus', label: '超期状态', align: 'center' },
	{ prop: 'order.no', label: '工单编码', width: 150 },
	{ prop: 'isProvince', label: '省/市工单', width: 100 },
	{ prop: 'title', label: '工单标题', width: 300 },
	{ prop: 'order.sourceChannel', label: '来源方式', width: 100 },
	{ prop: 'order.acceptType', label: '受理类型', width: 150 },
	{ prop: 'order.acceptorName', label: '受理人', width: 120 },
	{ prop: 'order.orgLevelOneName', label: '一级部门', width: 150 },
	{ prop: 'order.actualHandleOrgName', label: '接办部门', width: 150 },
	{
		prop: 'order.actualHandleTime',
		label: '接办时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.actualHandleTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'order.startTime',
		label: '受理时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'visit.visitTime',
		label: '回访时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.visit?.visitTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'creationTime',
		label: '甄别申请时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'creatorName', label: '甄别申请人', width: 120 },
	{ prop: 'creatorOrgName', label: '甄别申请部门', width: 150 },
	{ prop: 'typeDicName', label: '甄别申请类型', width: 120 },
	{ prop: 'content', label: '甄别申请原因', width: 200 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 160, align: 'center' },
]);
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键字
		CreationTimeStart: null, // 创建时间 开始
		CreationTimeEnd: null, // 创建时间 结束
		exTime: [], // 办理期限
		Status: '0',
		source: '1', // 甄别待办 1 甄别已办 1
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	screenStatus: [], // 甄别状态
	screenType: [], // 甄别类型
});
const handleTimeChange = (val: string[], startKey: string, endKey: string) => {
	if (val) {
		state.queryParams[startKey] = val[0];
		state.queryParams[endKey] = val[1];
	} else {
		state.queryParams[startKey] = null;
		state.queryParams[endKey] = null;
	}
};
// 受理时间
const timeStartChangeCr = (val: string[]) => {
	handleTimeChange(val, 'CreationTimeStart', 'CreationTimeEnd');
};
// 获取查询条件基础信息
const getBaseData = async () => {
	const res: any = await screenBaseData();
	state.screenStatus = res.result?.screenStatus ?? [];
	state.screenType = res.result?.screenType ?? [];
};
/** 获取列表 */
const queryList = throttle(() => {
	const index = columns.value.findIndex((item) => item.prop === 'title');
	let request = other.deepClone(state.queryParams);
	Reflect.deleteProperty(request, 'exTime'); // 删除无用的参数
	state.loading = true;
	screenList(request)
		.then((response: any) => {
			if (state.queryParams.Status === '4') {
				if (!columns.value.find((item) => item.prop === 'statusText')) {
					columns.value.splice(
						index,
						0,
						{ prop: 'statusText', label: '甄别状态', width: 120 },
						{ prop: 'actualHandleStepName', label: '当前节点', width: 150 },
						{ prop: 'actualHandlerName', label: '当前审批人', width: 150 }
					);
				}
			} else {
				if (columns.value.find((item) => item.prop === 'statusText')) {
					columns.value = columns.value.filter(
						(item) => item.prop !== 'statusText' && item.prop !== 'actualHandleStepName' && item.prop !== 'actualHandlerName'
					);
				}
			}
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
}, 500);
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.CreationTimeStart = null;
	state.queryParams.CreationTimeEnd = null;
	queryList();
}, 500);
// 导出
const onExport = () => {
	console.log('导出');
};
// 查看详情
const discernDetailRef = ref<RefType>(); // 甄别详情ref
const discernEditRef = ref<RefType>(); // 甄别修改ref
const onDetail = async (row: any) => {
	try {
		const { result } = await screenDetail(row.id);
		console.log(result);
		if (result.handle) {
			// 退回到了开始 需要重新打开编辑页面在发起流程
			discernEditRef.value.openDialog(row);
		} else {
			discernDetailRef.value.openDialog(row);
		}
	} catch (e) {
		console.log(e);
	}
};
onMounted(async () => {
	await getBaseData();
	queryList();
});
</script>
