<template>
  <el-dialog
    :visible.sync="visible"
    title="文件上传"
    width="50%"
    @close="handleClose"
    :append-to-body="true"
    custom-class="inner-dialog"
  >
    <!-- 文件上传组件 -->
    <el-upload
      class="upload-container"
      ref="upload"
      :action="url"
      :headers="headers"
      :data="uploadData"
      :before-upload="beforeUpload"
      :on-success="handleSuccess"
      :on-error="handleError"
      :auto-upload="false"
      :limit="1"
      :file-list="fileList"
      :drag="true"
      :show-file-list="true"
      @upload-success="handleFileUploadSuccess" 
      @upload-error="handleFileUploadError"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        将文件拖到此处，或
        <em>点击上传</em>
      </div>
      <div class="el-upload__tip" slot="tip">只能上传Excel文件（.xls, .xlsx），且不超过2MB</div>
    </el-upload>

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";
export default {
  name: "FileUploadDialog",
  props: {
    // 控制弹窗的显示和隐藏
    value: {
      type: Boolean,
      default: false
    },
    // 上传接口
    uploadUrl: {
      type: String,
      required: true
    },
    // 父组件传递的文件列表（如果需要）
    fileList: {
      type: Array,
      default: () => []
    },
    // 父组件传递的回调方法，用来处理文件上传成功后的逻辑
    onSuccess: {
      type: Function,
      default: () => {}
    },
    onError: {
      type: Function,
      default: () => {}
    },
    accessOnlyId:{

    }
  },
  watch: {
    value(newVal) {
      this.visible = newVal;
    },
    visible(newVal) {
      this.$emit("input", newVal);
    },
    uploadUrl: {
      handler(newVal) {
        this.url = newVal;
      },
      deep: true, // 深入监听
      immediate: true
    },
    accessOnlyId: {
      handler(newVal) {
        this.onlyId = newVal;
        this.uploadData.outsideId = newVal;
        console.log( this.uploadData.accessOnlyId,' this.uploadData.accessOnlyId');
        
      },
      deep: true, // 深入监听
      immediate: true
    }
  },
  data() {
    return {
      onlyId:'',
      headers: {
        Authorization: "Bearer " + getToken(),
        // 'Content-Type': "multipart/form-data"
      },
      uploadData: {
        systemType: "接入反馈表", // 附加参数
        outsideId:'',
      },
      loading: false, // 提交按钮的加载状态
      visible: this.value,
      file: null,
      url: ""
    };
  },
  mounted () {
    this.fileList=[]
  },
  methods: {
    // 上传前处理
    beforeUpload(file) {
      console.log("上传前处理");

      console.log(file, "file");
      // // 可以在这里做一些文件格式、大小的校验
      // const isPdf = file.type === "application/pdf";
      // if (!isPdf) {
      //   this.$message.error("只能上传PDF文件");
      // }
      // return isPdf; // 如果返回 false，文件将不会被上传
    },
    // 上传成功处理
    handleSuccess(res, file, fileList) {
      console.log("上传成功处理");
      console.log(file, "file");
      // this.file = file;
      // 判断上传结果的状态码
      if (res.code === 200) {
        // 上传成功，调用父组件传递的 onSuccess 方法
        this.onSuccess(res, file, fileList);
        this.$modal.msgSuccess("上传成功！");
      } else {
        // 上传失败，提示错误
        this.$modal.msgError(res.msg || "上传失败，请重试");
      }
    },
    // 上传失败处理
    handleError(error, file, fileList) {
      this.$modal.msgError(error || "上传文件失败，请重试");
    },
    // 提交上传
    submitUpload() {
      this.loading = true;
      this.$refs.upload.submit(); // 提交上传
    },
    // 关闭弹窗时清空文件列表
    handleClose() {
      // this.fileList = []; // 清空文件列表
      this.visible = false;
      this.$emit("before-close");
    },

    // 取消按钮处理方法
    handleCancel() {
      this.visible = false;
      this.$emit("confirm"); // 可以在父组件监听 'cancel' 事件
    },

    // 确定按钮处理方法
    handleConfirm(response) {
      console.log(response, "response");

      this.$refs.upload.submit();
      // this.$modal.msgSuccess(response.msg);
      this.handleCancel();
    }
  }
};
</script>

<style scoped lang="scss">
.el-upload__tip {
  color: #888;
  font-size: 14px;
  text-align: center;
  margin-top: 10px;
}
.inner-dialog {
  z-index: 2002 !important; /* 设置一个比外层更高的值 */
}
</style>
