<template>
	<div class="business-visit-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<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 #table-search>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
						<el-form-item label="数据范围" prop="IsProvince">
							<el-segmented
								:options="[
									{
										value: 'all',
										label: '全部',
									},
									{
										value: 'city',
										label: '市工单',
									},
									{
										value: 'province',
										label: '省工单',
									},
								]"
								v-model="fastSearch"
								@change="fastSearchChange"
							/>
						</el-form-item>
						<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="No">
							<el-input v-model="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
						</el-form-item>
						<el-form-item label="" label-width="0">
							<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
							<el-button @click="drawer = true" class="default-button"> <SvgIcon name="ele-Search" class="mr5" />更多查询</el-button>
						</el-form-item>
					</el-form>
				</template>
				<template #title="{ row }">
					<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</template>
				<template #voicePj="{ row }">
					<span v-for="item in row.orderVisitDetails">
						<span v-if="item.visitTarget === 10">{{ item.voiceEvaluateText }}</span>
					</span>
				</template>
				<template #hwyPj="{ row }">
					<span v-for="item in row.orderVisitDetails">
						<span v-if="item.visitTarget === 10">{{ item.seatEvaluateText }}</span>
					</span>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="visitDetail(row)" title="查看回访详情"> 回访详情 </el-button>
					<el-button
						link
						type="primary"
						@click="updateVisitResult(row)"
						title="修改回访结果"
						v-auth="'business:visit:visitEdit'"
						v-if="['ZiGong'].includes(themeConfig.appScope) && row.visitState === 30"
					>
						修改回访结果
					</el-button>
				</template>
			</ProTable>
		</div>
		<!--	更多查询	-->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px">
				<el-form-item label="归档类型" prop="FiledType">
					<el-select v-model="state.queryParams.FiledType" placeholder="请选择归档类型" @change="handleQuery" clearable>
						<el-option label="中心归档" value="10" />
						<el-option label="部门归档" value="20" />
					</el-select>
				</el-form-item>
				<el-form-item label="是否会签" prop="IsCountersign">
					<el-select v-model="state.queryParams.IsCountersign" placeholder="请选择是否会签" @change="handleQuery" clearable>
						<el-option label="是" value="true" />
						<el-option label="否" value="false" />
					</el-select>
				</el-form-item>
				<el-form-item label="回访状态" prop="VisitState" v-if="['YiBin','LuZhou'].includes(themeConfig.appScope)">
					<el-select v-model="state.queryParams.VisitState" placeholder="请选择回访状态" @change="handleQuery">
						<el-option v-for="item in visitStateQuery" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="回访方式" prop="VisitType">
					<el-select v-model="state.queryParams.VisitType" placeholder="请选择回访方式" @change="handleQuery" clearable>
						<el-option v-for="item in state.visitTypeOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="回访人" prop="EmployeeName">
					<el-input v-model="state.queryParams.EmployeeName" placeholder="回访人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="回访时间" prop="crTime">
					<el-date-picker
						v-model="state.queryParams.crTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
					/>
				</el-form-item>
				<el-form-item label="语音评价" prop="VoiceEvaluate">
					<el-select v-model="state.queryParams.VoiceEvaluate" placeholder="请选择语音评价" clearable multiple>
						<el-option v-for="items in voiceEvaluate" :key="items.key" :label="items.value" :value="items.key" />
					</el-select>
				</el-form-item>
				<el-form-item label="话务员评价" prop="SeatEvaluate">
					<el-select v-model="state.queryParams.SeatEvaluate" placeholder="请选择话务员评价" clearable multiple>
						<el-option v-for="items in seatEvaluate" :key="items.key" :label="items.value" :value="items.key" />
					</el-select>
				</el-form-item>
				<el-form-item label="部门办件结果" prop="OrgProcessingResults">
					<el-select v-model="state.queryParams.OrgProcessingResults" placeholder="请选择部门办件结果" clearable multiple>
						<el-option v-for="items in visitSatisfaction" :key="items.dicDataValue" :label="items.dicDataName" :value="items.dicDataValue" />
					</el-select>
				</el-form-item>
				<el-form-item label="部门办件态度" prop="OrgHandledAttitude" v-if="!['YiBin'].includes(themeConfig.appScope)">
					<el-select v-model="state.queryParams.OrgHandledAttitude" placeholder="请选择部门办件态度" clearable multiple>
						<el-option v-for="items in visitManner" :key="items.dicDataValue" :label="items.dicDataName" :value="items.dicDataValue" />
					</el-select>
				</el-form-item>
				<el-form-item label="有效智能回访" prop="IsEffectiveAiVisit" v-if="['YiBin'].includes(themeConfig.appScope)">
					<el-select v-model="state.queryParams.IsEffectiveAiVisit" placeholder="请选择有效智能回访" @change="handleQuery" clearable>
						<el-option label="是" value="true" />
						<el-option label="否" value="false" />
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				<el-button @click="resetQuery(drawerRuleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
			</template>
		</el-drawer>
		<!-- 回访详情 -->
		<visit-detail-com ref="visitDetailRef" @updateList="queryList" />
	</div>
