<template>
  <el-dialog title="游戏预览" :visible.sync="show" width="600px" :before-close="close" :show-close="showClose">
    <div class="link-tip">
      <i class="el-icon-info"></i>
      非会员进入游戏页面将提示进行认证注册成为会员，认证成功后自动返回游戏页面
    </div>
    <div v-loading="loading" class="links-block">
      <div class="links__body">
        <div class="links__body--url">{{ miniprogram.link }}</div>
        <div class="links__body--btn">
          <el-button v-clipboard:text="miniprogram.link" v-show="miniprogram.link" type="text" icon="iconfont icon-lianjie fz14"> 复制链接</el-button>
        </div>
      </div>
      <div class="link__divider"></div>
      <div class="links__qr">
        <div style="text-align:center">
          <el-image style="width:102px;height:102px;border:4px solid #fff;cursor: pointer;" lazy :src="miniprogram.url" @click.native="onView(false)">
            <div slot="placeholder" style="display:flex;align-items:center;justify-content:center;width:100%;height:100%;">
              <i class="el-icon-picture-outline" style="font-size:30px"></i>
            </div>
          </el-image>
        </div>
        <el-button class="links__qr--btn" v-show="miniprogram.url" type="text" icon="iconfont icon-icon_yunxiazai fz14 mr4" @click="downloadMiniporgramImg">小程序二维码下载</el-button>
      </div>
    </div>
    <div slot="footer" v-if="showFooter">
      <slot></slot>
    </div>
  </el-dialog>
</template>
<script>
import VueQr from 'vue-qr';
import { formatDateTimeByType } from '@/utils/index.js';
import { generateMiniProgramLink, getQrGame } from '@/service/api/gameApi.js';
import { api as viewerApi } from 'v-viewer';
import 'viewerjs/dist/viewer.css';
export default {
  name: 'links',
  components: {
    VueQr
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    obj: {
      type: Object,
      default() {
        return {};
      }
    },
    gameTypeName: {
      type: String,
      default: ''
    },
    showFooter: {
      type: Boolean,
      default: false
    },
    isNew: {
      type: Boolean,
      default: false
    },
    showClose: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      loading: false,
      qcText: '',
      miniprogram: {
        link: '',
        url: ''
      }
    };
  },
  watch: {
    show(val) {
      if (val) {
        this.miniprogram = {
          link: '',
          url: ''
        };
        this.createQrImg();
        if (this.isNew) {
          this.getQrGame();
        } else {
          this.generateMiniProgramLink();
        }
      }
    }
  },
  methods: {
    createQrImg() {
      this.qcText = this.obj.gameUrl || '';
    },
    generateMiniProgramLink() {
      this.loading = true;
      let gameType = '';
      switch (this.gameTypeName) {
        case '大转盘':
          gameType = 'GAME_LINK_DZP';
          break;
        case '刮刮卡':
          gameType = 'GAME_LINK_GGK';
          break;
        case '拼图游戏':
          gameType = 'GAME_LINK_PTYX';
          break;
        case '找你马':
          gameType = 'GAME_LINK_ZNM';
          break;
        case '口令福利':
          gameType = 'GAME_LINK_KLFL';
          break;
        case '拆盲盒':
          gameType = 'GAME_LINK_CMH';
          break;
      }
      generateMiniProgramLink({
        id: this.obj.gameId || this.obj.gameActivityId,
        linkId: gameType
      })
        .then(res => {
          this.miniprogram = res.result;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    getQrGame() {
      this.loading = true;
      getQrGame({ id: this.obj.gameId })
        .then(res => {
          this.miniprogram = res.result;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    close() {
      this.$emit('update:show', false);
    },

    onView(isH5) {
      let qrImg = '';
      if (isH5) {
        const qrWrap = document.getElementById('qrWrap');
        qrImg = qrWrap && qrWrap.childNodes[0].src;
      } else {
        qrImg = this.miniprogram.url + '?imageMogr2/thumbnail/500x500';
      }
      viewerApi({
        images: [qrImg],
        options: {
          toolbar: false,
          navbar: false,
          title: false,
          zIndex: 9999
        }
      });
    },
    downloadMiniporgramImg() {
      const downloadFile = (fileName, content) => {
        const url = content;
        const x = new XMLHttpRequest();
        x.open('GET', url, true);
        x.responseType = 'blob';
        x.onload = function(e) {
          const blob = new Blob([x.response]);
          let url = window.URL.createObjectURL(blob);
          let a = document.createElement('a');
          a.href = url;
          a.download = fileName + '.png';
          a.click();
        };
        x.send();
      };
      downloadFile(this.gameTypeName + '-' + this.obj.gameName + '-小程序-' + formatDateTimeByType(this.obj.gameStartTime, 'yyyy-MM-dd-HH-mm-ss') || '游戏链接', this.miniprogram.url);
    }
  }
};
</script>
<style lang="scss" scoped>
.links__body {
  // border: 1px solid #ddd;
  flex: 1;
  &--url {
    height: 138px;
    overflow-x: hidden;
    overflow-y: auto;
    word-break: break-all;
    padding: 13px 16px 0 16px;
    line-height: 17px;
    font-size: 12px;
  }
  &--btn {
    text-align: center;
    line-height: 1;
  }
}
.links__qr {
  padding: 22px 30px 10px 24px;
  text-align: center;
  &--btn {
    margin: 18px 0 0;
  }
}
.link__divider {
  margin-top: 19px;
  height: 130px;
  width: 1px;
  background-color: #ebecf0;
}
.links-block {
  display: flex;
  background: #f7f8fa;
  margin: 10px 0 10px;
}
.link-title {
  display: flex;
  align-items: center;
  font-weight: 500;
  color: #303133;
  line-height: 20px;
  margin-top: 20px;
  i {
    font-size: 20px;
    margin-right: 8px;
  }
}
.link-tip {
  width: 552px;
  background: #fcf6f1;
  border-radius: 2px;
  font-size: 12px;
  color: #303133;
  line-height: 22px;
  padding: 5px 0;
  margin-bottom: 16px;
  display: flex;
  .el-icon-info {
    line-height: 22px;
    margin-left: 17px;
    margin-right: 9px;
    color: #fa8c16;
    font-size: 12px;
  }
}
#qrWrap {
  /deep/ img {
    border: 4px solid #fff;
    width: 102px;
    height: 102px;
    cursor: pointer;
  }
}
</style>

<style lang="scss">
.mr4 {
  margin-right: 4px;
}
</style>
