<template>
  <div id="goodTopic">
    <!-- <search-box-new
      ref="searchBox"
      :formData="formData"
      :formList="formList"
      @search="search"
      @init="init"
      :topType="topType"
    /> -->
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      :loading="loading"
    >
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="share(props.scope.row)">分享</el-button>
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="formData.pageSize"
      :currentPage="formData.pageNum"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <el-dialog
      :visible.sync="dialogVisible"
      width="650px"
      :show-close="false"
      :close-on-click-modal="false"
      destroy-on-close
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">分享</div>
        <div class="rightBoxs">
          <img
            src="@/assets/images/Close@2x.png"
            alt=""
            @click="dialogVisible = false"
          />
        </div>
      </div>
      <el-form label-width="180px" :model="formLabelAlign">
        <el-form-item label="观看链接：">
          <span id="copycode">{{ formLabelAlign.enCodePC }}</span
          ><el-button
            v-if="formLabelAlign.enCodePC"
            type="text"
            style="margin-left: 10px"
            @click="copyUrl"
            >复制</el-button
          >
        </el-form-item>
        <el-form-item label="观看二维码：" v-if="false">
          <img
            style="width: 100px; height: 100px"
            :src="formLabelAlign.enCode"
            alt=""
          />
          <el-button
            type="text"
            @click="downloadFileByBase64(formLabelAlign.enCode, '小程序二维码')"
            >下载</el-button
          >
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关 闭</el-button>
        <!-- <el-button type="primary" @click="submitTable">保 存</el-button> -->
      </span>
    </el-dialog>
  </div>
</template>

<script>
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  name: "GoodTopic",
  components: { searchBoxNew, tableList, pagination },
  data() {
    return {
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "商品专题页",
        index: 0,
        ch: "条",
        num: false,
        border: true,
        choice: true,
        addHide: true,
        changeWidth: "180px",
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      topType: true,
      dialogVisible: false, //分享窗口
      formLabelAlign: {
        enCodePC: '',
        enCode: '',
      },
      //搜索
      formList: [],
      formData: {
        pageSize: 10,
        pageNum: 1,
      },
      // 表单
      tableSet: [
        {
          label: "教育类型",
          prop: "educationName",
          hidden: true,
        },
        {
          label: "业务层次",
          prop1: "projectName",
          prop2: "businessName",
          hidden: true,
          scope: "InfoMore",
        },
        {
          label: "业务层次别名",
          prop: "aliasName",
          hidden: true,
        },
        {
          label: "专题页状态",
          prop: "status",
          hidden: true,
          scope: "isOptions",
          options: [
            {
              label: "启用",
              value: 1,
            },
            {
              label: "关闭",
              value: 0,
            },
          ],
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
    };
  },
  mounted() {
    this.search();
  },
  activated() {
    this.search();
  },
  methods: {
    share(v) {
      this.formLabelAlign.enCodePC = v.enCodePC
      this.formLabelAlign.enCode = v.enCode
      this.dialogVisible = true
    },
    search(int) {
      this.loading = true;
      if (int === 1) {
        this.formData.pageNum = 1;
      }
      if (int === 2) {
        this.formData = {
          pageSize: 10,
          pageNum: 1,
        };
      }
      if (int === 3) {
        this.formData.pageNum = 1;
      }
      var data = JSON.parse(JSON.stringify(this.formData));
      this.$api
        .inquirecoursetopiclist(data)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    init() {
      this.search(2);
    },
      /**
     * 复制地址
     */
    copyUrl() {
      const range = document.createRange();//创建range对象;
      range.selectNode(document.getElementById("copycode")); //获取复制内容的 id 选择器
      const selection = window.getSelection(); //创建 selection对象
      if (selection.rangeCount > 0) selection.removeAllRanges(); //如果页面已经有选取了的话，会自动删除这个选区，没有选区的话，会把这个选取加入选区
      selection.addRange(range); //将range对象添加到selection选区当中，会高亮文本块
      document.execCommand("copy"); //复制选中的文字到剪贴板
      this.$message.success("复制成功");
      selection.removeRange(range); // 移除选中的元素
    },
    //将base64转换为blob
    dataURLtoBlob(dataurl) {
      if (!dataurl) {
        this.$message.error("下载失败");
        return;
      }
      var arr = dataurl.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new Blob([u8arr], { type: mime });
    },
    //下载方法
    downloadFile(url, name = "下载图") {
      var a = document.createElement("a");
      a.setAttribute("href", url);
      a.setAttribute("download", name);
      a.setAttribute("target", "_blank");
      let clickEvent = document.createEvent("MouseEvents");
      clickEvent.initEvent("click", true, true);
      a.dispatchEvent(clickEvent);
    },

    downloadFileByBase64(base64, name) {
      var myBlob = this.dataURLtoBlob(base64);
      var myUrl = URL.createObjectURL(myBlob);
      this.downloadFile(myUrl, name);
    },
    del(v) {
      this.$api.gradecheckGoodsChange({ goodsId: v.goodsId }).then((res) => {
        if (res.data > 0) {
          this.$message.error("已有学员正在学习该商品，无法删除");
          return;
        } else {
          this.$alert(
            "确定删除此内容？<br />内容删除后将无法恢复，请慎重考虑",
            "提示",
            {
              dangerouslyUseHTMLString: true,
            }
          )
            .then(() => {
              var data = {
                goodsId: v.goodsId,
                status: -1,
              };
              this.$api.editGoods(data).then((res) => {
                this.$message.success("删除成功");
                this.search();
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
      });
    },
    handleSizeChange(v) {
      this.formData.pageSize = v;
      this.formData.pageNum = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.formData.pageNum = v;
      this.search();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-button {
  border-radius: 8px;
}
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    padding: 0;
    .hearders {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0px 18px 0px 20px;
      border-bottom: 1px solid #e2e2e2;
      .leftTitle {
        font-size: 14px;
        font-weight: bold;
        color: #2f4378;
      }
      .rightBoxs {
        display: flex;
        align-items: center;
        img {
          width: 14px;
          height: 14px;
          margin-left: 13px;
          cursor: pointer;
        }
      }
    }
  }
  .el-dialog__footer {
    padding: 0;
    .dialog-footer {
      padding: 0px 40px;
      height: 70px;
      border-top: 1px solid #e2e2e2;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
.imgBox {
  width: 100%;
  // height: 210px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
  padding: 8px 8px 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .imgLabel {
    flex: 1;
    width: 100%;
    border: 1px dotted #e2e2e2;
    color: #999;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    .msPhoto {
      display: flex;
      justify-content: center;
      align-items: center;
      max-width: 100%;
      max-height: 270px;
      img {
        max-width: 100%;
        max-height: 270px;
      }
    }
    .imgbbx {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      i {
        font-weight: bold;
        margin: 14px 0;
        font-size: 24px;
      }
    }
  }
  p {
    margin: 5px 0px;
  }
}
</style>

