<template>
	<el-dialog v-model="state.dialogVisible" draggable title="编辑行业" @close="close" append-to-body destroy-on-close>
		<el-form :model="state.ruleForm" label-width="100px" ref="ruleFormRef" :disabled="loading">
			<el-form-item label="行业名称" prop="industryName" :rules="[{ required: true, message: '请填写行业名称', trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.industryName" placeholder="请填写行业名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="关联部门" prop="orgsIds" :rules="[{ required: true, message: '请选择关联部门', trigger: 'change' }]">
				<div class="w100">
					<el-cascader
						:options="state.orgData"
						:props="{
							multiple: true,
							checkStrictly: true,
							emitPath: false,
							value: 'orgId',
							label: 'orgName',
							disabled: 'isDisable',
						}"
						clearable
						v-model="state.ruleForm.orgsIds"
						filterable
						class="w100"
						placeholder="请选择关联部门"
						collapse-tags
						collapse-tags-tooltip
						:max-collapse-tags="3"
					/>
				</div>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { updateAuxIndustry, getAuxIndustryTree, getAuxIndustryDetail } from '@/api/auxiliary/industry';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		industryName: null, // 行业名称
		orgsIds: [], // 部门
	},
	orgData: [], // 部门数据
});
let loading = ref<boolean>(false); // 加载状态
// 获取部门数据
const getOrgListApi = async (row: any) => {
	loading.value = true;
	try {
		const { result } = await getAuxIndustryTree(row.id);
		state.orgData = result ?? []; //部门
		loading.value = false;
		await getDetailApi(row.id);
	} catch (e) {
		console.log(e);
		loading.value = false;
	}
};
// 查询详情
const getDetailApi = async (id: string) => {
	loading.value = true;
	try {
		const { result } = await getAuxIndustryDetail(id);
		state.ruleForm = {
			id: result.id,
			industryName: result.industrialName,
			orgsIds: result.orgs.map((item: any) => item.id),
		};
		loading.value = false;
	} catch (e) {
		console.log(e);
		loading.value = false;
	}
};
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = (row: any) => {
	state.dialogVisible = true;
	getOrgListApi(row);
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.resetFields();
	ruleFormRef.value?.resetFields();
};
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		updateAuxIndustry(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '操作成功',
					type: 'success',
				});
				emit('updateList');
			})
			.catch((error) => {})
			.finally(() => {
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
