<template>
	<div class="statistics-center-detail-seat-satisfaction-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="gridOptions.loading">
						<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"
								:clearable="false"
							/>
						</el-form-item>
						<el-form-item label="工单标题" prop="Title">
							<el-input v-model="state.queryParams.Title" 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>
							<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 #statusText="{ row }">
					<el-text type="danger" tag="b" v-if="[1, 2, 3, 9, 101, 102, 103, 104, 105, 200].includes(row.order?.status)">{{
						row.order?.statusText
					}}</el-text>
					<span v-else>{{ row.order?.statusText }}</span>
				</template>
				<template #order_detail="{ row }">
					<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</template>
				<template #action="{ row }">
					<el-button link type="primary" @click="visitDetail(row)" title="查看回访详情"> 回访详情 </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>

		<!-- 回访详情 -->
		<visit-detail-com ref="visitDetailRef" @updateList="queryList" />
		<!--	更多查询	-->
		<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="UserName">
					<el-input v-model="state.queryParams.UserName" placeholder="受理人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="满意度" prop="SeatEvaluate">
					<el-select v-model="state.queryParams.SeatEvaluate" placeholder="请选择满意度" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in seatEvaluate" :value="item.key" :key="item.key" :label="item.value" />
					</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>
	</div>
</template>
<script setup lang="tsx" name="statisticsCenterDetailSeatSatisfaction">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { useRoute } from 'vue-router';
import { centerSatisfactionDetailBasicData, centerSatisfactionDetailData, centerSatisfactionDetailDataExport } from '@/api/statistics/center';
import Other from '@/utils/other';
import { FormInstance } from 'element-plus';
import { defaultDate, defaultTimeStartEnd, shortcuts } from '@/utils/constants';

// 引入组件
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 state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		// 查询条件
		crTime: defaultDate, //
		StartTime: null,
		EndTime: null,
		UserName: null,
		SeatEvaluate: null,
		Title: null,
		No: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});

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: 'statisticsCenterDetailSeatSatisfaction',
	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.statusText',
			title: '工单状态',
			width: 110,
			slots: {
				default: 'statusText',
			},
		},
		{ field: 'order.sourceChannel', title: '来源渠道', width: 110 },
		{ field: 'order.no', title: '工单编码', width: 140 },
		{
			field: 'order.startTime',
			title: '受理时间',
			width: 160,
			formatter: 'formatDate',
		},
		{ field: 'order.acceptorName', title: '受理人', width: 120 },
		{
			field: 'order.title',
			title: '工单标题',
			minWidth: 200,
			slots: { default: 'order_detail' },
		},
		{ field: 'orderVisit.employeeName', title: '回访人', width: 120 },
		{
			field: 'orderVisit.visitTime',
			title: '回访时间',
			width: 160,
			formatter: 'formatDate',
		},
		{ field: 'seatEvaluateText', title: '坐席满意度', width: 140 },
		{
			field: 'order.expiredTime',
			title: '工单期满时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'order.actualHandleTime',
			title: '接办时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'order.filedTime',
			title: '办结时间',
			width: 160,
			formatter: 'formatDate',
		},
		{ field: 'order.acceptType', title: '受理类型', width: 110 },
		{ field: 'order.hotspotName', title: '热点分类', width: 150 },
		{ field: 'orderVisit.visitTypeText', title: '回访方式', width: 100 },
		{ title: '操作', width: 90, fixed: 'right', align: 'center', slots: { default: 'action' } },
	],
	data: [],
	params: {
		exportMethod: centerSatisfactionDetailDataExport,
		exportParams: requestParams,
	},
	sortConfig: {
		remote: true,
	},
});
const route = useRoute(); // 路由
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};

// 回访详情
const visitDetailRef = ref<RefType>();
const visitDetail = (row: any) => {
	visitDetailRef.value.openDialog(row.orderVisit, '回访详情');
};
const routeQueryParams = route.query;
/** 获取列表 */
const queryList = async () => {
	try {
		if (routeQueryParams.SeatEvaluate) state.queryParams.SeatEvaluate = Number(routeQueryParams.SeatEvaluate);
		if(routeQueryParams.UserId) state.queryParams.UserId = routeQueryParams.UserId;
		state.queryParams.crTime = [routeQueryParams.StartTime, routeQueryParams.EndTime];
		state.loading = true;
		gridOptions.loading = true;
		requestParams.value = Other.deepClone(state.queryParams);
		requestParams.value.StartTime = state.queryParams.crTime[0];
		requestParams.value.EndTime = state.queryParams.crTime[1];
		Reflect.deleteProperty(requestParams.value, 'crTime');
		const { result } = await centerSatisfactionDetailData(requestParams.value);
		state.tableData = result.items ?? [];
		state.total = result.total;
		gridOptions.data = state.tableData;
		state.loading = false;
		gridOptions.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
		gridOptions.loading = false;
	}
};
/** 重置按钮操作 */
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
};
// 获取基础数据
const seatEvaluate = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	try {
		const { result } = await centerSatisfactionDetailBasicData();
		seatEvaluate.value = result.seatEvaluate;
	} catch (e) {
		console.log(e);
	}
};
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
