<template>
  <el-dialog v-model="dialogVisible" title="编辑层级">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
      <el-form-item label="层级" prop="level">
        <el-select v-model="formData.level" placeholder="请选择层级" :clearable="false">
          <el-option v-for="item in KHLX" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, defineExpose, defineEmits, getCurrentInstance } from 'vue'
import { ElMessage } from 'element-plus'
import { getApi, postApi } from "@/api/tobAcco_api.js";

const { proxy } = getCurrentInstance()

const emits = defineEmits(['close'])
const { KHLX } = proxy.$dict("KHLX");

// 外部传入的层级选项


// 表单数据
const formData = ref({
  level: null
})

// 表单验证规则
const rules = {
  level: [
    { required: true, message: '请选择层级', trigger: 'change' }
  ]
}

// 控制弹窗显示
const dialogVisible = ref(false)

// 提交表单并关闭弹窗
const submitForm = () => {
  if (!formData.value.level) {
    ElMessage({
      type: 'warning',
      message: '请选择层级！',
    })
    return
  }
  // 这里可以添加额外的逻辑，比如提交到服务器
  getApi({ id: formData.value.id, level: formData.value.level }, `/mosty-base/sysUser/editUserLevel`).then(res => {
    ElMessage({
      type: 'success',
      message: '修改层级成功！',
    })
    dialogVisible.value = false
  })
}

// 关闭前处理
const handleClose = () => {
  // 可以在这里添加关闭前的处理逻辑
  dialogVisible.value = false
  emits('close')
}

// 公开方法：打开弹窗
const open = (obj) => {
  formData.value.level = obj.positionType
  formData.value.id = obj.id
  dialogVisible.value = true
}

// 将打开弹窗的方法暴露给父组件
defineExpose({ open })
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
