<template>
	<div class="statistics-knowledge-data-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" v-on="gridEvents" ref="gridRef">
				<template #form>
					<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="Keyword">
							<el-input v-model="state.queryParams.Keyword" 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="resetQuery(ruleFormRef)" class="default-button" :loading="state.loading">
								<SvgIcon name="ele-Refresh" class="mr5" />重置
							</el-button>
						</el-form-item>
					</el-form>
				</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>
	</div>
</template>
<script setup lang="tsx" name="statisticsKnowledgeData">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { knowledgeExport, knowledgeList } from '@/api/statistics/knowledge';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/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,
		Keyword: null, // 关键词
		crTime: defaultDate, // 时间默认今天开始到今天结束
		SortField: null,
		SortRule: 0,
	},
	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,
	},
	sortConfig: {
		remote: true,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		tools: [{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	id: 'statisticsKnowledgeData',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{
			field: 'orgName',
			title: '部门名称',
		},
		{
			field: 'orgTypeText',
			title: '部门类型',
		},
		{
			field: 'addNum',
			title: '新增数量',
			sortable: true,
		},
		{
			field: 'deleteNum',
			title: '删除数量',
			sortable: true,
		},
		{
			field: 'publicNum',
			title: '公开数量',
			sortable: true,
		},
		{
			title: '公开数量',
			children: [
				{
					field: 'sendBackNum',
					title: '退回数量',
					sortable: true,
				},
				{
					field: 'passNum',
					title: '通过数量',
					sortable: true,
				},
			],
		},
	],
	data: [],
	params: {
		exportMethod: knowledgeExport,
		exportParams: requestParams,
	},
});
const gridEvents = {
	sortChange(val: any) {
		state.queryParams.SortField = val.order ? val.field : null;
		// 0 升序 1 降序
		state.queryParams.SortRule = val.order ? (val.order == 'desc' ? 1 : 0) : null;
		handleQuery();
	},
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.CreationTimeStart = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.CreationTimeEnd = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime');
	knowledgeList(requestParams.value)
		.then((res: any) => {
			gridOptions.data = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
			gridOptions.loading = false;
		})
		.catch(() => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
onMounted(() => {
	queryList();
});
</script>
