<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" :disabled="loading">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="所属区域" prop="systemAreaId" :rules="[{ required: false, message: '请选择所属区域', trigger: 'change' }]">
						<el-cascader
							:options="areaOptions"
							class="w100"
							@change="selectArea"
							ref="areaRef"
							filterable
							:props="{
								label: 'areaName',
								value: 'id',
								checkStrictly: true,
							}"
							clearable
							placeholder="请选择所属区域"
							v-model="state.ruleForm.systemAreaId"
						/>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="所属镇/街" prop="street" :rules="[{ required: true, message: '请填写所属镇/街', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.street" 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="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="phoneNumber" :rules="[{ required: true, message: '请填写联系方式', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.phoneNumber" placeholder="请填写联系方式" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
					<el-form-item label="性别" prop="gender" :rules="[{ required: true, message: '请选择性别', trigger: 'change' }]">
						<el-radio-group v-model="state.ruleForm.gender">
							<el-radio :value="1">先生</el-radio>
							<el-radio :value="0">女士</el-radio>
						</el-radio-group>
					</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="parameterEdit">
import {  reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { baseAreaUser, editAreaUser, getAreaUserDetail } from '@/api/snapshot/config';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		systemAreaId: null, // 所属区域
		systemAreaName: null, // 所属区域名称
		street: null, // 所属镇/街
		name: null, // 姓名
		phoneNumber: null, // 联系方式
		gender: 1, // 性别
	},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = async (id: string) => {
	await getBaseData();
	try {
		const res: any = await getAreaUserDetail(id);
		state.ruleForm = res.result ?? <EmptyObjectType>{};
		state.dialogVisible = true;
	} catch (error) {
		console.log(error);
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.resetFields();
	ruleFormRef.value?.resetFields();
};
// 选择区域
const areaRef = ref<RefType>();
const selectArea = (val: any) => {
	const currentNode = areaRef.value?.getCheckedNodes()[0];
	if (currentNode) {
		state.ruleForm.systemAreaName = currentNode.label;
	} else {
		state.ruleForm.systemAreaName = null;
	}
};
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		editAreaUser(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '操作成功',
					type: 'success',
				});
				loading.value = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
				// 新增失败
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
const areaOptions = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	try {
		const { result } = await baseAreaUser();
		areaOptions.value = result.area;
	} catch (e) {
		console.log(e);
	}
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
