<template>
	<div class="tels-callLog-connect-container">
		<el-dialog v-model="state.dialogVisible" draggable title="关联业务" @close="close">
			<el-form :model="state.ruleForm" label-width="80px" label-position="left">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="关联类型">
							<el-radio-group v-model="state.ruleForm.isOrder" @change="queryList">
								<el-radio :label="true">工单</el-radio>
								<el-radio :label="false">回访</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent>
				<el-row :gutter="10">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="" prop="Keyword">
							<el-input v-model="state.queryParams.Keyword" placeholder="工单编码/标题" clearable @keyup.enter="queryList" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label=" ">
							<el-button type="primary" @click="queryList" :loading="loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
							<el-button
								@click="
									state.queryParams.Keyword = null;
									queryList();
								"
								v-waves
								class="default-button"
								:loading="loading"
							>
								<SvgIcon name="ele-Refresh" class="mr5" />重置
							</el-button>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="loading" @current-change="handleSelectionChange">
				<el-table-column label="选择" width="70" fixed="left" align="center">
					<template #default="{ row }">
						<el-radio v-model="tableRadio" :label="row.id" @change="handleRowChange(row)">&nbsp;</el-radio>
					</template>
				</el-table-column>
				<!-- 工单 -->
				<template v-if="state.ruleForm.isOrder">
					<el-table-column prop="no" label="工单编码" show-overflow-tooltip width="150"></el-table-column>
					<el-table-column width="100" label="省/市工单" prop="isProvince">
						<template #default="{ row }">
							<span>{{ row.isProvince ? '省工单' : '市工单' }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="actualHandleStepName" label="办理节点" show-overflow-tooltip width="150"></el-table-column>
					<el-table-column label="工单状态" show-overflow-tooltip width="100" prop="statusText"></el-table-column>
					<el-table-column label="标题" show-overflow-tooltip width="300">
						<template #default="{ row }">
							<span class="color-primary">{{ row.title }}</span>
						</template>
					</el-table-column>
					<el-table-column label="受理时间" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span>{{ formatDate(row.startTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="expiredTime" label="工单期满时间" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span>{{ formatDate(row.expiredTime, '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.filedTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="orgLevelOneName" label="一级部门" show-overflow-tooltip width="170"></el-table-column>
					<el-table-column prop="actualHandleOrgName" label="接办部门" show-overflow-tooltip width="170"></el-table-column>
					<el-table-column prop="acceptType" label="受理类型" show-overflow-tooltip width="150"></el-table-column>
					<el-table-column prop="counterSignTypeText" label="是否会签" show-overflow-tooltip width="100"></el-table-column>
					<el-table-column prop="sourceChannel" label="来源方式" show-overflow-tooltip width="100"></el-table-column>
					<el-table-column prop="hotspotName" label="热点分类" show-overflow-tooltip width="200"></el-table-column>
					<el-table-column prop="tagNames" label="工单标签" show-overflow-tooltip width="200"></el-table-column>
					<el-table-column prop="employeeName" label="受理人" show-overflow-tooltip width="120">
						<template #default="{ row }">
							<span
								>{{ row.acceptorName }} <span v-if="row.acceptorStaffNo">[{{ row.acceptorStaffNo }}]</span>
							</span>
						</template>
					</el-table-column>
				</template>
				<!-- 回访 -->
				<template v-else>
					<el-table-column prop="order.no" label="工单编码" show-overflow-tooltip width="150"></el-table-column>
					<el-table-column width="100" label="省/市工单" prop="isProvince">
						<template #default="{ row }">
							<span>{{ row.order?.isProvince ? '省工单' : '市工单' }}</span>
						</template>
					</el-table-column>
					<el-table-column label="工单标题" show-overflow-tooltip width="300">
						<template #default="{ row }">
							<span class="color-primary">{{ row.order?.title }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="order.sourceChannel" label="来源方式" show-overflow-tooltip></el-table-column>
					<el-table-column prop="visitStateText" label="回访状态" show-overflow-tooltip width="100"></el-table-column>
					<el-table-column prop="visitTypeText" label="回访方式" show-overflow-tooltip width="100"></el-table-column>
					<el-table-column prop="order.acceptType" label="受理类型" show-overflow-tooltip width="120"></el-table-column>
					<el-table-column prop="order.hotspotName" label="热点分类" show-overflow-tooltip width="200"></el-table-column>
					<el-table-column prop="expiredTime" label="受理人" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span
								>{{ row.order?.acceptorName }} <span v-if="row.order?.acceptorStaffNo">[{{ row.order?.acceptorStaffNo }}]</span>
							</span>
						</template>
					</el-table-column>
					<el-table-column prop="order.orgLevelOneName" label="一级部门" show-overflow-tooltip width="150"></el-table-column>
					<el-table-column prop="order.actualHandleOrgName" 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?.filedTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="publishTime" label="发布时间" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span>{{ formatDate(row.publishTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="creationTime" label="回访任务创建时间" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="employeeName" label="回访人" show-overflow-tooltip></el-table-column>
					<el-table-column prop="visitTime" label="回访时间" show-overflow-tooltip width="170">
						<template #default="{ row }">
							<span>{{ formatDate(row.visitTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
						</template>
					</el-table-column>
					<el-table-column prop="order.counterSignTypeText" label="是否会签" show-overflow-tooltip></el-table-column>
					<el-table-column label="语音评价" show-overflow-tooltip width="150">
						<template #default="{ row }">
							<span v-for="item in row.orderVisitDetails">
								<span v-if="item.visitTarget === 10">{{ item.voiceEvaluateText }}</span>
							</span>
						</template>
					</el-table-column>
					<el-table-column label="话务员满意度" show-overflow-tooltip width="150">
						<template #default="{ row }">
							<span v-for="item in row.orderVisitDetails">
								<span v-if="item.visitTarget === 10">{{ item.seatEvaluateText }}</span>
							</span>
						</template>
					</el-table-column>
				</template>
				<template #empty>
					<Empty />
				</template>
			</el-table>
			<!-- 分页 -->
			<pagination
				:total="state.total"
				v-model:page="state.queryParams.PageIndex"
				v-model:limit="state.queryParams.PageSize"
				@pagination="queryList"
			/>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="closeDialog" class="default-button">取 消</el-button>
					<el-button type="primary" @click="onSubmit" :disabled="!tableRadio" :loading="loading">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="blackAdd">
import { ref, reactive } from 'vue';
import { ElButton, ElMessage } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { callLogLink, callLogOrder, callLogVisit } from '@/api/tels/callLog';

const props = defineProps({
	specialFlagList: {
		type: Array,
		default: () => [],
	},
});
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容

const state = reactive<any>({
	ruleForm: {
		isOrder: true, // 是否工单
		specialFlag: null,
	},
	queryParams: {
		// 查询条件
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		Keyword: null, // 关键字
	},
	tableData: [],
	total: 0,
	dialogVisible: false, // 弹窗显示隐藏
	currentCall: {}, // 当前通话记录
});
const loading = ref<Boolean>(false);
// 打开弹窗
const openDialog = (val: any) => {
	state.currentCall = val;
	queryList();
	state.dialogVisible = true;
};
const queryList = () => {
	loading.value = true;
	if (state.ruleForm.isOrder) {
		callLogOrder(state.queryParams)
			.then((res: any) => {
				state.tableData = res.result?.items ?? [];
				state.total = res.result?.total ?? 0;
				loading.value = false;
			})
			.catch(() => {
				state.tableData = [];
				state.total = 0;
				loading.value = false;
			});
	} else {
		callLogVisit(state.queryParams)
			.then((res: any) => {
				state.tableData = res.result?.items ?? [];
				state.total = res.result?.total ?? 0;
				loading.value = false;
			})
			.catch(() => {
				state.tableData = [];
				state.total = 0;
				loading.value = false;
			});
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	state.ruleForm.isOrder = true;
	state.queryParams.PageIndex = 1;
	state.queryParams.PageSize = 10;
	state.queryParams.Keyword = null;
	state.tableData = [];
	state.total = 0;
	tableRadio.value = '';
	selectRow.value = {};
};
const tableRadio = ref<String>(''); // 选择的单选框
const selectRow = ref<EmptyObjectType>({}); // 选择的行
// 选择重复件
const handleSelectionChange = (row: any) => {
	if (row) {
		tableRadio.value = row.id;
		selectRow.value = row;
	}
};
const handleRowChange = (row: any) => {
	tableRadio.value = row.id;
	selectRow.value = row;
};
// 保存
const onSubmit = () => {
	loading.value = true;
	const request = {
		callId: state.currentCall.callAccept,
		id: tableRadio.value,
		isOrder: state.ruleForm.isOrder,
	};
	callLogLink(request)
		.then(() => {
			loading.value = false;
			ElMessage.success('关联成功');
			closeDialog();
			emit('updateList');
		})
		.catch(() => {
			loading.value = false;
		});
};
//暴漏变量和方法
defineExpose({ closeDialog, openDialog });
</script>
