<template>
	<el-dialog v-model="state.dialogVisible" width="60%" draggable title="回访明细" append-to-body destroy-on-close>
		<el-form :model="state.queryParams" ref="queryParamsRef" inline @submit.native.prevent>
			<el-form-item label="关键词" prop="Keyword">
				<el-input v-model="state.queryParams.Keyword" placeholder="工单标题/工单编码" clearable @keyup.enter="queryList" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="queryList" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				<el-button @click="resetQuery(queryParamsRef)" :loading="state.loading" class="default-button">
					<SvgIcon name="ele-Refresh" class="mr5" />重置
				</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="state.tableData" v-loading="state.loading">
			<el-table-column prop="order.no" label="工单编码" show-overflow-tooltip width="150"></el-table-column>
			<el-table-column label="标题" show-overflow-tooltip width="300">
				<template #default="{ row }">
					<order-detail :order="row.order">{{ row.order?.title }}</order-detail>
				</template>
			</el-table-column>
			<el-table-column prop="aiOrderVisitStateText" label="外呼状态" show-overflow-tooltip width="150"></el-table-column>
			<el-table-column prop="outerNo" label="外呼电话" show-overflow-tooltip width="150"></el-table-column>
			<el-table-column prop="order.fromName" label="姓名" show-overflow-tooltip width="150"></el-table-column>
			<el-table-column prop="order.fromGenderText" label="性别" show-overflow-tooltip width="150"></el-table-column>
			<!--      <el-table-column label="短信发送时间" show-overflow-tooltip width="170">
        <template #default="{ row }">
          <span>{{ formatDate(row.order?.startTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
        </template>
      </el-table-column>-->
			<el-table-column label="受理时间" show-overflow-tooltip width="170">
				<template #default="{ row }">
					<span>{{ formatDate(row.order?.startTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="办结时间" show-overflow-tooltip width="170">
				<template #default="{ row }">
					<span>{{ formatDate(row.order?.filedTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="order.actualOpinion" label="办件结果" show-overflow-tooltip width="150"></el-table-column>
			<el-table-column label="话务员评价" show-overflow-tooltip width="120">
				<template #default="{ row }">
					<span v-for="item in row.orderVisit?.orderVisitDetails">
						<span v-if="item.visitTarget === 10">{{ item.seatEvaluateText }}</span>
					</span>
				</template>
			</el-table-column>
			<el-table-column label="部门办件结果" show-overflow-tooltip width="170">
				<template #default="{ row }">
					<span v-for="item in row.orderVisit?.orderVisitDetails">
						<span v-if="item.visitTarget === 20">{{ item.orgProcessingResults.value }}</span>
					</span>
				</template>
			</el-table-column>
			<el-table-column label="部门办件态度" show-overflow-tooltip width="170">
				<template #default="{ row }">
					<span v-for="item in row.orderVisit?.orderVisitDetails">
						<span v-if="item.visitTarget === 20">{{ item.orgHandledAttitude.value }}</span>
					</span>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<pagination
			@pagination="queryList"
			:total="state.total"
			v-model:current-page="state.queryParams.PageIndex"
			v-model:page-size="state.queryParams.PageSize"
		/>
	</el-dialog>
</template>
<script setup lang="ts">
import { formatDate } from '@/utils/formatTime';
import { ElInput, FormInstance } from 'element-plus';
import { defineAsyncComponent, reactive, ref } from 'vue';
import { getSmartVisitDetail } from '@/api/smartVisit';

const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键字
		id: '',
	},
	tableData: [], //表格
	loading: false, // 加载
	total: 0, // 总数
	dialogVisible: false, // 弹窗
});
// 打开弹窗
const openDialog = (row?: any) => {
	if (row) {
		state.queryParams.id = row.id;
		queryList();
	}
};
// 查询智能回访明细表
const queryList = async () => {
	state.dialogVisible = true;
	try {
		state.loading = true;
		const res = await getSmartVisitDetail(state.queryParams);
		state.tableData = res.result.items ?? [];
		state.total = res.result.total ?? 0;
		state.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
// 重置所有工单列表
const queryParamsRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};

defineExpose({
	openDialog,
});
</script>
