<template>
  <div class="app-container" ref="main">
    <div ref="search" class="search">
      <MyInput v-model="queryParams.name" @clickSearch="handleSearch" />
    </div>

    <ul class="book-main" :style="listStyle" v-if="certificatesList.length > 0">
      <li
        v-for="(item, index) in certificatesList"
        :key="item.id"
        :style="{ marginBottom: isLastRow(index) ? '0px' : '' }"
      >
        <el-image
          style="width: 100%; height: 87%"
          :src="baseUrl + item.cover"
          fit="fill"
        >
        </el-image>

        <div class="operate">
          <div class="integral">
            {{ item.certificateName }}
          </div>
          <div class="operate-child">
            <el-button
              type="text"
              size="mini"
              style="color: #909399"
              @click="handleInfo(item)"
              >查看</el-button
            >
          </div>
        </div>
      </li>
    </ul>
    <div v-if="certificatesList.length == 0" :style="listStyle" class="noData">
      <el-empty description="暂无证书"></el-empty>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 查看 -->
    <el-dialog :visible.sync="infoShow" width="600px" append-to-body>
      <div slot="title" class="dialog-title">
        <span class="title-line"></span>
        查看
      </div>
      <div class="conversionInfo">
        <div class="basicInfo">
          <div class="title">
            <img src="@/assets/images/huodong.png" alt="" />
            <span>证书详情</span>
          </div>
          <div class="info">
            <div class="lists">
              <div class="list-left">
                <div>证书名称：</div>
                <div>{{ form.certificateName }}</div>
              </div>
              <div class="list-right">
                <div>志愿者：</div>
                <div>{{ form.userName }}</div>
              </div>
            </div>
            <div class="lists">
              <div class="list-left">
                <div>兑换时间：</div>
                <div>{{ form.createTime }}</div>
              </div>
              <div class="list-left">
                <div>状态：</div>
                <div>{{ form.status == 1 ? "已发放" : "未发放" }}</div>
              </div>
            </div>
          </div>
        </div>
        <div class="logisticsInfo" v-show="form.type == 2">
          <div class="title">
            <img src="@/assets/images/huodong.png" alt="" />
            <span>纸制证书</span>
          </div>
          <div class="info">
            <div class="lists">
              <div class="list-left">
                <div>快递公司：</div>
                <div>{{ form.courierCompanies }}</div>
              </div>
              <div class="list-right">
                <div>快递单号：</div>
                <div>{{ form.trackingNumber }}</div>
              </div>
            </div>
          </div>
        </div>
        <div class="logisticsInfo" v-show="form.type == 1">
          <div class="title">
            <img src="@/assets/images/huodong.png" alt="" />
            <span>电子证书</span>
          </div>
          <div class="info zs-box" ref="zs">
            <div class="top-text">
              <span>{{ form.userName }}</span> 先生/女士：
              <p>
                您在苏州工业园区累计志愿服务0小时，感谢您的无私奉献，特发此证，以兹鼓励。
              </p>
            </div>
            <div class="bottom-text">
              <div class="lable-value">
                <div class="lable">签发单位：</div>
                <div class="value">苏州工业园区宣传和统战部</div>
              </div>
              <div class="lable-value">
                <div class="lable"></div>
                <div class="value">苏州工业园区新时代文明实践中心</div>
              </div>
              <div class="lable-value">
                <div class="lable"></div>
                <div class="value">
                  {{ $moment().format("YYYY年MM月DD日") }}
                </div>
              </div>
            </div>
            <div class="zs-z"></div>
          </div>
          <el-button
            type="primary"
            size="small"
            style="margin-top: 10px"
            @click="downloadContentAsImage()"
            >下载电子证书</el-button
          >
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import html2canvas from "html2canvas";
import {
  dsbrecordsList,
  dsbrecordsInfo,
} from "@/api/volunteer/gxhzs/hdzsff/index.js";
export default {
  data() {
    return {
      listStyle: {
        height: 0,
        overflowY: "auto",
      },
      infoShow: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      certificatesList: [],
      loading: false,
      tableData: [],
      total: 0,
      queryParams: {
        status: 1,
        UId: undefined,
        certificateName: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      form: {},
      rules: {},
    };
  },
  created() {
    this.queryParams.UId = this.$store.getters.userId;
    // //给表格一个固定值
    this.$nextTick(() => {
      this.listStyle.height =
        this.$refs.main.offsetHeight -
        40 -
        this.$refs.search.offsetHeight -
        42 +
        "px";
      this.getList();
    });
  },
  methods: {
    /**下载电子证书 */
    downloadContentAsImage() {
      const content = this.$refs.zs; // 获取目标<div>的DOM节点
      // 创建一个Canvas元素，并设置宽高等于目标<div>的尺寸
      const canvas = document.createElement("canvas");
      canvas.width = content.offsetWidth;
      canvas.height = content.offsetHeight;
      const context = canvas.getContext("2d");
      // 使用Canvas绘制目标<div>中的内容
      html2canvas(content).then((canvas) => {
        // 将Canvas转换为DataURL
        const dataURL = canvas.toDataURL("image/png");
        // 使用FileSaver.js保存DataURL为图片文件
        saveAs(dataURL, "download.png");
      });
    },

    isLastRow(index) {
      const rowCount = Math.ceil(this.certificatesList.length / 5);
      const row = Math.floor(index / 5);
      return row === rowCount - 1;
    },
    /**搜索 */
    handleSearch(keyWord) {
      this.queryParams = {
        status: 1,
        UId: this.$store.getters.userId,
        certificateName: keyWord,
        pageNum: 1,
        pageSize: 10,
      };
      this.getList();
    },
    /** 查询证书管理列表 */
    getList() {
      this.loading = true;
      dsbrecordsList(this.queryParams).then((response) => {
        this.certificatesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**查看 */
    /**查详情 */
    async handleInfo(row) {
      let res = await this.getInfo(row.id);
      this.form = res.data;
      this.infoShow = true;
    },
    async getInfo(id) {
      let res = await dsbrecordsInfo(id);
      return res;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/myTable.scss";
</style>
