<template>
  <div>
    <el-form :model="form">
      <el-form-item :label="name">
        <el-row>
          <el-col :span="1.5">
            <el-input v-model="fileName" placeholder="待上传" readonly />
          </el-col>
          <el-col :span="1.5" v-show="!fileUrlid">
            <el-button type="info" plain icon="el-icon-upload2" size="small" @click="uploadAccessory(field)">
              上传
            </el-button>
          </el-col>
          <el-col :span="1.5" v-show="fileUrlid" >
            <el-button type="success" plain icon="el-icon-download" size="small" @click="exportAccessory">
              下载
            </el-button>
          </el-col>
          <el-col :span="1.5" v-show="fileUrlid">
            <el-button size="small" type="danger" plain icon="el-icon-delete" @click="deleteAccessory(field)">
              删除
            </el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <!-- 上传对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls, .doc, .docx, .word, .wordx, .png, .jpg, .gif, .txt"
        :headers="upload.headers"
        :action="upload.url + '?boId=' + form.id + '&flag=' + upload.flag + '&boType=' + form.boType"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {delAccessory} from "@/api/business/spd/bo/basic";
import axios from "axios";
import { getToken } from "@/utils/auth";

export default {
  name: "filemanager",
  props: ["form","name","fileName","fileUrlid","field"],
  data() {
    return {
      // 上传参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 上传类型
        flag: "",
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/mk/bo/basic/upload",
      },
    };
  },
  created() {
  },
  methods: {
    //上传附件公共方法
    uploadAccessory(f) {
      // this.upload.title = "上传附件";
      this.upload.open = true;
      this.upload.flag = f;
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "上传结果",
        { dangerouslyUseHTMLString: true }
      );
      this.$emit('reload');
    },
    //下载附件
    exportAccessory() {
      let resUrl = "https://sy.derom.com/document-center/fastdfs/download?id=" + this.fileUrlid;
      axios
        .create({
          timeout: 3000,
          responseType: "blob", // 响应类型, 将响应数据转换为二进制数据
          headers: {},
        })
        .get(resUrl)
        .then((res) => {
          console.log(res);
          // 地址转换
          let url = window.URL.createObjectURL(res.data);
          const a = document.createElement("a");
          a.setAttribute("href", url);
          a.setAttribute("download", this.fileName);
          document.body.append(a);
          a.click();
          document.body.removeChild(a);
        });
    },
    //删除附件
    deleteAccessory(f) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {})
        .then(() => {
          delAccessory(this.form.id, f, this.form.boType,this.fileUrlid).then((res) => {
            console.log('删除返回',res);
            this.$emit('reload');
          });
        })
        .catch(() => {});
    },
  },
};
</script>
