/*
 * @Author: zwy 1518051043@qq.com
 * @Date: 2026-01-04 09:13:55
 * @LastEditors: zwy 1518051043@qq.com
 * @LastEditTime: 2026-01-21 16:24:06
 * @FilePath: \yuanxinPro\src\pages\all\components\loadingComponent\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const SubLoading = (function() {
  let loadingInstance = null;
  let styleInstance = null;

  function createLoading(options = {}) {
    const {
      text = '正在加载中',
      background = 'rgba(0, 0, 0, 0.57)',
      color = '#fff',
      zIndex = 9999999,
      spinnerColor = color,
      spinnerSize = '40px'
    } = options;

    // 如果已经存在，直接复用（避免重复创建）
    if (loadingInstance) return;

    loadingInstance = document.createElement('div');
    loadingInstance.className = 'custom-loading';
    Object.assign(loadingInstance.style, {
      position: 'fixed',
      top: '0',
      left: '0',
      width: '100%',
      height: '100%',
      backgroundColor: background,
      display: 'flex',
      justifyContent: 'center',
      alignItems: 'center',
      zIndex: String(zIndex),
      flexDirection: 'column'
    });

    const spinnerEl = document.createElement('div');
    spinnerEl.className = 'custom-loading-spinner';
    Object.assign(spinnerEl.style, {
      border: '4px solid rgba(0, 0, 0, 0.3)',
      borderRadius: '50%',
      borderTop: `4px solid ${spinnerColor}`,
      width: spinnerSize,
      height: spinnerSize,
      animation: 'spin 1s linear infinite'
    });

    const textEl = document.createElement('p');
    textEl.className = 'custom-loading-text';
    textEl.textContent = text;
    Object.assign(textEl.style, {
      color: color,
      marginTop: '20px',
      fontFamily: 'Arial, sans-serif'
    });

    styleInstance = document.createElement('style');
    styleInstance.textContent = `
      @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
      }
    `;

    loadingInstance.appendChild(spinnerEl);
    loadingInstance.appendChild(textEl);
    document.head.appendChild(styleInstance);
    document.body.appendChild(loadingInstance);
  }

  function destroyLoading() {
    if (loadingInstance) {
      document.body.removeChild(loadingInstance);
      document.head.removeChild(styleInstance);
      loadingInstance = null;
      styleInstance = null;
    }
  }

  return {
    show: createLoading,
    hide: destroyLoading
  };
})();

export default SubLoading;