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

    <el-table
      v-loading="loading"
      :data="tableData"
      :height="tableHeigth + 'px'"
    >
      <el-table-column
        label="序号"
        align="center"
        type="index"
        prop="id"
        width="55"
      />
      <el-table-column label="活动名称" align="center" prop="name" />
      <el-table-column label="志愿者" align="center" prop="userName" />
      <el-table-column
        label="活动参与时间"
        align="center"
        prop="activityTime"
      />
      <el-table-column label="活动时长" align="center" prop="points">
        <template slot-scope="scope"> {{ scope.row.points }}小时 </template>
      </el-table-column>
      <el-table-column label="活动积分" align="center" prop="points" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span
            :style="{
              color: scope.row.issuanceStatus
                ? '#67C23A'
                : scope.row.issuanceStatus == false && scope.row.type == 2
                ? '#E6A23C'
                : '#F56C6C',
            }"
          >
            {{ filterIissuanceStatus(scope.row) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleInfo(scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="!scope.row.recordsId"
            size="mini"
            type="text"
            @click="handleApplyfor(scope.row)"
            >申请</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <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.name }}</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.activityTime }}</div>
              </div>
              <div class="list-left">
                <div>状态：</div>
                <div>{{ filterIissuanceStatus(form) }}</div>
              </div>
            </div>
          </div>
        </div>
        <div
          class="logisticsInfo"
          v-show="form.type == 2 && form.issuanceStatus == true"
        >
          <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 && form.issuanceStatus == true"
        >
          <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>
// 初始  false  null  （未申请）
// 志愿者申请电子   true   1 （已发放电子）
// 志愿者申请纸质  false   2 （待发放纸质）
// 管理端发放纸质  true   2 （已发放纸质）
import {
  dsbrecordsList,
  dsbrecordsInfo,
  getYcInfo,
  ycIssue,
  getYcList,
} from "@/api/volunteer/gxhzs/hdzsff/index.js";
import html2canvas from "html2canvas";
export default {
  data() {
    return {
      currentRow: {},
      radio: 1,
      applyShow: false,
      infoShow: false,
      infoOpen: false,
      form: {},
      rules: {
        courierCompanies: [
          { required: true, message: "请输入快递公司", trigger: "blur" },
        ],
        trackingNumber: [
          { required: true, message: "请输入快递公司", trigger: "blur" },
        ],
      },
      open: false,
      title: "",
      tableHeigth: 0,
      loading: false,
      tableData: [],
      total: 1,
      queryParams: {
        UId: null,
        name: undefined,
        // creType: 1, //1:一次性证书发放,2：个性化证书发放
        pageNum: 1,
        pageSize: 20,
      },
    };
  },
  created() {
    this.queryParams.UId = this.$store.getters.userId;
    // //给表格一个固定值
    this.$nextTick(() => {
      this.tableHeigth =
        this.$refs.main.offsetHeight - 40 - this.$refs.search.offsetHeight - 73;
      this.getList();
    });
  },
  methods: {
    /**状态过滤 */
    filterIissuanceStatus(row) {
      if (row.issuanceStatus == false && row.type == null) {
        return "未发放";
      } else if (
        (row.issuanceStatus == true && row.type == 1) ||
        (row.issuanceStatus == true && row.type == 2)
      ) {
        return "已发放";
      } else if (row.issuanceStatus == false && row.type == 2) {
        return "已申请";
      }
    },
    /**电子 */
    submitElectron() {
      let _this = this;
      let title = "";
      if (this.radio == 1) {
        title = `您确定要申请“${this.currentRow.name}”电子证书吗？`;
      } else {
        title = `您确定要申请“${this.currentRow.name}”纸质证书吗？`;
      }
      this.$modal.confirm(title).then(function () {
        let data = {
          status: _this.radio,
          id: _this.currentRow.id,
          type: _this.radio, //	1:电子证书 2:纸制证书
        };
        _this.subIssue(data);
      });
      this.applyShow = false;
    },
    //电子发放
    subIssue(data) {
      ycIssue(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");
      });
    },

    /**搜索 */
    handleSearch(keyWord) {
      this.queryParams = {
        name: keyWord,
        // creType: 1, //1:一次性证书发放,2：个性化证书发放
        pageNum: 1,
        pageSize: 10,
      };
      this.getList();
    },
    /**查详情 */
    async handleInfo(row) {
      let res = await this.getInfo(row.id);
      this.form = res.data;
      this.infoShow = true;
      // console.log("详情", res);
    },
    async getInfo(id) {
      let res = await getYcInfo(id);
      return res;
    },
    /**关闭 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /**重置 */
    reset() {
      this.form = {
        id: null,
        activityId: null,
        certificateId: null,
        uId: null,
        content: null,
        createId: null,
        createBy: null,
        activityTime: null,
        updateId: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        deptId: null,
        type: null,
      };
    },
    /** 查询发放记录列表 */
    getList() {
      this.loading = true;
      getYcList(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/myTable.scss";
.info-form-title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  font-size: 17px;
  font-family: "Alibaba-PuHuiTi-Regular";
  font-weight: 500;
  color: #333;
  font-weight: bold;
  span {
    width: 5px;
    height: 17px;
    margin-right: 6px;
    background: #f8414d;
  }
}
</style>
