/*
 * @Author: 吴文洁 
 * @Date: 2020-07-20 19:12:49 
 * @Last Modified by: 吴文洁
 * @Last Modified time: 2020-07-20 20:25:13
 * @Description: 大班直播分享弹窗
 */

import React from 'react';
import { Modal, Input, Button, message } from 'antd';
import domtoimage from 'dom-to-image';
import qrcode from "@/libs/qrcode/qrcode.js";
import User from '@/common/js/user';
import $ from 'jquery';
import CourseService from "@/domains/course-domain/CourseService";

import './SharePlanModal.less';

const  storeName = User.getStoreName();
const DEFAULT_COVER = 'https://image.xiaomaiketang.com/xm/YNfi45JwFA.png';

class ShareLiveModal extends React.Component {

  constructor(props) {
    super(props);
    
    this.state = {
      shareUrl: ''
    }
  }
  
  componentDidMount() {
    // 获取短链接
    this.handleConvertShortUrl();
  }

  handleConvertShortUrl = () => {
    const { longUrl } = this.props.data;
    // 发请求
    CourseService.getQrcode({
      urls: [longUrl]
    }).then((res) => {
      const { result = [] } = res;
     
      this.setState({
        shareUrl: result[0].shortUrl
      }, () => { 
        const qrcodeWrapDom = document.querySelector('#qrcodeWrap');
        const qrcodeNode = new qrcode({
          text: this.state.shareUrl,
          size: 98,
        })
        qrcodeWrapDom.appendChild(qrcodeNode);
      });
    })
  }

  componentWillUnmount() {
    // 页面销毁之前清空定时器
    clearTimeout(this.timer);
  }

  // 下载海报
  handleDownloadPoster = () => {
    this.setState({
      showImg:true,
      time:new Date().valueOf()
    },()=>{
      this.setState({time:new Date().valueOf()},()=>{
        let node = document.getElementById('poster');
        domtoimage.toPng(node)
          .then((imgData) => {
            console.log(imgData)
            const download = document.createElement('a');
            const { planName } = this.props.data;
            $(download).attr('href', imgData).attr('download', `${planName}.png`).get(0).click();
          })
      })
    })
  
    
  }
  
  // 复制分享链接
  handleCopy = () => {
    const textContent = document.getElementById('shareUrl').innerText;

    window.copyText(textContent);
    message.success('复制成功!');
  }
  
  render() {
    const {data} = this.props;
    const { planName, coverUrl = DEFAULT_COVER} = data;
    const { shareUrl,showImg,time} = this.state;
    return (
      <Modal
        title={'分享培训计划'}
        width={680}
        visible={true}
        footer={null}
        maskClosable={false}
        closeIcon={<span className="icon iconfont modal-close-icon">&#xe6ef;</span>} 
        className="share-live-modal"
        onCancel={this.props.close}
      >
      <div className="left">
        <div id="poster">
          <div className="store-name">
            <span className="text">{storeName}</span>
          </div>

          <div className="course-name-title">邀请你参与培训：</div>
         
          <div class="live-couse-name">{planName}</div>
          {
           showImg ?  <img
              crossOrigin='*'
              src={coverUrl+`?=${time}`}
              className="course-cover"
            />:  <img
              src={coverUrl+`?=${time}`}
              className="course-cover"
            />
          }
          <div className="qrcode-wrap">
            <div className="qrcode-wrap__left">
              <div className="text">长按识别二维码进入观看</div>
              <img className="finger" src="https://image.xiaomaiketang.com/xm/thpkWDwJsC.png"/>
            </div>
            <div className="qrcode-wrap__right" id="qrcodeWrap">
            </div>
          </div>
        </div>
      </div>
      <div className="right">
        <div className="share-poster right__item">
          <div className="title">① 海报分享</div>
          <div className="sub-title">用户可通过微信扫描海报二维码，查看培训计划</div>
          <div className="content" onClick={this.handleDownloadPoster}>下载海报</div>
        </div>

        <div className="share-url right__item">
          <div className="title">② 链接分享</div>
          <div className="sub-title">用户可通过微信或浏览器打开以下链接，查看培训计划</div>
          <div className="content url-content">
            <div className="share-url" id="shareUrl">{shareUrl}</div>
            <Button type="primary" onClick={this.handleCopy}>复制</Button>
          </div>
        </div>
      </div>
      </Modal>
    )
  }
}

export default ShareLiveModal;
