<template>
  <div class="file-item">
    <div class="file-item-container" ref="htmlElement">
      <div class="file-header_title">宁夏电投新能源有限公司</div>
      <div class="file-header">{{ items.proName }}</div>
      <div class="item-content">
        <div class="file-icon">
          <img :src="`${items.faceUrl ? lookFile+items.faceUrl : ''}`" style="width: 100px; height: 100px;" alt="文件图标"/>
        </div>
        <div class="file-details">
          <div class="file-info">姓名: {{ items.name }}</div>
          <div class="file-info">联系电话: {{ items.phone }}</div>
          <div class="file-info">岗位: {{ items.postName }}</div>
        </div>
      </div>
    </div>
    <div class="file-footer">
      <el-button type="primary" @click="download(items)" size="small">下载</el-button>
    </div>
  </div>
</template>
<script>
import html2canvas from 'html2canvas';
import { saveAs } from 'file-saver';
import { lookFaceFile } from '@/utils/bonus'
export default {
  components: {},
  data() {
   return {
     lookFile:''
   }
  },
  props: {
    items: {
      type: Object,
      default: () => {
        return {
          proName: '',
          faceUrl: '',
          name: '',
          phone: '',
          postName: ''
        }
      }
    },
  },
  created() {
    this.lookFile = lookFaceFile()
  },
  methods: {
    async download(item) {
      try {
        const element = this.$refs.htmlElement;
        if (!element) {
          throw new Error('HTML element not found');
        }

        const canvas = await html2canvas(element);
        const imageDataURL = canvas.toDataURL('image/png');

        saveAs(imageDataURL, item.name+'的工作证.png');
      } catch (error) {
        console.error('Error converting HTML to PNG:', error);
      }
    },
  }
}
</script>
<style scoped>
.file-item-container {
  border: 1px solid #ccc;
  padding: 50px;
  border-radius: 5px;
  background-color: #f9f9f9;
}
.file-header_title {
  text-align: center;
  font-weight: bold;
  font-size: 22px;
  margin-bottom: 10px;
  margin-top: -10px;
}
.file-header {
  text-align: center;
  font-size: 20px;
  margin-bottom: 10px;
}

.item-content {
  display: flex;
  align-items: center;
  margin-left: 25%;
}

.file-icon img {
  margin-right: 20px;
}

.file-details {
  flex: 1; /* 文件详情占据剩余空间 */
}

.file-info {
  margin-bottom: 5px;
}

.file-footer {
  text-align: right;
  margin-top: 10px;
}
</style>
