<template>
	<div class="business-secondVisit-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>
					<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 #title="{ row }">
					<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</template>
				<template #employeeName="{ row }">
					<span>{{ row.orderVisit?.order?.acceptorName }}</span>
				</template>
				<template #orgProcessingResults="{ row }">
					<span>{{ row.orgProcessingResults?.value }}</span>
				</template>
				<template #orgHandledAttitude="{ row }">
					<span>{{ row.orgHandledAttitude?.value }}</span>
				</template>
				<template #orgNoSatisfiedReason="{ row }">
					<span>
						{{ row.orgNoSatisfiedReason?.map((item) => item.value).join(',') }}
					</span>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onSecondVisit(row)" title="二次回访" v-auth="'business:secondVisit:apply'"> 二次回访 </el-button>
					<order-detail :order="row.order" @updateList="handleQuery" />
				</template>
			</ProTable>
		</el-card>
		<!-- 二次回访 -->
		<second-visit ref="secondVisitRef" @updateList="handleQuery" />
	</div>
</template>
<script setup lang="tsx" name="secondVisit">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import { secondVisitagainlist } from '@/api/business/secondVisit';
// 引入组件
const SecondVisit = defineAsyncComponent(() => import('@/views/business/secondVisit/components/Visit.vue'));
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 40 },
	{ prop: 'order.no', label: '工单编码', minWidth: 140 },
	{ prop: 'order.statusText', label: '回访状态', minWidth: 100 },
	{ prop: 'order.isProvinceText', label: '省/市工单', minWidth: 90 },
	{ prop: 'order.title', label: '工单标题', minWidth: 200 },
	{ prop: 'order.sourceChannel', label: '来源渠道', minWidth: 100 },
	{ prop: 'order.acceptType', label: '受理类型', minWidth: 100 },
	{ prop: 'order.hotspotName', label: '热点分类', minWidth: 150 },
	{ prop: 'acceptorName', label: '受理人', minWidth: 120 },
	{ prop: 'order.orgLevelOneName', label: '一级部门', minWidth: 140 },
	{ prop: 'order.actualHandleOrgName', label: '接办部门', minWidth: 140 },
	{
		prop: 'order.startTime',
		label: '受理时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'order.filedTime',
		label: '办结时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.filedTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'orderVisit.publishTime',
		label: '发布时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.orderVisit?.publishTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'orderVisit.visitTime',
		label: '回访时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.orderVisit?.visitTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'visitOrgName', label: '被回访部门', minWidth: 140 },
	{ prop: 'orgProcessingResults', label: '部门办件结果', minWidth: 120 },
	{ prop: 'orgHandledAttitude', label: '部门办件态度', minWidth: 120 },
	{ prop: 'orgNoSatisfiedReason', label: '不满意原因', minWidth: 150 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 160, align: 'center' },
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const router = useRouter(); // 路由
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键字
		VisitApplyState: null, // 回访状态
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	secondVisitagainlist(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 secondVisitRef = ref<RefType>();
const onSecondVisit = (row: any) => {
	secondVisitRef.value.openDialog(row);
};
onMounted(() => {
	queryList();
});
</script>
