<template>
  <el-dialog
    v-model="visible"
    :title="type === 'add' ? '新增' : '修改'"
    :on-close="fnClose"
  >
    <el-form ref="formRef" :rules="rules" :model="form" label-width="110px">
      <el-form-item label="名称" prop="NAME">
        <el-input v-model="form.NAME" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="fnClose">关闭</el-button>
      <el-button type="primary" @click="fnSubmit">提交</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { useVModels } from "@vueuse/core";
import { ref } from "vue";
import { debounce } from "throttle-debounce";
import { ElMessage } from "element-plus";
import {
  setWorkforceManagementAdd,
  setWorkforceManagementEdit,
} from "@/request/workforce_management.js";
import useFormValidate from "@/assets/js/useFormValidate.js";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const rules = {
  NAME: [{ required: true, message: "请输入名称", trigger: "blur" }],
};
const formRef = ref(null);
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    const params = {
      ...form.value,
      ISCUSTOM: "0",
      LEVEL: "1",
      PARENTID: "0",
    };
    if (props.type === "add") await setWorkforceManagementAdd(params);
    else await setWorkforceManagementEdit(params);
    fnClose();
    ElMessage.success("操作成功");
    emits("get-data");
  },
  { atBegin: true }
);
</script>

<style scoped lang="scss"></style>
