<template>
	<div class="business-visit-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions"  ref="gridRef">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="gridOptions.loading">
						<el-form-item label="数据范围" prop="IsProvince">
							<el-segmented
								:options="[
							{
								value: 'all',
								label: '全部',
							},
							{
								value: 'city',
								label: '市工单',
							},
							{
								value: 'province',
								label: '省工单',
							},
						]"
								v-model="fastSearch"
								@change="fastSearchChange"
								:disabled="state.loading"
							/>
						</el-form-item>
						<el-form-item label="工单标题" prop="Keyword">
							<el-input v-model.trim="state.queryParams.Keyword" placeholder="工单标题" clearable @keyup.enter="handleQuery" class="keyword-input" />
						</el-form-item>
						<el-form-item label="工单编码" prop="No">
							<el-input v-model.trim="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 #order_detail="{ row }">
					<order-detail :order="row.order" @updateList="handleQuery">{{ row.order?.title }}</order-detail>
				</template>
				<template #actionYB="{row}">
					<el-button link type="primary" @click="visitDetail(row)" title="查看回访详情"> 回访详情 </el-button>
				</template>
				<template #action="{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="row.visitState === 30"
					>
						修改回访结果
					</el-button>
				</template>
				<template #pager>
					<pagination
						@pagination="queryList"
						:total="state.total"
						v-model:current-page="state.queryParams.PageIndex"
						v-model:page-size="state.queryParams.PageSize"
						:disabled="state.loading"
					/>
				</template>
			</vxe-grid>
		</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="IdentityType">
					<el-select v-model="state.queryParams.IdentityType" placeholder="请选择来电主体" clearable @change="handleQuery">
						<el-option :value="1" label="市民" />
						<el-option :value="2" label="企业" />
					</el-select>
				</el-form-item>
				<el-form-item label="来电号码" prop="FromPhone">
					<el-input v-model.trim="state.queryParams.FromPhone" placeholder="来电号码" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="一级部门" prop="OrgLevelOneName">
					<el-input v-model="state.queryParams.OrgLevelOneName" placeholder="一级部门" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="接办部门" prop="ActualHandleOrgName">
					<el-input v-model="state.queryParams.ActualHandleOrgName" placeholder="接办部门" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<!--				<el-form-item label="回访状态" prop="VisitStateQuery" v-if="['YiBin','LuZhou'].includes(themeConfig.appScope)">
					<el-select v-model="state.queryParams.VisitStateQuery" 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="slTime">
					<el-date-picker
						v-model="state.queryParams.slTime"
						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="['ZiGong'].includes(themeConfig.appScope)">
					<el-select v-model="state.queryParams.OrgHandledAttitude" placeholder="请选择部门办件态度" clearable multiple>
						<el-option v-for="items in visitMananer" :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-item label="来源渠道" prop="Channel">
					<el-select v-model="state.queryParams.Channel" placeholder="请选择来源渠道" clearable @change="handleQuery">
						<el-option v-for="item in channelOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item label="是否超期" prop="IsOverTime">
					<el-select v-model="state.queryParams.IsOverTime" placeholder="请选择是否超期" class="w100" clearable @change="handleQuery">
						<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="handleQuery" />
	</div>
</template>

<script setup lang="tsx" name="businessVisit">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { visitList, visitListExport, 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 pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
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 state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键字
		VisitStateQuery: 1, // 回访状态 1：已回访
		VisitType: null, // 回访方式
		No: null,
		IsCountersign: null, // 是否会签
		FiledType: null, // 归档类型
		IsProvince: null,
		EmployeeName: null,
		crTime: [],
		StartTime: null,
		EndTime: null,
		IsEffectiveAiVisit: null,
		VoiceEvaluate: [],
		SeatEvaluate: [],
		OrgProcessingResults: [],
		OrgHandledAttitude: [],
		IdentityType:null,
		FromPhone:null,
		OrgLevelOneName:null,
		ActualHandleOrgName:null,
		slTime:[], // 受理时间
		CreationTimeStart:null,
		CreationTimeEnd:null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	visitTypeOptions: [], // 回访方式
});

