<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' : '' }"
      >
        <div class="integral-num">积分：{{ item.integral }}</div>
        <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:
                  item.status == 1
                    ? '#67C23A'
                    : item.status == 2 && item.type == 2
                    ? '#E6A23C'
                    : '#F56C6C',
              }"
              >{{ filterIissuanceStatus(item) }}</el-button
            >
            <el-button
              type="text"
              size="mini"
              @click="handleApplyfor(item)"
              v-if="item.status != 1 && !item.type"
              >申请</el-button
            >
            <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>{{ filterIissuanceStatus(form) }}</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>
    <!-- 申请 -->
    <el-dialog :visible.sync="applyShow" width="350px" append-to-body>
      <div slot="title" class="dialog-title">
        <span class="title-line"></span>
        申请操作
      </div>
      <el-radio-group v-model="radio">
        <el-radio-button :label="1">电子证书</el-radio-button>
        <el-radio-button :label="2">纸质证书</el-radio-button>
      </el-radio-group>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitElectron">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import html2canvas from "html2canvas";
import {
  dsbrecordsList,
  dsbrecordsInfo,
  issue,
} from "@/api/volunteer/gxhzs/hdzsff/index.js";
export default {
  data() {
    return {
      listStyle: {
        height: 0,
        overflowY: "auto",
      },
      currentRow: {},
      radio: 1,
      applyShow: false,
      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: {
    /**状态过滤 */
    filterIissuanceStatus(row) {
      if (row.status == 2 && !row.type) {
        return "未发放";
      } else if (
        (row.status == 1 && row.type == 1) ||
        (row.status == 1 && row.type == 2)
      ) {
        return "已发放";
      } else if (row.status == 2 && row.type == 2) {
        return "已申请";
      }
    },
    /**电子 */
    submitElectron() {
      //status  type
      //1        1    志愿者电子证书发放
      //2        2    志愿者申请纸质证书
      //1        2   管理发放纸质证书
      let _this = this;
      let title = "";
      if (this.radio == 1) {
        title = `您确定要申请“${this.currentRow.certificateName}”电子证书吗？`;
      } else {
        title = `您确定要申请“${this.currentRow.certificateName}”纸质证书吗？`;
      }
      this.$modal.confirm(title).then(function () {
        let data = {
          status: _this.radio, //1.发放 2.未
          id: _this.currentRow.id,
          type: _this.radio, //	1:电子证书 2:纸制证书
        };
        _this.subIssue(data);
      });
      this.applyShow = false;
    },
    //电子发放
    subIssue(data) {
      issue(data).then((res) => {
        this.applyShow = false;
        this.getList();
        if (data.type == 1) {
          this.$modal.msgSuccess("发放成功");
        } else {
          this.$modal.msgSuccess("申请成功");
        }
      });
    },
    /**申请操作 */
    handleApplyfor(row) {
      this.currentRow = row;
      this.applyShow = true;
    },
    /**下载电子证书 */
    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>
