<template>
	<el-card shadow="never" class="mb20" style="position: relative" v-loading="state.tableLoading">
		<el-button
			link
			type="primary"
			style="position: absolute; right: 20px; top: 32px; z-index: 2"
			@click="linkList"
			v-auths="['home:orderTodo', 'home:orderSeatTodo']"
		>
			更多<SvgIcon name="ele-ArrowRight" />
		</el-button>
		<el-tabs v-model="activeName" @tab-change="handleChange" v-if="auths(['home:orderTodo', 'home:orderSeatTodo'])">
			<el-tab-pane name="0" label="工单待办" v-if="auth('home:orderTodo')"></el-tab-pane>
			<el-tab-pane name="1" label="工单坐席待办" v-if="auth('home:orderSeatTodo')"></el-tab-pane>
		</el-tabs>
		<template v-if="auths(['home:orderTodo', 'home:orderSeatTodo'])">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="handleChange(activeName)"
				:loading="state.loading"
				:pagination="false"
				: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="handleChange(activeName)">{{ 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="handleChange(activeName)" />
				</template>
			</ProTable>
		</template>
		<Empty v-else />
	</el-card>
</template>
<script setup lang="tsx">
import { computed, defineAsyncComponent, onMounted, reactive, ref, watch, watchEffect } from 'vue';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import { orderListTodo, orderSign, seatsListTodo } from '@/api/todo/order';
import { ElButton, ElMessage, ElMessageBox } from 'element-plus';
import { auth, auths } from '@/utils/authFunction';

const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const state = reactive({
	tableLoading: false, // 表格loading
	tableData: [], //表格数据
	queryParams: {
		PageIndex: 1,
		PageSize: 10,
	},
});
const activeName = ref('0');
watchEffect(() => {
	if (auth('home:orderTodo')) {
		return (activeName.value = '0');
	} else if (auth('home:orderSeatTodo')) {
		return (activeName.value = '1');
	}
});

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([]);

// 工单待办表头
const todoColumns = [
	{ prop: 'expiredStatus', label: '超期状态', align: 'center' },
	{ 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: '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: 100 },
	{ prop: 'sourceChannel', label: '来源方式', width: 100 },
	{ prop: 'hotspotName', label: '热点分类', width: 100 },
	{ prop: 'employeeName', label: '受理人', width: 170 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 170, align: 'center' },
];
// 工单坐席待办表头
const seatsColumns = [
	{ 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: '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: 120 },
	{ prop: 'sourceChannel', label: '来源方式', width: 120 },
	{ prop: 'emergencyLevelText', label: '紧急程度', width: 100 },
	{ prop: 'hotspotName', label: '热点分类', width: 100 },
	{ prop: 'employeeName', label: '受理人', width: 150 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 190, align: 'center' },
];
// tab切换
const handleChange = async (tab: any) => {
	state.tableLoading = true;
	switch (tab) {
		case '0': // 工单待办
			try {
				const res: any = await orderListTodo({ ...state.queryParams, IsHandled: 'false' });
				state.tableData = res.result?.items ?? [];
				columns.value = todoColumns;
				state.tableLoading = false;
			} catch (error) {
				state.tableLoading = false;
			}
			break;
		case '1': // 工单待办
			try {
				const res: any = await seatsListTodo({ ...state.queryParams, IsHandled: 'false' });
				state.tableData = res.result?.items ?? [];
				columns.value = seatsColumns;
				state.tableLoading = false;
			} catch (error) {
				state.tableLoading = false;
			}
			break;
		default:
			state.tableLoading = false;
			break;
	}
};
// 跳转
const router = useRouter();
// 列表入口
const linkList = () => {
	switch (activeName.value) {
		case '0': // 工单待办
			router.push('/todo/order');
			break;
		case '1': // 工单坐席待办
			router.push('/todo/seats');
			break;
	}
};
// 编辑工单
const onOrderEdit = (row: any) => {
	if (!router.hasRoute('orderAccept')) {
		ElMessage.warning('当前用户未配置工单受理页面');
		return;
	}
	router.push({
		name: 'orderAccept',
		params: {
			tagsViewName: '工单受理-' + row.no,
			id: row.id,
			callId: '0',
		},
	});
};
// 签收工单
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('签收成功');
				handleChange(activeName.value);
			});
		})
		.catch(() => {});
};
onMounted(() => {
	handleChange(activeName.value);
});
</script>
