/**
 * KRA - Word Document Viewer Component
 * Display Word documents using Microsoft Office Online or Google Docs
 */
import React from 'react';
import { Empty, Spin } from 'antd';

interface DocxViewerProps {
  url?: string;
  width?: string | number;
  height?: string | number;
  loading?: boolean;
  provider?: 'microsoft' | 'google';
  className?: string;
  style?: React.CSSProperties;
}

const DocxViewer: React.FC<DocxViewerProps> = ({
  url,
  width = '100%',
  height = 600,
  loading = false,
  provider = 'microsoft',
  className,
  style,
}) => {
  if (!url) {
    return <Empty description="暂无Word文档" />;
  }

  // Use Microsoft Office Online or Google Docs viewer
  const viewerUrl = provider === 'microsoft'
    ? `https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(url)}`
    : `https://docs.google.com/viewer?url=${encodeURIComponent(url)}&embedded=true`;

  return (
    <Spin spinning={loading}>
      <iframe
        src={viewerUrl}
        className={className}
        style={{
          width: typeof width === 'number' ? `${width}px` : width,
          height: typeof height === 'number' ? `${height}px` : height,
          border: 'none',
          ...style,
        }}
        title="Word Viewer"
      />
    </Spin>
  );
};

export default DocxViewer;
