<!-- 供应商附件管理 -->
<script>
import { referFile, download } from "@/api/purchase/supAtttachment";
import useColumns from "./columns";
export default {
  name: "SupAtttachment",
  components: {
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
  },
  data() {
    const page = this.$init.page();
    const { TableColumns } = useColumns();
    return {
      page,
      size: "mini",
      TableColumns,
      loading: false,
      tableData: [],
    };
  },
  methods: {
    async getList(params) {
      try {
        this.loading = true;
        let { code, msg, rows, total } = await referFile(params);
        if (code == 200) {
          this.tableData = rows;
          this.page.total = total;
        }
      } catch (error) {
        console.log(error);
      } finally {
        this.loading = false;
      }
    },
    // 下载
    async useDownload(row) {
      try {
        this.$modal.loading("请稍候...");
        let formData = new FormData();
        formData.append("url", row.url);
        await download(formData).then(async (res) => {
          // var buffer = new ArrayBuffer(res);
          // var blob = new Blob([buffer], { type: "image/jpeg" });
          var blob = new Blob([res], {
            type: "image/jpeg",
          });
          const link = document.createElement("a");
          link.href = URL.createObjectURL(blob);
          link.download = row.name; // 这里填保存成的文件名
          link.click();
          URL.revokeObjectURL(link.href);
        });
      } catch (error) {
      } finally {
        this.$modal.closeLoading();
      }
    },
    useDbclick(row) {
      // window.open(row.url);
      let [url, isPreview] = row.url.split("?");
      let [isView, value] = isPreview.split("=");
      window.open(`${url}?${isView}=${false}`);
    },
    usePreview(row) {
      let [url, isPreview] = row.url.split("?");
      let [isView, value] = isPreview.split("=");
      window.open(`${url}?${isView}=${true}`);
    },
  },
  async created() {
    let { code } = this.$route.query;
    await this.getList({ ...this.page, code });
  },
};
</script>

<template>
  <el-card
    v-loading="loading"
    :body-style="{
      height: '100%',
      padding: 0,
      display: 'flex',
      'flex-direction': 'column',
    }"
  >
    <el-super-ux-table
      v-model="tableData"
      :size="size"
      :page="page"
      :columns="TableColumns"
      index
      pagination
      storage-key="SupAtttachmentSuperTable"
      @row-dblclick="useDbclick"
      @pagination="getList(page)"
    >
      <ux-table-column fixed="right" title="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="useDbclick(scope.row)" :size="size"
            >下载</el-button
          >
          <el-button type="text" @click="usePreview(scope.row)" :size="size"
            >预览</el-button
          >
        </template>
      </ux-table-column>
    </el-super-ux-table>
  </el-card>
</template>

<style scoped lang="scss">
.el-card {
  width: calc(100% - 32px);
  height: calc(100vh - 32px);
  margin: 16px;
  padding: 16px;
  border-radius: 8px;
}

.el-button-group + .el-button-group {
  margin: 0 0 0 8px;
}
</style>
