<template>
	<div class="todo-center-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<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"
				@sort-change="sortChange"
			>
				<template #table-search>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent class="mb10" inline>
						<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>
				</template>
				<template #tableHeader="scope">
					<el-button type="primary" @click="onJbExport" :disabled="!scope.isSelected" :loading="state.loading" v-auth="'todo:center:jbdExport'"
						><SvgIcon name="iconfont icon-daochu" class="mr5" />交办单导出<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
					</el-button>
					<el-button type="primary" @click="onToEnd" :disabled="!scope.isSelected" :loading="state.loading" v-auth="'todo:center:toEnd:multiple'"
					><SvgIcon name="ele-List" class="mr5" />批量归档<span v-if="proTableRef?.selectedList?.length">({{proTableRef?.selectedList?.length}})</span>
					</el-button>
					<el-button type="primary" @click="onAssignOrders" :loading="state.loading" v-auth="'todo:center:assignOrders'"
					><SvgIcon name="ele-List" class="mr5" />分配工单
					</el-button>
				</template>
				<template #expiredStatusText="{ row }">
					<span :class="'overdue-status-' + row.expiredStatus" :title="row.expiredStatusText"></span>
				</template>
				<template #title="{ row }">
					<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onSign(row)" title="签收工单" v-if="row.canSign" v-auth="'todo:center:sign'"> 签收 </el-button>
					<el-button link type="success" @click="onOrderEdit(row)" title="编辑工单" v-if="row.canEdit" v-auth="'todo:center:edit'"> 修改 </el-button>
					<el-button link type="primary" @click="onMigration(row)" title="平级移动" v-auth="'todo:center:migration'"> 平级移动 </el-button>
				</template>
			</ProTable>
		</div>
		<!-- 工单平移 -->
		<order-migration ref="orderMigrationRef" @updateList="queryList" />
		<!-- 批量归档 -->
		<to-end ref="toEndOrderRef" @updateList="queryList" />
		<!--	更多查询	-->
		<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="scTime">
					<el-date-picker
						v-model="state.queryParams.scTime"
						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-item label="受理时间" prop="slTime">
					<el-date-picker
						v-model="state.queryParams.slTime"
						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-item label="当前节点" prop="StepName">
					<el-select v-model="state.queryParams.StepName" placeholder="请选择当前节点" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.stepNamesOptions" :value="item" :key="item" :label="item" />
					</el-select>
				</el-form-item>
				<el-form-item label="派单员" prop="CenterToOrgHandlerName">
					<el-input v-model="state.queryParams.CenterToOrgHandlerName" placeholder="派单员" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="是否紧急" prop="IsUrgent">
					<el-select v-model="state.queryParams.IsUrgent" placeholder="请选择是否紧急" clearable class="w100" @change="handleQuery">
						<el-option :value="true" label="紧急" />
						<el-option :value="false" label="不紧急" />
					</el-select>
				</el-form-item>
				<el-form-item label="接办部门" prop="ActualHandleOrgName">
					<el-input v-model="state.queryParams.ActualHandleOrgName" placeholder="接办部门名称" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="工单状态" prop="Status">
					<el-select v-model="state.queryParams.Status" placeholder="请选择工单状态" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.orderStatusOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="受理人" prop="AcceptorName">
					<el-input v-model="state.queryParams.AcceptorName" placeholder="受理人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="超期状态" prop="ExpiredStatus">
					<el-select v-model="state.queryParams.ExpiredStatus" placeholder="请选择超期状态" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.expiredStatusOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</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="todoCenter">
