<template>
	<el-dialog v-model="state.dialogVisible" width="50%" draggable title="新增邀请码" destroy-on-close append-to-body @close="close">
		<el-form :model="state.ruleForm" label-width="100px" ref="ruleFormRef">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="上级名称" prop="parentOrgId" :rules="[{ required: false, message: '请选择上级名称', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.parentOrgId" placeholder="请选择上级名称" class="w100" clearable filterable>
							<el-option v-for="item in orgNameOptions" :key="item.key" :label="item.value" :value="item.key" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="部门名称" prop="orgName" :rules="[{ required: true, message: '请填写部门名称', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.orgName" placeholder="请填写部门名称" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="邀请码" prop="beginCode" :rules="[{ required: true, message: '请填写邀请码', trigger: 'blur' }]">
						<el-input
							type="number"
							v-model.number="state.ruleForm.beginCode"
							placeholder="请填写邀请码"
							oninput="if(value.length > 5) value = value.slice(0, 5)"
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="邀请码2" prop="endCode" :rules="[{ required: true, message: '请填写邀请码2', trigger: 'blur' }]">
						<el-input
							type="number"
							v-model.number="state.ruleForm.endCode"
							placeholder="请填写邀请码2"
							oninput="if(value.length > 5) value = value.slice(0, 5)"
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</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>
				<el-button type="primary" @click="onQrcode" :loading="loading">生成二维码</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { SetingsAdd } from '@/api/system/parameter';
import { addClue, baseClue, baseIndustry } from '@/api/snapshot/config';
import { addInviteCode, getInviteCodeBaseData } from '@/api/snapshot/inviteCode';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		beginCode:0, // 邀请码
		endCode:0, // 邀请码2
		parentOrgId:null, // 上级名称
		orgName:null, // 部门名称
	},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = async () => {
	await getBaseData();
	try {
		state.dialogVisible = true;
	} catch (error) {
		console.log(error);
	}
};
// 关闭弹窗
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;
		addInviteCode(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '操作成功',
					type: 'success',
				});
				emit('updateList');
			})
			.catch(() => {})
			.finally(() => {
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
const orgNameOptions = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	try {
		const { result } = await getInviteCodeBaseData();
		orgNameOptions.value = result.OrgName ?? [];
	} catch (e) {
		console.log(e);
	}
};
// 生成二维码
const onQrcode =()=>{

}
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
