<template>
  <transition
    name="dialog-fade"
    @after-enter="afterEnter"
    @after-leave="afterLeave"
  >
    <div :key="key" v-show="visible" class="aegle-preview-wrapper">
      <div class="aegle-preview">
        <div class="aegle-preview-left">
          <div class="aegle-preview-top">
            <slot name="title">
              <span class="aegle-preview-title">{{ title }}</span>
            </slot>
          </div>
          <el-scrollbar tag="div" class="aegle-preview-bottom">
            <div
              class="filename-item"
              @click="changeIndex(index)"
              :class="{ active: currentUrl == index }"
              v-for="(item, index) in list"
              :key="index"
            >
              <i class="el-icon-tickets"></i><span>{{ item.label }}</span>
            </div>
          </el-scrollbar>
        </div>
        <div class="aegle-preview-right">
          <!-- 加载动画 -->
          <!-- <div v-show="showLoading && list[0] && list[0].isSucceed" class="preview-loading">
            <el-progress :text-inside="true" :stroke-width="12" :percentage="percentage"></el-progress>
            <span>预览报告中...</span>
          </div> -->

          <!-- <div v-show="!list[0]['isSucceed']" class="preview-loading-fail">
            <img :src="require('@/assets/images/loadFail.png')" alt="">
            <span>预览报告失败，请重试！</span>
            <div>
              <el-button
                class="cancel"
                size="small"
                plain
                @click="preview()"
                >取消</el-button
              >
              <el-button
                class="cancel"
                size="small"
                type="primary"
                @click="preview()"
                >重试</el-button
              >
            </div>
          </div> -->
          
          <el-scrollbar tag="div" style="height: 100%" ref="myScrollbar">
            <div
              style="
                display: flex;
                flex-direction: column;
                align-items: center;
                padding-top: 25px;
              "
            >
              <pdf
                style="
                  max-width: 80%;
                  min-width: 1000px;
                  display: inline-block;
                  margin-bottom: 30px;
                "
                v-for="i in numPages"
                :key="i"
                :src="laoclUrl"
                :page="i"
              ></pdf>
            </div>
          </el-scrollbar>
        </div>
        <div class="aegle-preview-button">
          <span class="close-icon" @click="handleClose">
            <i class="el-icon-close"></i>
          </span>
          <span class="aegle-preview-button-bottom">
            <template name='btnBottom'></template>
            <div><slot /></div>
          </span>
          
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
import pdf from "vue-pdf";
export default {
  name: "AeglePreview",
  props: {
    title: {
      type: String,
      default: "病例资料",
    },
    visible: {
      type: Boolean,
      default: false,
    },
    destroyOnClose: {
      type: Boolean,
      default: true,
    },
    list: {
      type: Array,
      default: [],
    },
    loading: {
      type: Boolean,
      default: false,
    }
  },
  components: { pdf },
  data() {
    return {
      closed: false,
      key: 0,
      currentUrl: "",
      imageUrl: [],
      numPages: 0,
      laoclUrl: "",
      showLoading: false,
      percentage: 0,
      timer: null
    };
  },
  watch: {
    visible(val) {
      if (val) {
        this.showLoading = this.loading;
        this.$nextTick(() => {
          this.$refs.myScrollbar.wrap.scrollTop = 0;
        });
        document.body.appendChild(this.$el);
        if (this.list.length) {
          this.currentUrl = 0;
          this.pdfToPng(this.list[0].url);
        }
      } else {
        clearInterval(this.timer);
        this.showLoading = true;
        this.percentage = 0;
        if (this.destroyOnClose) {
          this.$nextTick(() => {
            this.key++;
          });
        }
      }
    },
    currentUrl(v) {
      if (v !== null && v !== undefined) {
        this.$refs.myScrollbar.wrap.scrollTop = 0;
      }
    }, 
    list: {
      handler(val) {
        // if(!this.showLoading) return
        // if(!val[0].url) {
        //   this.percentage = 0;
        //   this.setTime(1);
        // } else {
        //   this.showLoading = false;
        //   this.percentage < 80 && (this.percentage = 80);
        //   this.setTime(2);
        // }
      },
      deep: true
    }
  },
  mounted() {
    if (this.visible) {
      document.body.appendChild(this.$el);
    }
    // if(this.list[0]?.url){
    //   this.pdfToPng(this.list[0]?.url)
    // }
  },
  methods: {
    afterEnter() {
      this.$emit("opened");
    },
    afterLeave() {
      this.$emit("closed");
    },
    updatePopper() {
      this.broadcast("ElSelectDropdown", "updatePopper");
      this.broadcast("ElDropdownMenu", "updatePopper");
    },
    handleClose() {
      this.$emit("update:visible", false);
    },
    pdfToPng(url) {
      this.$nextTick(() => {
				const loadTask = pdf.createLoadingTask(url);
				loadTask.promise.then((_pdf) => {
					this.numPages = _pdf.numPages;
					this.laoclUrl = loadTask;
				});
			});
      // const loadTask = pdf.createLoadingTask(url);
      // loadTask.promise.then((_pdf) => {
      //   this.numPages = _pdf.numPages;
      //   this.laoclUrl = loadTask;
      // });
    },
		changeIndex(index) {
			this.currentUrl = index;
			this.pdfToPng(this.list[index].url)
		},
    setTime(type) {
      let _this = this;
      clearInterval(this.timer);
      if(type==1) {
        this.timer = setInterval(()=> {
          this.percentage = this.percentage + Math.round(Math.random()*10);
          if(this.percentage >= 80) clearInterval(this.timer);
        },1000);
      } 
      if(type==2) {
        this.timer = setInterval(()=> {
          this.percentage = this.percentage + Math.round(Math.random()*10);
          if(this.percentage >= 100) {
            debugger
            this.percentage = 100;
            clearInterval(_this.timer);
            this.loading = false;
            this.changeIndex(0);
          }
        },500);
      }
    }
  },
  destroyed() {
    if (this.$el && this.$el.parentNode) {
      this.$el.parentNode.removeChild(this.$el);
    }
  },
};
</script>
<style lang="less" scoped>
.aegle-preview-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  z-index: 999;
}
.aegle-preview {
  height: 100%;
  display: flex;

  &-left {
    flex: 0 0 200px;
    height: 100%;
    padding: 20px;

    .aegle-preview-top {
      height: 45px;
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: rgba(0, 0, 0, 0.85);
      line-height: 22px;
      padding-bottom: 20px;
    }
    .aegle-preview-bottom {
      height: calc(100% - 45px);
      /deep/ .el-scrollbar__wrap {
        overflow-x: hidden;
      }
    }
    .filename-item {
      display: flex;
      align-items: flex-start;
      color: rgba(0, 0, 0, 0.4);
      height: 30px;
      width: 160px;
      cursor: pointer;
      i {
        font-size: 13px;
        padding-top: 2px;
      }
      span {
        padding-left: 4px;
        font-size: 12px;
        font-family: PingFangSC, PingFangSC-Semibold;
        font-weight: 600;
        text-align: left;
        line-height: 17px;
        letter-spacing: 0.18px;
        flex: 1;
        display: -webkit-box;
        word-break: break-all;
        text-overflow: ellipsis;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        // white-space: nowrap;
        overflow: hidden;
      }
      &.active span {
        color: #1890ff;
      }
    }
  }
  &-right {
    flex: 1 1 auto;
    width: 100%;
    height: 100%;
    background-color: rgba(216, 216, 216, 0.3);
    /deep/ .el-scrollbar__wrap {
      overflow-x: hidden;
    }
  }
  &-button {
    width: 85px;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 28px 0;
    > span {
      position: absolute;
      right: 22px;
      top: 20px;
      font-size: 32px;
      cursor: pointer;
      z-index: 1000;
      color: rgba(0, 0, 0, 0.6);
      &:hover {
        color: #1890ff;
      }
    }
  }
  .aegle-preview-button-bottom {
    position: absolute;
    top: auto;
    bottom: 32px;
  }
      .preview-loading {
      height: 100%;
      width: 100%;
      // display: flex;
      /deep/ .el-progress{
        position: relative;
        top: 50%;
        left: 50%;
        margin-top: -32px;
        margin-left: -80px;
        width: 160px;
        .el-progress-bar__outer {
          background-color: rgba(0,0,0,0.06);
        }
        .el-progress-bar__innerText {
          display: none;
        }
      }
      span {
        width: 100%;
        // height: 20px;
        font-size: 14px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        color: rgba(0,0,0,0.40);
        line-height: 20px;
        display: inline-block;
        text-align: center;
        position: relative;
        left: 0;
        top: 50%;
        margin-top: 20px;
      }
    }
  .preview-loading-fail {
      position: absolute;
      top: 50%;
      left: 50%;
      display: flex;
      flex-direction: column;
      align-items: center;
      z-index: 10;
      span {
        margin: 10px 0 20px 0;
      }
      img {
        width: 80px;
        height: 80px;
      }

  }
}
</style>