<template>
	<div class="business-return-province-container layout-pd">
		<el-card shadow="never">
			<el-tabs v-model="state.queryParams.State" @tab-change="handleQuery">
				<el-tab-pane name="0" label="退回待办"></el-tab-pane>
				<el-tab-pane name="1" 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>
					<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"
			>
				<template #title="{ row }">
					<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</template>
				<template #visitTarget="{ row }">
					<span v-if="row.visitTarget === 20">{{ row.visitOrgName }}</span>
				</template>
				<template #orgProcessingResults="{ row }">
					<span v-if="row.visitTarget === 20">{{ row.orgProcessingResults?.value }}</span>
				</template>
				<template #orgHandledAttitude="{ row }">
					<span v-if="row.visitTarget === 20">{{ row.orgHandledAttitude?.value }}</span>
				</template>
				<template #orgNoSatisfiedReason="{ row }">
					<span v-if="row.visitTarget === 20">
						{{ row.orgNoSatisfiedReason?.map((item) => item.value).join(',') }}
					</span>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button
						link
						type="primary"
						@click="onAudit(row)"
						title="退回审批"
						v-auth="'business:return:audit:province'"
						v-if="[0].includes(row.state)"
					>
						退回审批
					</el-button>
					<el-button link type="primary" @click="onReturnDetail(row)" title="查看退回审核详情" v-if="[1, 2].includes(row.state)">
						退回详情
					</el-button>
					<order-detail :order="row.order" @updateList="queryList" />
				</template>
			</ProTable>
		</el-card>
		<!-- 工单省退回 -->
		<order-return ref="orderReturnRef" @updateList="queryList" />
		<!-- 省退回详情 -->
		<return-detail ref="returnDetailRef" />
	</div>
</template>
<script setup lang="tsx" name="orderReturnProvince">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import { provinceReturnList } from '@/api/business/return';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const OrderReturn = defineAsyncComponent(() => import('@/views/business/return/components/Apply.vue')); // 工单省退回
const ReturnDetail = defineAsyncComponent(() => import('@/views/business/return/components/Return-detail.vue')); // 省退回详情
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 55 },
	{ prop: 'order.no', label: '工单编码', width: 150 },
	{ prop: 'order.isProvinceText', label: '省/市工单', width: 100 },
	{ prop: 'stateText', label: '退回状态', width: 100 },
	{ prop: 'order.title', label: '工单标题', width: 300 },
	{
		prop: 'order.startTime',
		label: '受理时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'order.acceptorName', label: '受理人', width: 170 },
	{ prop: 'order.acceptType', label: '受理类型', width: 150 },
	{ prop: 'order.sourceChannel', label: '热点分类', width: 200 },
	{ prop: 'order.orgLevelOneName', label: '一级部门', width: 170 },
	{
		prop: 'order.centerToOrgTime',
		label: '交办时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.centerToOrgTime, '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: 'creatorOrgName', label: '退回申请部门', width: 120 },
	{ prop: 'creatorName', label: '退回申请人', width: 120 },
	{ prop: 'content', label: '退回申请理由', minWidth: 200 },
	{
		prop: 'auditTime',
		label: '审批时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.auditTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'auditName', label: '审批人' },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 170, align: 'center' },
]);
const router = useRouter(); // 路由
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键字
		State: '0',
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	provinceReturnList(state.queryParams)
		.then((res) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch((err) => {
			console.log(err);
			state.loading = false;
		});
};

/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 导出
const onExport = () => {
	console.log('导出');
};
// 退回审批
const orderReturnRef = ref<RefType>();
const onAudit = (row: any) => {
	if (row.status == 200) {
		// 会签工单无法进行特提
		ElMessage.warning('工单会签中，请先结束会签！');
		return;
	}
	orderReturnRef.value.openDialog(row, true);
};
// 查看退回详情
const returnDetailRef = ref<RefType>();
const onReturnDetail = (row: any) => {
	returnDetailRef.value.openDialog(row);
};
onMounted(async () => {
	queryList();
});
</script>
