<template>
  <div>
    <el-table v-loading="loading" :data="netList" height="200">
      <el-table-column label="网络名称" align="center" prop="netName" />
      <el-table-column label="wifi名称" align="center" prop="wifiName" />
      <el-table-column label="wifi密码" align="center" prop="wifiPass" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleCode(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
      title="WI-FI码"
      :visible.sync="wifiopen"
      width="420px"
      append-to-body
    >
      <div ref="downloadElement" style="padding: 20px 10px">
        <img :src="code.imgs" alt="" class="codeImg" />
        <div class="netName">店铺名称：{{ code.posName }}</div>
        <div class="netName">WI-FI名称：{{ code.wifiName }}</div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleUpload()">下载图片</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNet } from "@/api/netEwm/net";
import html2canvas from "html2canvas";
import { getCode } from "@/api/login.js";
export default {
  data() {
    return {
      wifiopen: false,
      code: {
        imgs: "",
        netName: "",
      },
      loading: true,
      netList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  methods: {
    getList(shanghuId) {
      this.queryParams.shanghuId = shanghuId;
      this.loading = true;
      listNet(this.queryParams).then((response) => {
        this.netList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCode(row) {
      let data = {
        path: "pages/index/index",
        posterId: row.id,
      };
      getCode(data).then((res) => {
        this.code.imgs = "data:image/png;base64," + res.data;
        this.code.wifiName = row.wifiName;
        this.code.posName = row.posName;
        this.wifiopen = true;
      });
    },
    handleUpload() {
      const downloadElement = this.$refs.downloadElement;
      // 使用html2canvas将元素转换为Canvas
      html2canvas(downloadElement).then((canvas) => {
        // 获取Canvas的Base64编码
        const base64Image = canvas.toDataURL("image/png");

        // 创建一个虚拟的下载链接
        const downloadLink = document.createElement("a");
        downloadLink.href = base64Image;
        downloadLink.download = "downloaded_image.png";

        // 模拟点击下载链接
        downloadLink.click();
      });
    },
  },
};
</script>

<style></style>
