<template>
	<el-dialog v-model="state.dialogVisible" draggable title="重复工单选择" ref="dialogRef" width="60%" append-to-body>
		<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
			<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"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
			</el-form-item>
		</el-form>
		<vxe-table
			border
			:loading="state.loading"
			:data="state.tableData"
			:column-config="{ resizable: true }"
			:row-config="{ isCurrent: true, isHover: true, height: 30,useKey:true,keyField: 'id', }"
			ref="tableRef"
			show-overflow
			:scrollY="{ enabled: true, gt: 100 }"
			showHeaderOverflow
			@checkbox-all="selectAllChangeEvent"
			@checkbox-change="selectChangeEvent"
			max-height="500"
			:checkbox-config="{checkRowKeys: defaultSelectRowKeys}"
		>
			<vxe-column type="checkbox" width="60" align="center"></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="no" title="工单编码"></vxe-column>
			<vxe-column field="hotspotName" title="热点分类"></vxe-column>
			<vxe-column field="currentStepName" title="当前节点"></vxe-column>
			<vxe-column field="statusText" title="工单状态" width="160">
				<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-table>
		<pagination
			@pagination="queryList"
			:total="state.total"
			v-model:current-page="state.queryParams.PageIndex"
			v-model:page-size="state.queryParams.PageSize"
			:disabled="state.loading"
		/>
		<template #footer>
			<span class="dialog-footer">
				<el-button class="default-button" @click="state.dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="selectConfirm" :disabled="isChecked">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, defineAsyncComponent, computed } from 'vue';
import type { FormInstance } from 'element-plus';
import { historyOrderAll } from '@/api/business/order';

const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 引入组件
const emit = defineEmits(['orderRepeatSuccess']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		Keyword: null, // 关键字
	},
	tableData: [], // 表格数据
	total: 0, // 总条数
	loading: false, // 加载状态
	ruleForm: {}, // 表单数据
});
const ruleFormRef = ref<RefType>(); // 表单ref
// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm = row;
	queryList();
	state.dialogVisible = true;
};
const dialogRef = ref<RefType>();
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	handleQuery();
};
const defaultSelectRowKeys = ref<EmptyArrayType>([]);
/** 获取历史工单 */
const queryList = () => {
	state.loading = true;
	let request = {
		...state.queryParams,
		OrderId: state.ruleForm.id ?? null, //传入id 排除重复工单选择自己
		IndustryId: state.ruleForm.industryId ?? null,
	};
	historyOrderAll(request)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
			defaultSelectRowKeys.value = state.ruleForm.duplicateIds;
			// 循环赋值默认选中
			state.tableData.forEach((item: any) => {
				defaultSelectRowKeys.value.forEach((key: any) => {
					if (item.id === key) {
						checkTable.value.push(item);
					}
				});
			});
		})
		.catch(() => {
			state.loading = false;
		});
};
const tableRef = ref<RefType>();
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 selectConfirm = () => {
	emit('orderRepeatSuccess', checkTable.value);
};
// 清除选中
const clearSelection = () => {
	tableRef.value.clearCheckboxRow();
	checkTable.value = [];
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
	clearSelection
});
</script>
