<template>
	<div class="business-discern-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<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="Status">
					<el-select v-model="state.queryParams.Status" placeholder="请选择甄别申请状态" @change="handleQuery">
						<el-option v-for="item in state.screenStatus" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<!--						<el-form-item label="甄别类型" prop="AcceptType">
							<el-select v-model="state.queryParams.AcceptType" placeholder="请选择甄别类型" class="w100">
								<el-option v-for="item in state.screenType" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
							</el-select>
						</el-form-item>-->
				<el-form-item label="甄别时间" prop="exTime">
					<el-date-picker
						v-model="state.queryParams.exTime"
						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="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>
		</el-card>
		<el-card shadow="never">
			<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 #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="businessDiscern">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { shortcuts } from '@/utils/constants';
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 AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue')); // 附件列表
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'order.no', label: '工单编码', width: 150 },
	{ prop: 'statusText', label: '甄别状态', width: 100 },
	{ prop: 'isProvince', label: '省/市工单', width: 100 },
	{ prop: 'order.title', label: '工单标题', width: 300 },
	{ prop: 'order.sourceChannel', label: '来源方式', width: 120 },
	{ prop: 'order.acceptType', label: '受理类型', width: 120 },
	{ prop: 'order.hotspotName', label: '热点分类', width: 200 },
	{ prop: 'acceptorName', label: '受理人', width: 170 },
	{ prop: 'order.orgLevelOneName', label: '一级部门', width: 150 },
	{ prop: 'order.actualHandleOrgName', label: '接办部门', width: 150 },
	{
		prop: 'order.startTime',
		label: '受理时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'order.filedTime',
		label: '办结时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.filedTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'visit.publishTime',
		label: '发布时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.visit?.publishTime, '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: 'order.counterSignTypeText', label: '是否会签', width: 100 },
	{ prop: 'visit.employee', label: '回访人' },
	{ prop: 'visitDetail.visitOrgName', label: '被回访部门', width: 150 },
	{
		prop: 'creationTime',
		label: '甄别申请时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'creatorName', label: '甄别申请人', width: 170 },
	{ prop: 'creatorOrgName', label: '甄别申请部门', width: 200 },
	{ prop: 'typeDicName', label: '甄别申请类型', width: 170 },
	{ prop: 'content', label: '甄别申请原因', width: 200 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 170, align: 'center' },
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键字
		CreationTimeStart: null, // 创建时间 开始
		CreationTimeEnd: null, // 创建时间 结束
		exTime: [], // 办理期限
		source: '2', // 甄别列表 2
	},
	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] = '';
		state.queryParams[endKey] = '';
	}
	handleQuery();
};
// 甄别时间
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 ?? [];
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	let request = other.deepClone(state.queryParams);
	Reflect.deleteProperty(request, 'exTime'); // 删除无用的参数
	state.loading = true;
	screenList(request)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};

/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.CreationTimeStart = null;
	state.queryParams.CreationTimeEnd = null;
	queryList();
};
// 查看详情
const discernDetailRef = ref<RefType>(); // 甄别详情ref
const discernEditRef = ref<RefType>(); // 甄别修改ref
const onDetail = async (row: any) => {
	try {
		const { result } = await screenDetail(row.id);
		if (result.handle) {
			// 退回到了开始 需要重新打开编辑页面在发起流程
			discernEditRef.value.openDialog(row);
		} else {
			discernDetailRef.value.openDialog(row);
		}
	} catch (e) {
		console.log(e);
	}
};
onMounted(async () => {
	await getBaseData();
	queryList();
});
</script>
