<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="small"
          @click="uploadAccessory"
          >上传</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="accessoryList">
      <el-table-column
        type="index"
        label="序号"
        width="55"
        align="center"
      />
      <el-table-column label="名称" align="center" prop="fileName" />
      <el-table-column label="上传者" align="center" prop="createByName" />
      <el-table-column label="上传时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-row>
            <el-col :span="1.5">
              <el-button
                size="small"
                type="danger"
                plain
                icon="el-icon-delete"
                @click="deleteAccessory(scope.row.id)"
                >删除</el-button
              >
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-download"
                size="small"
                @click="downloadAccessory(scope.row)"
                >下载</el-button
              >
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <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=' + bo.id"
        :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">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

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

export default {
  name: "accessoryList",
  props:["source","bo","boAuthority"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      contactList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boId: null,
        name: null,
        code: null,
        customer: null,
        params:{},
      },
      // 上传参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 上传类型
        flag: "",
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/mk/bo/accessory/upload",
      },
    };
  },
  created() {
    this.queryParams.boId = this.bo.id;
    this.queryParams.params = {"post":this.boAuthority.post};
    this.getList();
  },
  methods: {
    /** 查询附件列表 */
    getList() {
      this.loading = true;
      listAccessory(this.queryParams).then(response => {
        this.accessoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //上传附件公共方法
    uploadAccessory() {
      this.upload.open = true;
    },
    // 提交上传文件
    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.getList();
    },
    //下载附件
    downloadAccessory(row) {
      console.log('row',row);
      var resUrl = "https://sy.derom.com/document-center/fastdfs/download?id=" + row.urlId;
      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", row.fileName);
          document.body.append(a);
          a.click();
          document.body.removeChild(a);
        });
    },
    //删除附件
    deleteAccessory(id) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {})
        .then(() => {
          delAccessory(id).then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess("删除成功");
              this.getList();
            } else {
              this.$modal.msgSuccess("删除失败");
            }
          });
        })
        .catch(() => {});
    },
  }
};
</script>
