<template>
  <div class="app-container">
    <div v-if="fileList.length > 0">
      <el-card
        class="box-card public-margin-b-20"
        v-for="(item, index) in fileList"
        :key="index"
      >
        <div slot="header" class="clearfix">
          <span>{{ item.uploadTypeName }}</span>
        </div>
        <div
          class="public-flex-between"
          v-for="(items, indexs) in item.children"
          :key="indexs"
        >
          <div class="public-flex">
            <div class="public-margin-r-10">{{ items.createBy }}: </div>
            <div
              class="fileItems"
              title="点击下载"
              @click="handlePreview(items)"
            >
              {{ items.newUploadName }}
            </div>
          </div>
          <span class="public-font-size-12">{{ items.createTime }}</span>
        </div>
      </el-card>
    </div>
    <div
      v-else
      class="public-flex-center public-padded-20"
      style="color: #909399"
    >
      暂无数据
    </div>
  </div>
</template>

<script>
// FileType：
// 【0：其他】
// 【1：跟进记录】
// 【2：研究资料】
// 【3：文件资料】
// 【4：会议记录】
// 【5：渠道信息】
// 【6：合同管理】
// 【7：公司信息】
// 【8：尽职调查】
import { listFileId, downloadFileById } from "@/api/system/file";
import fileItem from "./fileItem";
export default {
  props: {
    projectId: {
      type: String,
    },
  },
  components: { fileItem },
  data() {
    return {
      fileList: [],
      nameList: [
        "其他",
        "跟进记录",
        "研究资料",
        "文件资料",
        "会议记录",
        "渠道信息",
        "合同管理",
        "公司信息",
        "尽职调查",
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
      downloadFileById(file.id).then((response) => {
        const url = window.URL.createObjectURL(new Blob([response]));
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", file.newUploadName);
        document.body.appendChild(link);
        link.click();
      });
    },
    getList() {
      let fileData;
      listFileId(this.projectId)
        .then((response) => {
          fileData = response.data;
        })
        .then(() => {
          this.fileList = this.handleList(fileData);
          // console.log(this.fileList);
        });
    },
    handleList(arr) {
      let level1 = "uploadType"; //获取一级属性名称
      let list = Array.from(
        new Set(
          arr.map((item) => {
            return item[level1];
          })
        )
      );
      let subList = [];
      list.forEach((res) => {
        arr.forEach((ele) => {
          if (ele[level1] === res) {
            let nameArr = subList.map((item) => item[level1]);
            if (nameArr.indexOf(res) !== -1) {
              subList[nameArr.indexOf(res)].children.push({
                ...ele,
              });
            } else {
              let resIndex = parseInt(res);
              subList.push({
                uploadType: res,
                uploadTypeName: this.nameList[resIndex],
                children: [
                  {
                    ...ele,
                  },
                ],
              });
            }
          }
        });
      });
      return subList;
    },
  },
};
</script>
<style lang="scss" scoped>
.fileItems {
  cursor: pointer;
  color: #2440b3;
  text-decoration: underline;
  margin: 8px 0;
}
</style>
