<template>
	<div class="statistics-department-detail-overdue-list-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 prop="crTime">
					<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading" />
				</el-form-item>
				<el-form-item label="快捷查询" prop="ExpiredType">
					<el-segmented
						:options="[
							{
								value: '1',
								label: '系统中超期',
							},
							{
								value: '2',
								label: '申请延期超期',
							},
						]"
						v-model="state.queryParams.ExpiredType"
						@change="handleQuery"
						:disabled="state.loading"
					/>
				</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="">
					<div class="flex-end w100">
						<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>
					</div>
				</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" :disabled="isChecked" :loading="state.loading"
						><SvgIcon name="iconfont icon-daochu" class="mr5" />交办单导出
					</el-button>
				</template>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30,useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="statisticsDepartmentDetailOverdueList"
					:custom-config="{ storage: true }"
					showHeaderOverflow
					:sort-config="{ remote: true }"
					:params="{ exportMethod: departmentOverdueListExport, exportParams: requestParams }"
					@checkbox-all="selectAllChangeEvent"
					@checkbox-change="selectChangeEvent"
				>
					<vxe-column type="checkbox" width="50" align="center"></vxe-column>
					<vxe-column field="no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="title" title="工单标题" min-width="200">
						<template #default="{ row }">
							<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="isProvinceText" title="省/市工单" width="110"></vxe-column>
					<vxe-column field="currentStepName" title="当前节点" width="110"></vxe-column>
					<vxe-column field="sourceChannel" title="来源渠道" width="110"></vxe-column>
					<vxe-column field="acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="counterSignTypeText" title="受理类型" width="110"></vxe-column>
					<vxe-column field="hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="acceptorName" title="受理人" width="120"></vxe-column>
					<vxe-column field="startTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.startTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="expiredTime" title="工单期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="filedTime" title="办结时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.filedTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="overDays" title="超期天数" width="110"></vxe-column>
					<vxe-column field="statusText" title="工单状态" width="110">
						<template #default="{ row }">
							<el-text type="danger" tag="b" v-if="[1, 2, 3, 9, 101, 102, 103, 104, 105, 200].includes(row.status)">{{ row.statusText }}</el-text>
							<span v-else>{{ row.statusText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="orgLevelOneName" title="一级部门" width="140"></vxe-column>
					<vxe-column field="actualHandleOrgName" title="接办部门" width="140"></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>
		<!--	更多查询	-->
		<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="OrgName">
					<el-input v-model="state.queryParams.OrgName" placeholder="部门名称" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="工单状态" prop="Statuses">
					<el-select v-model="state.queryParams.Statuses" placeholder="请选择工单状态" clearable class="w100" multiple>
						<el-option v-for="item in state.orderStatusOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="来电主体" prop="TypeId">
					<el-select v-model="state.queryParams.TypeId" 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>
			<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="statisticsDepartmentDetailOverdueList">
import { onMounted, reactive, ref, defineAsyncComponent, computed } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentOverdueBase, departmentOverdueList, departmentOverdueListExport } from '@/api/statistics/department';
import { formatDate } from '@/utils/formatTime';
import { defaultDateTime } from '@/utils/constants';
import Other from '@/utils/other';
import { exportAssignment } from '@/utils/tools';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		ExpiredType: '1',
		crTime: defaultDateTime,
		StartTime: null,
		EndTime: null,
		No: null,
		Statuses: [], // 状态
		OrgName: null, // 部门名称
		TypeId: 0,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	orderStatusOptions: [], // 工单状态
});
/** 搜索按钮操作 */
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'); // 删除无用的参数
	departmentOverdueList(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 statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.ExpiredType = '1';
	ruleFormRef.value?.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
// 获取查询条件基础信息
const getBaseData = async () => {
	try {
		const { result } = await departmentOverdueBase();
		state.orderStatusOptions = result.orderStatusOptions ?? [];
	} catch (error) {
		console.log(error);
	}
};
// 交办单导出
const onJbExport = () => {
	const ids = checkTable.value.map((item: any) => item.id);
	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>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
