<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="visible"
    title="文件上传"
    append-to-body
    width="800px"
    @open="onOpen"
  >
    <FileUpload ref="fileUpload" />
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button
        v-hasPermi="['wms:ipqc-result-file:create']"
        type="primary"
        :loading="ensureLoading"
        :disabled="isUploadShow !== -1"
        @click="handelConfirm"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue";
import { createFileUpload } from "@/api/wms/quality/iqcInspectionExecute";

export default {
  components: {
    FileUpload,
  },
  props: {
    processNo: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      ensureLoading: false,
      // iqc->在表头传
      wmsIncomingReceiptInspectionId: undefined,
      // ipqc -> 表体的每一条
      wmsMaterialItemResultId: undefined,
      // 文件列表
      fileList: [],
      form: {},
      isUploadShow: "",
      selectRows: [],
      rules: {},
      visible: false,
    };
  },
  methods: {
    // onOpen() {
    //   this.$nextTick(() => {
    //     this.$refs.fileUpload.isUploadShow = this.isUploadShow
    //     this.$refs.fileUpload.fileList = this.fileList
    //   })
    // },
    onOpen() {
      this.$nextTick(() => {
        // 如果外部已经把 fileList 传进来，就不再调接口
        if (this.fileList && this.fileList.length) {
          this.$refs.fileUpload.fileList = this.fileList;
          this.$refs.fileUpload.isUploadShow = this.isUploadShow;
          return;
        }
        // 否则保持原有逻辑（兼容旧场景）
        this.$refs.fileUpload.isUploadShow = this.isUploadShow;
        this.$refs.fileUpload.fileList = this.fileList;
      });
    },
    close() {
      this.reset();
      this.visible = false;
    },
    reset() {
      this.form = {
        id: undefined,
        inspectionItemId: undefined,
        inspectionNo: undefined,
      };
      this.resetForm("form");
    },
    handelConfirm() {
      this.fileList = this.$refs.fileUpload.fileList;
      const idList = this.fileList.map((item) => {
        const { url, id } = item;
        return url.fileId === undefined ? id : url.fileId;
      });
      const params = {
        wmsIncomingReceiptInspectionId: this.wmsIncomingReceiptInspectionId,
        wmsMaterialItemResultId: this.wmsMaterialItemResultId,
        fileIds: idList,
        processNo: this.processNo || null,
      };
      createFileUpload(params).then((res) => {
        this.$modal.msgSuccess("上传成功!");
        this.visible = false;
        this.fileList = [];
        this.$refs.fileUpload.fileList = res.data;
        this.$emit("uploadComplete");
      });
    },
    queryDetails() {},
    badCodeChange(v) {
      console.log(v);
    },
  },
};
</script>
<style></style>
