<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="COMMITMENTNAME"
      >
        <el-input
          v-model="form.COMMITMENTNAME"
          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="选择时间"
          style="width: 200px"
        />
      </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 {
  addCommitmentView,
  editCommitmentView,
  getSafetyCommitmentList,
} from "@/request/traffic_safety_commitment.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" },
  ],
  COMMITMENTNAME: [
    { required: true, message: "请输入承诺书名称", trigger: "blur" },
  ],
};

const operatingCompany = ref("");
onMounted(async () => {
  const resData = await getSafetyCommitmentList();
  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 addCommitmentView(formData);
  } else {
    formData.append(
      "SECURITYCOMMITMENT_ID",
      rawFormValue.SECURITYCOMMITMENT_ID
    );
    await editCommitmentView(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>
