<template>
	<div class="statistics-center-seats-return-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef" v-on="gridEvents" @checkbox-all="selectAllChangeEvent" @checkbox-change="selectChangeEvent">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="gridOptions.loading">
						<el-form-item prop="slTime">
							<statistical-time v-model="state.queryParams.slTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading" />
						</el-form-item>
						<el-form-item label="姓名" prop="Name">
							<el-input v-model="state.queryParams.Name" 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 #toolbar_buttons>
					<el-button type="primary" @click="onJbExport" :disabled="isChecked" :loading="state.loading"
						><SvgIcon name="iconfont icon-daochu" class="mr5" />交办单导出<span v-if="checkTable.length">({{ checkTable.length }})</span></el-button
					>
				</template>
				<template #order_detail="{ row }">
					<order-detail :order="{ id: row.orderId }" @updateList="queryList">{{ row.title }}</order-detail>
				</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="90PX" :disabled="gridOptions.loading">
				<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="工单标题" prop="Title">
					<el-input v-model.trim="state.queryParams.Title" placeholder="工单标题" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</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="statisticsCenterSeatsReturn">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import { exportAssignment } from '@/utils/tools';
import Other from '@/utils/other';
import { returnBase } from '@/api/business/return';
import { centerRefundSeat, centerRefundSeatExport } from '@/api/statistics/center';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键字
		No: null, // 工单编码
		Title: null, // 工单标题
		slTime: defaultDate, // 受理时间
		StartTime: null,
		EndTime: null,
		IndustrialName: null, // 行业名称
		HotspotSpliceName: null, // 热点全称
		OrgLevelOneName: null, // 一级部门
		ActualHandleOrgName: 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' } }],
		slots: {
			buttons: 'toolbar_buttons',
		},
	},
	customConfig: {
		storage: true,
	},
	id: 'statisticsCenterSeatsReturn',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ type: 'checkbox', width: 50, align: 'center' },
		{ field: 'no', title: '工单编码', width: 140 },
		{
			field: 'title',
			title: '工单标题',
			minWidth: 200,
			slots: { default: 'order_detail' },
		},
		{ field: 'name', title: '姓名', width: 110 },
		{
			field: 'orderCreationTime',
			title: '受理时间',
			width: 160,
			formatter: 'formatDate',
			// sortable: true,
		},
		{ field: 'acceptType', title: '受理类型', width: 150 },
		{ field: 'sourceChannel', title: '来源渠道', width: 140 },
	],
	data: [],
	params: {
		exportMethod: centerRefundSeatExport,
		exportParams: requestParams,
	},
	sortConfig: {
		remote: true,
	},
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.StartTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[0]; // 受理时间
	requestParams.value.EndTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[1];
	Reflect.deleteProperty(requestParams.value, 'slTime'); // 删除无用的参数
	state.loading = false;
	centerRefundSeat(requestParams.value)
		.then((res: any) => {
			gridOptions.data = res?.result.items ?? [];
			state.total = res?.result.total ?? 0;
			state.loading = false;
			gridOptions.loading = false;
			gridRef.value.clearCheckboxRow();
			checkTable.value = [];
		})
		.catch(() => {
			state.loading = false;
			gridOptions.loading = false;
			gridRef.value.clearCheckboxRow();
			checkTable.value = [];
		});
};
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 drawerRuleFormRef = ref<RefType>();
const drawer = ref(false);
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
// 交办单导出
const onJbExport = () => {
	const ids = checkTable.value.map((item: any) => item.orderId);
	exportAssignment(ids);
};
const checkTable = ref<EmptyArrayType>([]);
const gridRef = ref<RefType>();
const selectAllChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !checkTable.value.length;
});
const acceptTypeOptions = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	// 获取基础数据
	try {
		const { result } = await returnBase();
		acceptTypeOptions.value = result.acceptTypeOptions;
	} catch (e) {
		console.log(e);
	}
};
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