</template>

<script setup lang="tsx" name="businessVisit">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { visitList, visitSearchBaseData } from '@/api/business/visit';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';

// 引入组件
const VisitDetailCom = defineAsyncComponent(() => import('@/views/business/visit/components/Visit-detail.vue')); // 回访
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情

// 定义变量内容
const proTableRef = ref<RefType>(); // 表格ref
const fastSearch = ref('all'); // tab位置
const fastSearchChange = (val: string) => {
	fastSearch.value = val;
	switch (val) {
		case 'all':
			state.queryParams.IsProvince = null;
			break;
		case 'city':
			state.queryParams.IsProvince = false;
			break;
		case 'province':
			state.queryParams.IsProvince = true;
			break;
	}
	handleQuery();
};
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const operationWidth = computed(()=>{
	return ['ZiGong'].includes(themeConfig.value.appScope) ? 190 : 90;
})
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'order.no', label: '工单编码', minWidth: 140 },
	{ prop: 'order.isProvinceText', label: '省/市工单', minWidth: 90 },
	{ prop: 'order.title', label: '工单标题', minWidth: 200 },
	{ prop: 'order.sourceChannel', label: '来源渠道', minWidth: 100 },
	{ prop: 'visitStateText', label: '回访状态', minWidth: 100 },
	{ prop: 'visitTypeText', label: '回访方式', minWidth: 100 },
	{ prop: 'isEffectiveAiVisitText', label: '有效智能回访', minWidth: 110, isShow: ['YiBin'].includes(themeConfig.value.appScope) },
	{ prop: 'order.acceptType', label: '受理类型', minWidth: 100 },
	{ prop: 'order.hotspotName', label: '热点分类', minWidth: 150 },
	{ prop: 'order.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: 'publishTime',
		label: '发布时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.publishTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'creationTime',
		label: '回访任务创建时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'employeeName', label: '回访人', minWidth: 120 },
	{
		prop: 'visitTime',
		label: '回访时间',
		minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.visitTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'order.counterSignTypeText', label: '是否会签', minWidth: 90 },
	// { prop: 'voicePj', label: '语音评价', width: 150 },
	{ prop: 'hwyPj', label: '话务员满意度', minWidth: 120 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: operationWidth, align: 'center' },
]);
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键字
		VisitState: 0, // 回访状态
		VisitType: null, // 回访方式
		No: null,
		IsCountersign: null, // 是否会签
		FiledType: null, // 归档类型
		IsProvince: null,
		EmployeeName:null,
		crTime: [],
		StartTime: null,
		EndTime: null,
		IsEffectiveAiVisit: null,
		VoiceEvaluate: [],
		SeatEvaluate: [],
		OrgProcessingResults: [],
		OrgHandledAttitude: [],
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	visitTypeOptions: [], // 回访方式
});
// 获取基础数据
const seatEvaluate = ref<EmptyArrayType>([]); // 话务员评价
const voiceEvaluate = ref<EmptyArrayType>([]); // 语音评价
const visitManner = ref<EmptyArrayType>([]); // 部门办件态度
const visitSatisfaction = ref<EmptyArrayType>([]); // 部门办件结果
const visitStateQuery = ref<EmptyArrayType>([]);
const getBaseData = () => {
	visitSearchBaseData().then(({ result }) => {
		state.visitTypeOptions = result?.visitType ?? [];
		seatEvaluate.value = result?.seatEvaluate ?? [];
		voiceEvaluate.value = result?.voiceEvaluate ?? [];
		visitManner.value = result?.visitManner ?? [];
		visitSatisfaction.value = result?.visitSatisfaction ?? [];
		visitStateQuery.value = result?.visitStateQuery ?? [];
	});
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime');
	visitList(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};

/** 重置按钮操作 */
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.IsProvince = null;
	fastSearch.value = 'all';
	ruleFormRef.value?.resetFields();
	queryList();
};
// 回访详情
const visitDetailRef = ref<RefType>();
const visitDetail = (row: any) => {
	visitDetailRef.value.openDialog(row, '回访详情');
};
// 修改回访结果
const updateVisitResult = (row: any) => {
	visitDetailRef.value.openDialog(row,'修改回访结果');
};
onMounted(() => {
	getBaseData();
	queryList();
});
</script>
