<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 label="风险点（单元）" prop="RISK_UNIT_ID">
        <el-select v-model="form.RISK_UNIT_ID">
          <el-option
            v-for="item in unitList"
            :key="item.RISKUNIT_ID"
            :label="item.DEPT_NAME + '-' + item.RISKUNITNAME"
            :value="item.RISKUNIT_ID"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="辨识部位名称" prop="PARTSNAME">
        <el-input v-model="form.PARTSNAME" />
      </el-form-item>
      <el-form-item label="告知卡" prop="file">
        <layout-upload
          v-model:file-list="form.file"
          accept=".jpg,.jpeg,.png"
          list-type="picture-card"
          delete-to-server
          :limit="99"
        />
      </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 { ref } from "vue";
import { useVModels } from "@vueuse/core";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { ElMessage } from "element-plus";
import {
  getRiskPointListAll,
  setIdentifyingPartsAdd,
  setIdentifyingPartsEdit,
} from "@/request/risk_control.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: "",
  },
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const rules = {
  RISK_UNIT_ID: [
    { required: true, message: "风险点（单元）不能为空", trigger: "change" },
  ],
  PARTSNAME: [
    { required: true, message: "辨识部位名称不能为空", trigger: "blur" },
  ],
};
const formRef = ref(null);
const unitList = ref([]);
const fnGetUnitList = async () => {
  const resData = await getRiskPointListAll();
  unitList.value = resData.unitList;
};
fnGetUnitList();
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    const formData = new FormData();
    for (let i = 0; i < form.value.file.length; i++) {
      if (form.value.file[i].raw) {
        formData.append("FFILE", form.value.file[i].raw);
      }
    }
    Object.keys(form.value).forEach((key) => {
      formData.append(key, form.value[key]);
    });
    formData.delete("file");
    props.type === "add"
      ? await setIdentifyingPartsAdd(formData)
      : await setIdentifyingPartsEdit(formData);
    ElMessage.success("操作成功");
    fnClose();
    emits("get-data");
  },
  { atBegin: true }
);
</script>

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