const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		tools: [{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	id: 'businessVisit',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns:[
		{
			field: 'order.expiredStatusText',
			title: '状态',
			width: 60,
			align: 'center',
			slots: {
				default: ({ row }) => {
					return <span class={'overdue-status-' + row.order?.expiredStatus} title={row.order.expiredStatusText}></span>;
				},
			},
		},
		{ field: 'order.no', title: '工单编码', width: 140 },
		{ field: 'order.isProvinceText', title: '省/市工单', width: 90 },
		{
			field: 'order.title',
			title: '工单标题',
			minWidth: 200,
			slots: { default: 'order_detail' },
		},
		{ field: 'order.sourceChannel', title: '来源渠道', width: 110 },
		{ field: 'visitStateText', title: '回访状态', width: 100 },
		{ field: 'visitTypeText', title: '回访方式', width: 100 },
		{ field: 'isEffectiveAiVisitText', title: '有效智能回访', width: 120 },
		{ field: 'order.acceptType', title: '受理类型', width: 110 },
		{ field: 'order.acceptorName', title: '受理人', width: 120 },
		{ field: 'order.hotspotName', title: '热点分类', width: 150 },
		{ field: 'order.orgLevelOneName', title: '一级部门', width: 150 },
		{ field: 'order.actualHandleOrgName', title: '接办部门', width: 140 },
		{
			field: 'order.startTime',
			title: '受理时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'order.filedTime',
			title: '办结时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'publishTime',
			title: '发布时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'creationTime',
			title: '回访任务创建时间',
			width: 160,
			formatter: 'formatDate',
		},
		{ field: 'employeeName', title: '回访人', width: 120 },
		{
			field: 'visitTime',
			title: '回访时间',
			width: 160,
			formatter: 'formatDate',
		},
		{ field: 'order.seatEvaluateTxt', title: '话务员满意度', width: 140 },
	],
	data: [],
	params: {
		exportMethod: visitListExport,
		exportParams: requestParams,
	},
	sortConfig: {
		remote: true,
	},
});
if(['YiBin'].includes(themeConfig.value.appScope)){
	gridOptions.columns.push({
		title: '操作', width: 100, fixed: 'right', align: 'center', slots: { default: 'actionYB' }
	})
}else if(['ZiGong','LuZhou'].includes(themeConfig.value.appScope)){
	gridOptions.columns = gridOptions.columns.filter((item:any) => item.field !== 'isEffectiveAiVisitText');
	gridOptions.columns.push({
		title: '操作', width: 200, fixed: 'right', align: 'center', slots: { default: 'action' }
	})
}
// 获取基础数据
const seatEvaluate = ref<EmptyArrayType>([]); // 话务员评价
const voiceEvaluate = ref<EmptyArrayType>([]); // 语音评价
const visitMananer = ref<EmptyArrayType>([]); // 部门办件态度
const visitSatisfaction = ref<EmptyArrayType>([]); // 部门办件结果
const visitStateQuery = ref<EmptyArrayType>([]); // 回访状态
const channelOptions = ref<EmptyArrayType>([]); // 来源渠道
const getBaseData = () => {
	visitSearchBaseData().then(({ result }) => {
		state.visitTypeOptions = result?.visitType ?? [];
		seatEvaluate.value = result?.seatEvaluate ?? [];
		voiceEvaluate.value = result?.voiceEvaluate ?? [];
		visitMananer.value = result?.visitMananer ?? [];
		visitSatisfaction.value = result?.visitSatisfaction ?? [];
		visitStateQuery.value = result?.visitStateQuery ?? [];
		channelOptions.value = result?.sourceChannel ?? [];
	});
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	return new Promise((resolve, reject) => {
		state.loading = true;
		gridOptions.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');
		requestParams.value.CreationTimeStart = state.queryParams.slTime === null ? null : state.queryParams.slTime[0];
		requestParams.value.CreationTimeEnd = state.queryParams.slTime === null ? null : state.queryParams.slTime[1];
		Reflect.deleteProperty(requestParams.value, 'slTime');
		visitList(requestParams.value)
			.then((res: any) => {
				state.tableData = res.result?.items ?? [];
				state.total = res.result?.total ?? 0;
				gridOptions.data = state.tableData;
				state.loading = false;
				gridOptions.loading = false;
				resolve(res);
			})
			.catch(() => {
				state.loading = false;
				gridOptions.loading = false;
				reject();
			});
	});
};

/** 重置按钮操作 */
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(() => {
	queryList().then(() => {
		getBaseData();
	});
});
</script>