import { defineAsyncComponent, onActivated, onBeforeUnmount, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import other from '@/utils/other';
import { useRouter } from 'vue-router';
import { formatDate } from '@/utils/formatTime';
import { centerTodo, centerTodoBase } from '@/api/todo/center';
import { orderAverage, orderSign } from '@/api/todo/order';
import { exportJbOrder } from '@/api/business/order';
import { downloadZip } from '@/utils/tools';
import mittBus from '@/utils/mitt';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const OrderMigration = defineAsyncComponent(() => import('@/views/todo/center/Order-migration.vue')); // 工单平移
const ToEnd = defineAsyncComponent(() => import('@/views/todo/center/components/To-end.vue')); // 批量归档

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		Title: null, // 标题
		No: null, // 工单编号
		scTime: [], // 生成时间
		StCreationTime: null, // 生成开始时间
		EnCreationTime: null, // 生成结束时间
		slTime: [], // 受理时间
		StartTimeSt: null, // 受理开始时间
		StartTimeEnd: null, // 受理结束时间
		AcceptorName: null, // 受理人
		ActualHandleOrgName: null, // 接办部门
		Status: null, // 工单状态
		ExpiredStatus: null, // 超期状态
		StepName: null, // 当前节点名称
		IsUrgent: null,
		CenterToOrgHandlerName: null, // 派单员
		SortField:null,
		SortRule:null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	expiredStatusOptions: [], //超期状态
	orderStatusOptions: [], //工单状态
	stepNamesOptions: [], //步骤名称
});
const router = useRouter(); // 路由
const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', minWidth: 40, align: 'center' },
	{ prop: 'expiredStatusText', label: '超期状态', minWidth: 80, align: 'center' },
	{ prop: 'no', label: '工单编码', minWidth: 140 },
	{ prop: 'isProvinceText', label: '省/市工单', minWidth: 90 },
	{
		prop: 'isUrgentText',
		label: '是否紧急',
		render: (scope:any) => {
			return <span class="color-danger font-bold">{scope.row.isUrgentText}</span>;
		},
	},
	{ prop: 'currentStepName', label: '当前节点', minWidth: 120 },
	{ prop: 'statusText', label: '工单状态', minWidth: 100 },
	{ prop: 'title', label: '工单标题', minWidth: 200 },
	{ prop: 'centerToOrgHandlerName', label: '派单员', minWidth: 120 },
	{
		prop: 'startTime',
		label: '受理时间',
		minWidth: 160,
		render: (scope:any) => {
			return <span>{formatDate(scope.row.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
		sortable: 'custom',
	},
	{
		prop: 'expiredTime',
		label: '工单期满时间',
		minWidth: 160,
		render: (scope:any) => {
			return <span>{formatDate(scope.row.expiredTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
		sortable: 'custom',
	},
	{
		prop: 'filedTime',
		label: '办结时间',
		minWidth: 160,
		render: (scope:any) => {
			return <span>{formatDate(scope.row.filedTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
		sortable: 'custom',
	},
	{ prop: 'orgLevelOneName', label: '一级部门', minWidth: 140 },
	{ prop: 'actualHandleOrgName', label: '接办部门', minWidth: 140 },
	{ prop: 'acceptType', label: '受理类型', minWidth: 100 },
	{ prop: 'counterSignTypeText', label: '是否会签', minWidth: 90 },
	{ prop: 'sourceChannel', label: '来源渠道', minWidth: 100 },
	{ prop: 'hotspotName', label: '热点分类', minWidth: 150 },
	{
		prop: 'sensitive',
		label: '敏感词',
		minWidth: 150,
	},
	{ prop: 'acceptorName', label: '受理人', minWidth: 120 },
	{
		prop: 'reTransactNum',
		label: '重办次数',
	},
	{ prop: 'operation', label: '操作', fixed: 'right', minWidth: 140, align: 'center' },
]);
// 获取查询条件基础信息
const getBaseData = async () => {
	try {
		const res: any = await centerTodoBase();
		const mappings: any = {
			expiredStatusOptions: 'expiredStatus',
			orderStatusOptions: 'orderStatus',
			stepNamesOptions: 'stepNames',
		};
		for (const key in mappings) {
			state[key] = res.result?.[mappings[key]] ?? [];
		}
	} catch (error) {
		console.log(error);
	}
};
// 排序
const sortChange = (val: any) => {
	state.queryParams.SortField = val.order ? val.prop : null;
	// 0 升序 1 降序
	state.queryParams.SortRule = val.order ? (val.order == 'descending' ? 1 : 0) : null;
	handleQuery();
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	requestParams.value = other.deepClone(state.queryParams);
	requestParams.value.StCreationTime = state.queryParams.scTime === null ? null : state.queryParams.scTime[0];
	requestParams.value.EndCreationTime = state.queryParams.scTime === null ? null : state.queryParams.scTime[1];
	Reflect.deleteProperty(requestParams.value, 'scTime');
	requestParams.value.StartTimeSt = state.queryParams.slTime === null ? null : state.queryParams.slTime[0];
	requestParams.value.StartTimeEnd = state.queryParams.slTime === null ? null : state.queryParams.slTime[1];
	Reflect.deleteProperty(requestParams.value, 'slTime');
	state.loading = true;
	centerTodo(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();
	ruleFormRef.value?.resetFields();
	queryList();
};
// 签收工单
const onSign = (row: any) => {
	ElMessageBox.confirm(`您确定要要签收【${row.title}】，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			orderSign(row.id).then(() => {
				ElMessage.success('签收成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 编辑工单
const onOrderEdit = (row: any) => {
	router.push({
		name: 'orderAccept',
		query: {
			id: row.id,
			tagsViewName: `编辑工单-${row.title}`,
		},
	});
};
// 交办单导出
const onJbExport = () => {
	const ids = proTableRef.value.selectedList.map((item: any) => item.id);
	ElMessageBox.confirm(`您确定导出选中的${proTableRef.value.selectedList.length}个工单的交办单，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			state.loading = true;
			exportJbOrder(ids)
				.then((res: any) => {
					downloadZip(res);
					state.loading = false;
					ElMessage.success('导出成功');
				})
				.catch(() => {
					state.loading = false;
				});
		})
		.catch(() => {});
};
// 平移功能
const orderMigrationRef = ref<RefType>();
const onMigration = (row: any) => {
	orderMigrationRef.value.openDialog('centerTodo', row);
};
// 分配工单
const onAssignOrders = () => {
	ElMessageBox.confirm(`您确定分配工单，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			state.loading = true;
			orderAverage()
				.then(() => {
					state.loading = false;
					ElMessage.success('分配成功');
					handleQuery();
				})
				.catch(() => {
					state.loading = false;
				});
		})
		.catch(() => {});
};
// 批量归档
const toEndOrderRef = ref<RefType>();
const onToEnd = ()=>{
	toEndOrderRef.value.openDialog();
}
onMounted(() => {
	getBaseData();
	queryList();
});
onActivated(() => {
	mittBus.on('clearCachePage', () => {
		//清除缓存
		queryList();
	});
});
onBeforeUnmount(() => {
	mittBus.off('clearCachePage');
});
</script>