<template>
  <el-dialog
    class="page-dialog"
    :visible.sync="modalIsShow"
    title="预览"
    fullscreen
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @open="visibleChange"
    @close="dialogClose"
  >
    <div class="infinite-list-wrapper" style="overflow: auto">
      <ul class="list">
        <li v-for="url in imageList" :key="url" style="margin-bottom: 10px">
          <image-watermark :content="content" :url="url"></image-watermark>
        </li>
      </ul>
    </div>
    <div slot="footer"></div>
  </el-dialog>
</template>

<script>
import ImageWatermark from "./ImageWatermark.vue";
import { attachmentPreview } from "@/modules/login/api";

export default {
  name: "preview-attachment",
  components: { ImageWatermark },
  props: {
    attachmentIds: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      imageList: [],
      content: "",
    };
  },
  created() {
    const { loginName, realName } = this.$ls.get("user", {});
    this.content = `${realName}(${loginName})`;
  },
  methods: {
    visibleChange() {
      this.getAttachments();
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    dialogClose() {
      this.imageList = [];
    },
    async getAttachments() {
      if (!this.attachmentIds || !this.attachmentIds.length) return;
      const res = await attachmentPreview(this.attachmentIds);
      this.imageList = (res || []).map((item) => item.url);
    },
  },
};
</script>
