<template>
	<el-dialog title="选择工单" v-model="dialogVisible" draggable append-to-body destroy-on-close>
		<el-form :model="state.queryParams" ref="ruleDialogFormRef" inline @submit.native.prevent :disabled="state.loading">
			<el-form-item label="工单标题" prop="Title">
				<el-input v-model="state.queryParams.Title" placeholder="工单标题" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="工单编码" prop="No">
				<el-input v-model="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" />
			</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(ruleDialogFormRef)" 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"
			:sort-config="{ remote: true }"
			:column-config="{ resizable: true }"
			:row-config="{ isHover: true, height: 30, keyField: 'id' }"
			ref="tableRef"
			max-height="500px"
			show-overflow
			:scrollY="{ enabled: true, gt: 100 }"
			@radio-change="radioChangeEvent"
			:radio-config="{ highlight: true }"
		>
			>
			<vxe-column type="radio" fixed="left" title="请选择" width="70" align="center"></vxe-column>
			<vxe-column field="title" title="工单标题" min-width="200">
				<template #default="{ row }">
					<order-detail :order="row.order">{{ row.order?.title }}</order-detail>
				</template>
			</vxe-column>
			<vxe-column field="order.no" title="工单编码" width="140"></vxe-column>
			<vxe-column field="order.acceptType" title="受理类型" width="110"></vxe-column>
			<vxe-column field="order.hotspotName" title="热点分类" width="150"></vxe-column>
			<vxe-column field="order.sourceChannel" title="来源渠道" width="110"></vxe-column>
			<vxe-column field="order.orgLevelOneName" title="一级部门" width="140"></vxe-column>
			<vxe-column field="order.actualHandleOrgName" title="接办部门" width="140"></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 @click="dialogVisible = false" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="!state.tableRadio">保 存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script lang="tsx" setup>
import { defineAsyncComponent, reactive, ref } from 'vue';
import { ElMessageBox, FormInstance } from 'element-plus';
import { publishedListFixed, publishedListFixedCount } from '@/api/business/publish';

const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情

// 定义子组件向父组件传值/事件
const emit = defineEmits(['choose']);

// 定义变量内容
const state = reactive({
	tableData: [], // 表格数据
	total: 0, // 总条数
	loading: false, // 加载
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		Title: null, // 关键词
		No: null,
	},
	tableRadio: '', // 选择的ID
});
const ruleDialogFormRef = ref<FormInstance>(); // 表单
const dialogVisible = ref(false); // 弹窗

// 打开弹窗
const openDialog = async (row: any) => {
	dialogVisible.value = true;
	try {
		queryList();
		getTotal();
		if (row) {
			state.tableRadio = row;
		} else {
			state.tableRadio = '';
		}
	} catch (error) {
		console.log(error);
		state.loading = false;
	}
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
	getTotal();
};
const tableRef = ref<RefType>();
const queryList = () => {
	state.loading = true;
	publishedListFixed(state.queryParams)
		.then((res: any) => {
			state.tableData = res.result ?? [];
			state.loading = false;
			const row = state.tableData.find((item) => item.order?.id === state.tableRadio);
			tableRef.value.setRadioRow(row);
		})
		.catch(() => {
			state.loading = false;
		});
};
// 查询总数
const getTotal = () => {
	publishedListFixedCount(state.queryParams)
		.then((res) => {
			state.total = res.result ?? 0;
		})
		.catch(() => {});
};
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.Title = null;
	state.queryParams.No = null;
	handleQuery();
};

// 关闭弹窗
const closeDialog = () => {
	dialogVisible.value = false;
};
const selectRow = ref<any>(null);
const radioChangeEvent = ({ row }) => {
	state.tableRadio = row.order?.id;
	selectRow.value = row;
};
// 选择工单
const onSubmit = () => {
	ElMessageBox.confirm(`是否将工单内容引用至“案例描述和案例办理结果”中？引用后原内容将清空`, '提示', {
		confirmButtonText: '引用',
		cancelButtonText: '不引用',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			emit('choose', { ...selectRow.value, isCopy: true });
			closeDialog();
		})
		.catch(() => {
			emit('choose', { ...selectRow.value, isCopy: false });
			closeDialog();
		});
};
//暴漏变量和方法
defineExpose({ closeDialog, openDialog });
</script>
