<template>
  <el-dialog
    v-model="visible"
    :title="type === 'edit' ? '修改' : '新增'"
    :before-close="fnClose"
  >
    <el-form ref="formRef" :rules="rules" :model="form" label-width="150px">
      <el-form-item
        v-if="type === 'add'"
        label="职业危害告知书"
        prop="OCCUPATIONALNAME"
      >
        <el-input
          v-model="form.OCCUPATIONALNAME"
          placeholder="请输入职业危害告知书名称"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item
        v-if="type === 'edit'"
        label="经营企业"
        prop="OPERATINGCOMPANY"
      >
        <span>{{ operatingCompany }}</span>
      </el-form-item>
      <el-form-item label="职业危害告知书" prop="file">
        <layout-upload
          v-model:file-list="form.file"
          accept=".pdf"
          :limit="9"
          :before-upload="fnUpload"
        />
      </el-form-item>
      <el-form-item label="到期时间" prop="EXPIRYDATE">
        <el-date-picker
          v-model="form.EXPIRYDATE"
          type="date"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          placeholder="选择时间"
        />
      </el-form-item>
      <el-form-item label="备注" prop="MEETING_CONTENT">
        <el-input
          v-model="form.MEETING_CONTENT"
          type="textarea"
          placeholder="请输入内容"
          style="width: 300px"
        ></el-input>
      </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 { onMounted, ref, toRaw } from "vue";
import { useVModels } from "@vueuse/core";
import { ElMessage } from "element-plus";
import LayoutUpload from "@/components/upload/index.vue";
import {
  addOccupationalHazardsView,
  editOccupationalHazardsView,
  getSafetyOccupationalHazardsList,
} from "@/request/traffic_occupational_hazards.js";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: {},
    required: false,
    default: false,
  },
  loading: {
    type: Boolean,
    required: true,
    default: false,
  },
  options: {
    type: Array,
    required: true,
    default: () => [],
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const rules = {
  file: [
    { required: true, message: "请上传职业危害告知书", trigger: "change" },
  ],
  OCCUPATIONALNAME: [
    { required: true, message: "请输入职业危害告知书名称", trigger: "blur" },
  ],
};

const operatingCompany = ref("");
onMounted(async () => {
  const resData = await getSafetyOccupationalHazardsList();
  if (resData && resData.varList && resData.varList.length > 0) {
    operatingCompany.value = resData.varList[0].CORP_NAME;
  }
});
const formRef = ref(null);
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};

const fnSubmit = async () => {
  await formRef.value.validate();

  const rawFormValue = toRaw(form.value);

  const formData = new FormData();

  // 处理文件上传
  if (Array.isArray(rawFormValue.file)) {
    rawFormValue.file.forEach((fileItem) => {
      if (fileItem.raw) {
        formData.append("FFILE", fileItem.raw);
      }
    });
  }
  Object.keys(rawFormValue).forEach((key) => {
    if (key !== "file") {
      formData.append(key, rawFormValue[key]);
    }
  });
  // 执行添加或编辑操作
  if (props.type === "add") {
    formData.append("USER_ID", rawFormValue.USER_ID);
    await addOccupationalHazardsView(formData);
  } else {
    formData.append(
      "OCCUPATIONALHAZARDS_ID",
      rawFormValue.OCCUPATIONALHAZARDS_ID
    );
    await editOccupationalHazardsView(formData);
  }

  // 操作成功提示
  ElMessage.success("操作成功");

  // 关闭表单对话框
  fnClose();

  // 触发更新数据的事件
  emits("get-data");
};

const fnUpload = (file) => {
  const isLt100M = file.size / 1024 / 1024 < 100;
  if (!isLt100M) {
    ElMessage.error("文件大小不能超过100M");
    return false;
  }
  return true;
};
</script>

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