/**
 * web端文件预览功能
 */
import React, { Component } from 'react';
import { checkIsImage } from '@/webPublic/one_stop_public/libs/UploadCom';
import { getModal, getPopconfirm, isFromIframe } from '@/webPublic/one_stop_public/utils/utils';
import Viewer from 'react-viewer';

// const FileViewer = CLIENT_TYPE === 'mobile' ? null : require('react-file-viewer');
const FileViewer = null;
const Modal = getModal();
const Popconfirm = getPopconfirm();

export default class index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
    };
    this.imageDom = null;
  }

  showModal = () => {
    if (isFromIframe()) {
      return false;
    }
    this.setState({
      visible: true,
    });
  };

  handleOk = e => {
    this.setState({
      visible: false,
    });
  };

  handleCancel = e => {
    this.setState({
      visible: false,
    });
  };
  download = () => {
    let { path, pathName } = this.props;
    if (window?.parent?.open && typeof window.parent.open === 'function') {
      window.parent.open(path, '_blank');
    } else {
      window.open(path, '_blank');
    }
  };

  render() {
    let { path, pathName, width, height } = this.props;

    const { visible } = this.state;
    let isShow = false;
    let type;
    let isImg = checkIsImage(pathName);
    if (isImg) {
      isShow = true;
    } else {
      let find = ['.pdf', '.mp3', '.mp4', '.csv'].find(x => {
        return pathName.indexOf(x) > -1;
      });
      if (find) {
        isShow = true;
      }
    }
    if (isShow) {
      type = pathName.split('.');
      type = Array.isArray(type) && type.length && type[type.length - 1];
    }

    // return null;

    return (
      <>
        {isShow ? (
          <Popconfirm
            title="查看附件"
            onConfirm={this.download}
            okText="下载"
            onCancel={this.showModal}
            cancelText={isFromIframe() ? '取消' : '预览'}
          >
            {isImg ? (
              <img
                ref={node => {
                  this.imageDom = node;
                }}
                style={{
                  width: width ? width : '100px',
                  height: height ? height : 'auto',
                }}
                src={path}
                alt={pathName}
              />
            ) : (
              <a target="_blank" href={path}>
                {pathName}
              </a>
            )}
          </Popconfirm>
        ) : (
          <a target="_blank" href={path}>
            {pathName}
          </a>
        )}
        {visible && (
          <Modal
            title={pathName}
            visible={true}
            width={1200}
            destroyOnClose
            centered={true}
            onOk={this.handleOk}
            onCancel={this.handleCancel}
          >
            {type === 'pdf' ? (
              <iframe
                style={{
                  width: '100%',
                  height: 600,
                }}
                src={path}
              />
            ) : (
              <div
                style={{
                  height: 650,
                  maxWidth: 1200,
                }}
              >
                {!checkIsImage(path) && <a onClick={this.download}>下载文件</a>}

                {!!checkIsImage(path) && (
                  <Viewer
                    visible={true}
                    onClose={this.handleCancel}
                    images={[
                      {
                        src: path,
                        alt: '预览',
                      },
                    ]}
                  />
                )}
              </div>
            )}
          </Modal>
        )}
      </>
    );
  }
}