<template>
  <el-dialog v-model="visible" title="检查照片" :on-close="fnClose">
    <el-form ref="formRef" :model="form" label-width="100px">
      <el-row>
        <el-col v-if="writeresults" :span="24">
          <el-form-item label="检查结果" prop="CHECK_RESULT">
            <el-input
              v-model="form.CHECK_RESULT"
              placeholder="请输入检查结果"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="检查照片" prop="file">
            <layout-upload
              v-model:file-list="form.file"
              :limit="99"
              list-type="picture-card"
              accept=".jpg,.jpeg,.png"
              delete-to-server
            />
          </el-form-item>
        </el-col>
      </el-row>
    </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 LayoutUpload from "@/components/upload/index.vue";
import { useVModels } from "@vueuse/core";
import { debounce } from "throttle-debounce";
import { setUploadImg } from "@/request/api.js";
import { ElMessage } from "element-plus";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
  id: {
    type: String,
    required: true,
    default: "",
  },
  writeresults: {
    type: Boolean,
    required: false,
    default: false,
  },
  index: {
    type: Number,
    required: false,
    default: 0,
  },
});
const emits = defineEmits(["update:visible", "update:form", "submit"]);
const { visible, form } = useVModels(props, emits);
const formRef = ref(null);
const fnClose = () => {
  formRef.value.resetFields();
  form.value.CHECK_RESULT = "";
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    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);
    }
    formData.append("FOREIGN_KEY", props.id);
    formData.append("TYPE", 14);
    await setUploadImg(formData);
    emits("submit", form.value.CHECK_RESULT, props.index);
    fnClose();
    ElMessage.success("保存成功");
  },
  { atBegin: true }
);
</script>

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