<template>
	<div class="business-secondVisitAudit-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="VisitApplyState">
					<el-select v-model="state.queryParams.VisitApplyState" placeholder="请选择审批状态" clearable class="w100">
						<el-option v-for="item in visitApplyState" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</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 #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="onAudit(row)"
						title="审批"
						v-if="[0].includes(row.visitApplyState)"
						v-auth="'business:secondVisitAudit:audit'"
					>
						审批
					</el-button>
					<el-button link type="primary" @click="onDetail(row)" title="查看审批详情" v-if="[1, 2].includes(row.visitApplyState)">
						二次回访详情
					</el-button>
					<order-detail :order="row.order" @updateList="queryList" />
				</template>
			</ProTable>
		</el-card>
		<!-- 二次回访 -->
		<second-visit ref="secondVisitRef" @updateList="queryList" />
		<!-- 回访详情 -->
		<second-visit-detail ref="secondVisitDetailRef" />
	</div>
</template>
<script setup lang="tsx" name="secondVisitAudit">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import { secondVisitBaseData, secondVisitList } from '@/api/business/secondVisit';
// 引入组件
const SecondVisit = defineAsyncComponent(() => import('@/views/business/secondVisit/components/Visit.vue'));
const SecondVisitDetail = defineAsyncComponent(() => import('@/views/business/secondVisit/components/Visit-detail.vue'));
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 55 },
	{ prop: 'orderVisit.no', label: '工单编码', width: 150 },
	{ prop: 'isProvince', label: '省/市工单', width: 100 },
	{ prop: 'title', label: '工单标题', width: 300 },
	{ prop: 'orderVisit.sourceChannel', label: '来源方式', width: 100 },
	{ prop: 'orderVisit.visitStateText', label: '回访方式', width: 100 },
	{ prop: 'orderVisit.acceptType', label: '受理类型', width: 150 },
	{ prop: 'orderVisit.hotspotName', label: '热点分类', width: 100 },
	{ prop: 'acceptorName', label: '受理人', width: 170 },
	{ prop: 'orderVisit.orgLevelOneName', label: '一级部门', width: 170 },
	{ prop: 'orderVisit.actualHandleOrgName', label: '接办部门', width: 170 },
	{
		prop: 'orderVisit.startTime',
		label: '受理时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'orderVisit.filedTime',
		label: '办结时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.filedTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'orderVisit.publishTime',
		label: '发布时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.orderVisit?.publishTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'orderVisit.visitTime',
		label: '回访时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.orderVisit?.visitTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'orderVisit.visitOrgName', label: '被回访部门', width: 150 },
	{ prop: 'orgProcessingResults', label: '部门办件结果', width: 150 },
	{ prop: 'orgHandledAttitude', label: '部门办件态度', width: 150 },
	{ prop: 'orgNoSatisfiedReason', label: '不满意原因', width: 150 },
	{ prop: 'creatorName', label: '申请人', width: 120 },
	{ prop: 'creatorOrgName', label: '申请部门', width: 150 },
	{ prop: 'visitReason', label: '申请理由', width: 200 },
	{ prop: 'examinOpinion', label: '审批意见', width: 200 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 190, 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, // 总数
});
// 基础信息
const visitApplyState = ref<EmptyArrayType>([]); // 回访状态
const getBaseData = async () => {
	try {
		const res = await secondVisitBaseData();
		visitApplyState.value = res.result?.visitApplyState ?? [];
	} catch (error) {
		console.log(error);
	}
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	secondVisitList(state.queryParams)
		.then((res) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
		})
		.catch((err) => {
			console.log(err);
		});
};

/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 导出
const onExport = () => {
	console.log('导出');
};
// 审批
const secondVisitRef = ref<RefType>();
const onAudit = (row: any) => {
	secondVisitRef.value.openDialog(row, true);
};
// 查看审批详情
const secondVisitDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	secondVisitDetailRef.value.openDialog(row);
};
onMounted(() => {
	getBaseData();
	queryList();
});
</script>
