<template>
  <div class="param-container" style="padding: 5vw 10vw;background-color: #fff">
    <h2 class="title1" style="line-height: 40px">资料下载</h2>
    <div>

      <div v-for="i in data.fileList" class="fileCard" style="text-align: left">
        <div class="cardTitle">{{ i.name }}</div>
        <div class="cardValue">{{ i.value }}</div>
        <el-button @click="downFile( i)" type="primary" class="downIcon" icon="el-icon-download "
                   circle></el-button>
      </div>
    </div>

    <el-dialog title="输入密码" :modal-append-to-body="false" :visible.sync="getPasswordVisible">
      <el-form :model="form" label-width="50px">
        <el-form-item label="密码">
          <el-input v-model="form.providedKey" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="downFile1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {getSecureDocumentAddress} from "@/api/hwWebDocument";

export default {
  name: 'PlatformFeatures',
  props: ['data'],
  computed: {},

  data() {
    return {
      getPasswordVisible: false,
      form: {}
    }
  },
  mounted() {
  },
  methods: {
    async downFile(e) {
      console.log(e)
      getSecureDocumentAddress({documentId: e.uuid}).then((res) => {
        if (res.code === 200) {
          console.log('下载地址', res.data)
        } else {
          this.form = {
            documentId: e.uuid,
            providedKey: ''
          }
          this.getPasswordVisible = true
          this.$message.error('请输入密钥')
        }
      })
    },
    downFile1() {
      if (!this.form.providedKey) {
        this.$message.error('请输入密码')
        return
      }

      getSecureDocumentAddress(this.form).then(async (res) => {
        if (res.code === 200) {
          console.log('下载地址', res)
          let url = res.msg
          let filename = url.split('/').at(-1)?.split('_')[0] + '.' + url.split('/').at(-1)?.split('.').at(-1)
          try {
            const response = await fetch(url, {mode: 'cors'});
            if (!response.ok) throw new Error('网络错误');

            const blob = await response.blob(); // 获取文件数据
            const blobUrl = URL.createObjectURL(blob);

            const a = document.createElement('a');
            a.href = blobUrl;
            a.download = filename || url.split('/').pop();
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);

            URL.revokeObjectURL(blobUrl); // 释放内存
            this.getPasswordVisible = false
          } catch (err) {
            console.error('下载失败:', err);
          }
        } else {
          this.$message.error('密钥错误')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import "~@/style.less";


.fileCard {
  vertical-align: top;
  display: inline-block;
  width: 40%;
  height: 100px;
  border: 1px solid #ccc;
  border-radius: 10px;
  margin-top: 1vw;
  position: relative;

  .cardTitle {
    position: absolute;
    top: 10px;
    left: 20px;
    font-size: 22px;
    font-weight: 700;
    line-height: 45px;
    width: calc(100% - 80px);
    white-space: nowrap; /* 不换行 */
    overflow: hidden; /* 超出部分隐藏 */
    text-overflow: ellipsis; /* 超出部分显示省略号 */
  }

  .cardValue {
    position: absolute;
    top: 55px;
    width: calc(100% - 80px);
    left: 20px;
    font-size: 14px;
    line-height: 45px;
    color: #aaa;
    white-space: nowrap; /* 不换行 */
    overflow: hidden; /* 超出部分隐藏 */
    text-overflow: ellipsis; /* 超出部分显示省略号 */
  }

  .downIcon {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
  }

  &:nth-child(2n) {
    margin-left: 5%;
  }
}
</style>


