
<template>
  <div class="base_form">
    <div class="label">
      <img class="pre-tag" src="../../../assets/images/template/pre-tag.png" />
      <div class="label-text" :title="label">{{ label }}</div>
    </div>
    <!-- 显示区域 -->
    <div>
      <div class="up" @click="upFile">
        <i class="iconfont iconpdf1"></i>
        上传PDF
      </div>
      <div class="show" v-for="(item, index) in localObjects" :key="index">
        <i class="iconfont iconPDF"></i>
        <p @click="openPDF(item)">{{ item.name }}</p>
        <div class="show-rigth">
          <i class="iconfont iconxingzhuang2"></i>上传成功
          <i class="iconfont iconshanchu1" @click="deletePDF(index)"></i>
        </div>
      </div>
    </div>
    <input
      type="file"
      hidden
      ref="inputDom"
      accept="application/pdf"
      @change="fileChange"
    />
  </div>
</template>
<script>
import { GO_URLS } from "@/config/URLS";
export default {
  data() {
    return {
      label: "", // 标题
      pdfURL: "",
      pdfSHOW: false,
      localObjects: [], // 本地展示用
      localHash: [], // 本地hash
    };
  },
  props: ["property"],
  mounted() {
    //   初始化标题
    this.label = this.property.label;
    // 数据回显
    if (this.property.value.length > 0) {
      this.pdfSHOW = true;
      let hashes = [];
      for (let index = 0; index < this.property.value.length; index++) {
        const element = this.property.value[index];
        hashes.push(element.value);
      }
      if (hashes.length === this.property.value.length) {
        this.getPDF(hashes);
      }
    }
  },
  watch: {
    property: {
      //深度监听
      handler(val, oldVal) {
        //   标题修改后对应的修改
        this.label = val.label;
      },
      deep: true,
    },
    localHash: {
      handler(newV, oldV) {
        this.property.value = newV;
        if (newV.length > 0) {
          this.pdfSHOW = true;
        }
      },
      deep: true,
    },
  },
  methods: {
    upFile() {
      this.$refs.inputDom.click();
    },
    async fileChange(e) {
      const file = e.target.files[0];
      if (!file) return;
      if (file.size > 10485760) {
        this.$message({
          message: "上传文件超过最大限制",
          type: "warning",
        });
        return;
      }
      const formData = new FormData();
      formData.append("file_data", file);
      const res = await this.$ajax({
        type: "post",
        url: GO_URLS.upload + `?file_type=pdf`,
        params: formData,
      });
      if (res) {
        // 上传成功
        this.$message({
          message: "上传成功",
          type: "success",
        });
        this.localHash.push(res.data.hash);
        this.pdfSHOW = true;
        this.localObjects.push(res.data);
      }
    },
    // 获取pdf文件
    async getPDF(hashes) {
      const data = await this.$ajax({
        type: "post",
        params: { hashes },
        url: GO_URLS.getFiles,
      });
      if (data) {
        this.localObjects = data.data.files;
        for (let index = 0; index < data.data.files.length; index++) {
          const element = data.data.files[index];
          this.localHash.push(element.hash);
        }
      }
    },
    // 打开pdf文件
    openPDF(item) {
      window.open(item.url);
    },
    // 删除pdf
    deletePDF(index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.localHash.splice(index, 1);
          this.localObjects.splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="less" scoped>
.base_form {
  flex-shrink: 0;
  padding-left: 18px;
  font-size: 0;
  padding-top: 10px;
  padding-bottom: 10px;
  display: flex;
  justify-content: flex-start;
}
.label {
  display: inline-block;
  width: 150px;
  flex-shrink: 0;
  cursor: pointer;
  color: #797d84;
  line-height: 44px;
  font-size: 14px;
  font-weight: 300;
  position: relative;
}
.pre-tag {
  position: absolute;
  left: -30px;
  top: 14px;
  display: none;
}
.label-text {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
.upload-area {
  width: 151px;
  height: 89px;
  border-radius: 4px;
  border: 1px dashed #e5e5e5;
  text-align: center;
  padding-top: 10px;
  cursor: pointer;
  .icon {
    font-size: 32px;
    color: #797d84;
  }
  p {
    margin: 0;
    padding: 0;
    padding-top: 8px;
    font-size: 12px;
    font-weight: 400;
    color: rgba(121, 125, 132, 1);
    &:last-child {
      font-size: 10px;
      padding-top: 2px;
      color: rgba(152, 159, 165, 1);
    }
  }
}
.show {
  line-height: 1;
  cursor: pointer;
  padding-top: 20px;
  padding-bottom: 4px;
  width: 800px;
  p {
    font-size: 14px;
    margin: 0;
    display: inline-block;
    padding: 0;
    font-weight: 400;
    color: #353535;
  }
  .iconPDF {
    color: #fe572f;
    font-size: 16px;
    margin-right: 6px;
  }
  .show-rigth {
    float: right;
    font-size: 14px;
    .iconshanchu1 {
      color: #b6b5ba;
      font-size: 12px;
      margin-left: 10px;
    }
    .iconxingzhuang2 {
      margin-right: 2px;
      color: #33c724;
    }
  }
}
.up {
  width: 151px;
  height: 36px;
  cursor: pointer;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 400;
  text-align: center;
  line-height: 36px;
  color: rgba(121, 125, 132, 1);
  background: #f6f6f7;
  border: 1px solid #f0f1f5;

  .icon {
    font-size: 14px;
    padding-right: 6px;
  }
}
</style>