<template>
	<el-dialog v-model="state.dialogVisible" width="769px" draggable title="新增用户" ref="dialogRef" append-to-body destroy-on-close @close="close">
		<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-divider content-position="left"> <el-text tag="b" size="large">基本信息</el-text> </el-divider>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="姓名" prop="name" :rules="[{ required: true, message: '请填写姓名', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.name" 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="userName" :rules="[{ required: true, message: '请填写账号', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.userName" 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="phoneNo" :rules="[{ required: isPhoneRequired, message: '请填写手机号', trigger: 'blur' }]">
						<el-input v-model.trim="state.ruleForm.phoneNo" 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="landlineNumber" :rules="[{ required: false, message: '请填写座机号', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.landlineNumber" 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="gender" :rules="[{ required: false, message: '请选择性别', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.gender" placeholder="请选择性别" class="w100">
							<el-option v-for="item in props.baseData.genderOptions" :key="item.key" :label="item.value" :value="item.key" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="所属部门" prop="orgId" :rules="[{ required: true, message: '请选择所属部门', trigger: 'change' }]">
						<el-cascader
							:options="props.baseOrg"
							filterable
							:props="{ checkStrictly: true, value: 'id', label: 'name', emitPath: false }"
							placeholder="请选择所属部门"
							clearable
							class="w100"
							v-model="state.ruleForm.orgId"
							ref="cascadeRef"
							@change="getKnowledgeList"
						>
						</el-cascader>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="默认分机" prop="defaultTelNo" :rules="[{ required: false, message: '请选择默认分机', trigger: 'change' }]">
						<el-select-v2
							v-model="state.ruleForm.defaultTelNo"
							:options="props.telsList"
							placeholder="请选择默认分机"
							filterable
							class="w100"
							:props="{
								label: 'no',
								value: 'no',
							}"
							clearable
						/>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="默认分机组" prop="defaultTelGroup" :rules="[{ required: false, message: '请选择默认分机组', trigger: 'change' }]">
						<el-select-v2
							v-model="state.ruleForm.defaultTelGroup"
							:options="props.telsListGroup"
							placeholder="请选择默认分机组"
							filterable
							class="w100"
							:props="{
								label: 'no',
								value: 'no',
							}"
							clearable
						>
						</el-select-v2>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="邮箱" prop="email" :rules="[{ required: false, message: '请填写邮箱', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.email" 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="staffNo" :rules="[{ required: false, message: '请填写工号', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.staffNo" 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="userType" :rules="[{ required: true, message: '请选择用户类型', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.userType" placeholder="请选择用户类型" class="w100">
							<el-option v-for="item in props.baseData.userTypeOptions" :key="item.key" :label="item.value" :value="item.key" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-divider content-position="left"> <el-text tag="b" size="large">角色配置</el-text> </el-divider>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="角色" prop="roleIds" :rules="[{ required: true, message: '请选择角色', trigger: 'change' }]">
						<el-select-v2
							v-model="state.ruleForm.roleIds"
							filterable
							multiple
							:options="props.roles"
							class="w100"
							placeholder="请选择角色"
							clearable
							collapse-tags
							collapse-tags-tooltip
							:props="{
								label: 'displayName',
								value: 'id',
							}"
						/>
					</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>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="systemUserAdd">
import { computed, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { addUser } from '@/api/system/user';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
const props = defineProps<{
	roles: {
		// 角色
		type: Array<any>;
		default: () => [];
	};
	baseOrg: {
		// 组织
		type: Array<any>;
		default: () => [];
	};
	baseData: {
		// 基础数据
		type: Array<any>;
		default: () => [];
	};
	telsList: {
		// 分机
		type: Array<any>;
		default: () => [];
	};
	telsListGroup: {
		// 分机组
		type: Array<any>;
		default: () => [];
	};
}>();
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	ruleForm: {
		phoneNo: '', // 手机号
		gender: 0, //性别
		name: '', // 姓名
		userName: '', // 用户昵称
		orgId: '', // 组织id
		orgCode: '', //组织code
		staffNo: '', //工号
		userType: 0, // 用户类型
		defaultTelNo: null, //默认分机
		email: '', //邮箱
		fullOrgName: null, // 部门全称
		roleIds: <EmptyArrayType>[], // 角色
		defaultTelGroup: null, // 默认分机组
		landlineNumber:null, // 座机号
	},
});
let loading = ref<boolean>(false); // 确定按钮loading
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = () => {
	state.dialogVisible = true;
};
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
// 判断用户是否必填 自贡非必填 其他必填
const isPhoneRequired = computed(()=>{
	return ['YiBin','LuZhou'].includes(themeConfig.value.appScope)
})
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
const cascadeRef = ref<RefType>();
// 获取选择组织name值
const getKnowledgeList = () => {
	let currentNode = cascadeRef.value.getCheckedNodes();
	state.ruleForm.orgCode = currentNode[0]?.data.id ?? '';
	state.ruleForm.fullOrgName = currentNode[0]?.text ?? '';
};

// 新增
const onSubmit = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		addUser(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '新增成功',
					type: 'success',
				});
				loading.value = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
				// 新增失败
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
