<template>
  <el-dialog
    v-model="visible"
    :title="type === 'add' ? '新增' : '修改'"
    :before-close="fnClose"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="所属部门">
        <el-tag>{{ departmentName }}</el-tag>
      </el-form-item>
      <el-form-item label="岗位名称" prop="NAME">
        <el-input v-model="form.NAME" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="岗位职责" prop="DESCR">
        <el-input
          :autosize="{
            minRows: 3,
          }"
          v-model="form.DESCR"
          type="textarea"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="状态" prop="STATUS">
        <el-radio-group v-model="form.STATUS">
          <el-radio label="0">启用</el-radio>
          <el-radio label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="告知卡" prop="files">
        <layout-upload
          v-model:file-list="form.files"
          :limit="99"
          accept=".jpeg,.png,.jpg"
          delete-to-server
          list-type="picture-card"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="fnSubmit">确认</el-button>
      <el-button @click="fnClose">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { useVModels } from "@vueuse/core";
import { ref } from "vue";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { setPostAdd, setPostEdit } from "@/request/enterprise_management.js";
import LayoutUpload from "@/components/upload/index.vue";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
  departmentName: {
    type: String,
    required: true,
    default: "",
  },
  departmentId: {
    type: String,
    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" }],
  DESCR: [{ 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 formData = new FormData();
    Object.keys(form.value).forEach((key) => {
      formData.append(key, form.value[key]);
    });
    formData.delete("files");
    for (let i = 0; i < form.value.files.length; i++) {
      if (form.value.files[i].raw)
        formData.append("FFILE", form.value.files[i].raw);
    }
    if (props.type === "add") {
      formData.append("DEPARTMENT_ID", props.departmentId);
      await setPostAdd(formData);
    } else await setPostEdit(formData);
    fnClose();
    emits("get-data");
  },
  { atBegin: true }
);
</script>

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