<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
      > -->
      <el-button
        type="primary"
        :loading="ensureLoading"
        :disabled="isUploadShow !== -1"
        @click="handelConfirm"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

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

export default {
  components: {
    FileUpload,
  },
  props: {
    processNo: {
      type: String,
      default: "",
    },
    headerForm: { type: Object, default: () => ({}) }, // 表单头
    rowData: { type: Object, default: () => ({}) }, // 行数据
  },
  data() {
    return {
      ensureLoading: false,
      // iqc->在表头传
      wmsIncomingReceiptInspectionId: undefined,
      // ipqc -> 表体的每一条
      wmsMaterialItemResultId: undefined,
      // 文件列表
      fileList: [],
      form: {},
      isUploadShow: "",
      selectRows: [],
      rules: {},
      visible: false,
    };
  },
  methods: {
    onOpen() {
      this.$nextTick(() => {
        if (this.rowData && this.rowData.id) {
          const id = this.rowData.id;
          getIqcInspectionDataDetailForPda({ id: id }).then((res) => {
            console.log("打印一下返回的数据", res);
            if (res.code === 0 && res.data) {
              const urlList = res.data.urlList || [];
              const fileIds = res.data.fileIds || [];

              this.$refs.fileUpload.fileList = urlList.map((url, index) => {
                // 清理URL中的额外空格和括号
                const cleanUrl = url.trim().replace(/[`]/g, "");
                // 从URL中提取文件名
                const lastSlashIndex = cleanUrl.lastIndexOf("/");
                const fileName =
                  lastSlashIndex !== -1
                    ? cleanUrl.substring(lastSlashIndex + 1)
                    : cleanUrl;
                return {
                  id: fileIds[index] || "", // 关键修复：直接设置 id 字段
                  name: { fileId: fileIds[index] || "" }, // 兼容 handleDelete 中的 name.fileId
                  sysName: fileName,
                  url: cleanUrl, // url 直接存字符串，不包对象
                };
              });
            }
          });
        } else {
          this.$refs.fileUpload.fileList = this.fileList;
        }
        this.$refs.fileUpload.isUploadShow = this.isUploadShow;
      });
    },
    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 actualCreateReqVOS = [
        // { id: "", actual: this.rowData.actual1 },
        // { id: "", actual: this.rowData.actual2 },
        // { id: "", actual: this.rowData.actual3 },
        // { id: "", actual: this.rowData.actual4 },
        // { id: "", actual: this.rowData.actual5 },
      ];

      const params = {
        actualCreateReqVOS,
        fileIds: idList,
        processNo: this.processNo || null,
        inspectionModel: this.rowData.inspectionModel,
        singleResult: this.rowData.singleResultNum || "",
        measuredResults: this.rowData.measuredResults || "",
        materialLots: this.headerForm.materialLots || "",
        id: this.rowData.id,
        pcFlag: 1,
      };
      createAttachmentUpload(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>
