import React from 'react'
import axios from 'axios';

import { getQueryParam } from '../../../../util';

const ts = Date.now()

const FC = (props) => {
  const url = getQueryParam('downloadurl', props.location?.search);

  React.useEffect(() => {
    window.addEventListener('message', download)

    return () => {
      window?.removeEventListener("message", download);
    }
  }, [])

  const download = (e) => {
    console.log('type', e.data?.type)
    if (e.data?.type === 'excel.mount') {
      axios.request({
        method: 'GET',
        url,
        responseType: 'blob'
      }).then((resp) => {
        var iframe = document.getElementById('myIframe');
        iframe.contentWindow.postMessage({ data: resp.data, type: "preview.excel.file" }, "*");
      });
    }
  }

  return (
    <iframe
      id='myIframe'
      src={"http://192.168.0.12:8003/spreadjs/?date=" + ts}
      style={{ width: '100%', height: '100vh' }}
    />
  )
}

export default FC