<template>
	<div class="statistics-department-satisfaction-detail-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item label="来电主体" prop="TypeCode">
					<el-select v-model="state.queryParams.TypeCode" placeholder="请选择来电主体" @change="handleQuery">
						<el-option :value="0" label="全部" />
						<el-option :value="1" label="市民" />
						<el-option :value="2" label="企业" />
					</el-select>
				</el-form-item>
				<el-form-item label="回访方式" prop="VisitType">
					<el-select v-model="state.queryParams.VisitType" placeholder="请选择回访方式" clearable @change="handleQuery">
						<el-option v-for="item in state.visitType" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<!--        <el-form-item label="超期类型" prop="ExpiredType">
					<el-select v-model="state.queryParams.ExpiredType" placeholder="请选择超期类型" clearable @change="handleQuery">
						<el-option  value="1" label="系统中超期"></el-option>
						<el-option  value="2" label="申请延期超期"></el-option>
					</el-select>
				</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="resetQuery(ruleFormRef)" class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }]"
			>
				<template #buttons>
					<el-button type="primary" @click="onJbExport" :loading="state.loading" :disabled="isChecked"
						><SvgIcon name="iconfont icon-daochu" class="mr5" />交办单导出
					</el-button>
					<el-popover :width="500" trigger="click">
						<template #reference>
							<el-button type="primary" title="口径说明"><SvgIcon name="ele-QuestionFilled" class="mr5" />口径说明</el-button>
						</template>
						<el-descriptions title="" :column="1" border>
							<el-descriptions-item label="工单编码">工单编号</el-descriptions-item>
							<el-descriptions-item label="重办次数">发布时重办的次数</el-descriptions-item>
							<el-descriptions-item label="甄别">甄别中、未甄别、甄别退回、不同意、</el-descriptions-item>
							<el-descriptions-item label="回访信息"> 工单回访内容 </el-descriptions-item>
							<el-descriptions-item label="受理时间"> 工单新建时间 </el-descriptions-item>
							<el-descriptions-item label="工单标题"> 工单的标题(点击可查看工单详情)</el-descriptions-item>
							<el-descriptions-item label="回访人"> 工单回访用户</el-descriptions-item>
							<el-descriptions-item label="回访部门"> 工单回访用户所在部门 </el-descriptions-item>
							<el-descriptions-item label="回访时间"> 工单回访时间 </el-descriptions-item>
							<el-descriptions-item label="满意度"> 回访时的满意度 </el-descriptions-item>
							<el-descriptions-item label="办结时间"> 工单办理完成的时间 </el-descriptions-item>
						</el-descriptions>
					</el-popover>
				</template>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:sort-config="{ remote: true }"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
					ref="tableRef"
					@sort-change="sortChange"
					@checkbox-all="selectAllChangeEvent"
					@checkbox-change="selectChangeEvent"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="statisticsDepartmentSatisfiedDetail"
					:custom-config="{ storage: true }"
					:params="{ exportMethod: departmentSatisfactionDetailExport, exportParams: requestParams }"
				>
					<vxe-column type="checkbox" width="60" align="center"></vxe-column>
					<vxe-column field="no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="reTransactNum" title="重办次数" width="90"></vxe-column>
					<vxe-column field="orderScreenStatusText" title="甄别" width="90"></vxe-column>
					<vxe-column field="visitContent" title="回访信息" width="150"></vxe-column>
					<vxe-column field="hotspotSpliceName" title="热点全称" width="150"></vxe-column>
					<vxe-column field="orgLevelOneName" title="一级部门" width="140"></vxe-column>
					<vxe-column field="actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="creationTime" title="受理时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="title" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="{ ...row, id: row.orderId }" @updateList="queryList">{{ row.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="visitUser" title="回访人" width="120"></vxe-column>
					<vxe-column field="visitOrgName" title="回访部门" width="140"></vxe-column>
					<vxe-column field="visitTypeText" title="回访方式" width="110"></vxe-column>
					<vxe-column field="visitTime" title="回访时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.visitTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="orgProcessingResults" title="满意度" width="120"></vxe-column>
					<vxe-column field="content" title="受理内容" width="150"></vxe-column>
					<vxe-column field="fileOpinion" title="承办意见" width="150"></vxe-column>
					<vxe-column field="filedTime" title="办结时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.filedTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" fixed="right" width="90" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="visitDetail(row)" title="查看回访详情"> 回访详情 </el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
		<!-- 回访详情 -->
		<visit-detail-com ref="visitDetailRef" @updateList="queryList" />
	</div>
</template>
<script setup lang="tsx" name="statisticsDepartmentSatisfiedDetail">
import { onMounted, reactive, ref, defineAsyncComponent, computed } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentSatisfactionBase, departmentSatisfactionDetail, departmentSatisfactionDetailExport } from '@/api/statistics/department';
import { formatDate } from '@/utils/formatTime';
import { defaultDate } from '@/utils/constants';
import { useRoute } from 'vue-router';
import { exportAssignment } from '@/utils/tools';

// 引入组件
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 ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		TypeId: '1', //
		LineNum: null,
		crTime: defaultDate, // 时间默认今天开始到今天结束
		TypeCode: 0,
		SortField: null,
		SortRule: null,
		visitType: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	visitType: [],
});
// 排序
const sortChange = (val: any) => {
	state.queryParams.SortField = val.order ? val.field : null;
	// 0 升序 1 降序
	state.queryParams.SortRule = val.order ? (val.order == 'desc' ? 1 : 0) : null;
	queryList();
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};

/** 获取列表 */
const route = useRoute();
const routeQueryParams = route.query;
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = {
		StartTime: routeQueryParams.StartTime,
		EndTime: routeQueryParams.EndTime,
		TypeId: routeQueryParams.TypeId,
		LineNum: routeQueryParams.LineNum,
		OrgCode: routeQueryParams.OrgCode,
		DateValue: routeQueryParams.DateValue,
		PageIndex: state.queryParams.PageIndex,
		PageSize: state.queryParams.PageSize,
		IsOnlyMy: routeQueryParams.IsOnlyMy,
		TypeCode: state.queryParams.TypeCode,
		SortField: state.queryParams.SortField,
		SortRule: state.queryParams.SortRule,
		VisitType: state.queryParams.VisitType,
		PickOrgCode: routeQueryParams.PickOrgCode,
	};
	departmentSatisfactionDetail(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 回访详情
const visitDetailRef = ref<RefType>();
const visitDetail = (row: any) => {
	visitDetailRef.value.openDialog({ ...row, id: row.visitId }, '回访详情');
};
// 交办单导出
const onJbExport = () => {
	const ids = checkTable.value.map((item: any) => item.orderId);
	exportAssignment(ids);
};
const checkTable = ref<EmptyArrayType>([]);
const selectAllChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !Boolean(checkTable.value.length);
});
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
// 获取查询基础数据
const getBaseData = async () => {
	try {
		const { result } = await departmentSatisfactionBase();
		state.visitType = result.visitType;
	} catch (e) {
		console.log(e);
	}
};
onMounted(() => {
	queryList();
	getBaseData();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
