/*
 * @Author: 赵伟
 * @Date: 2024-04-19 14:42:51
 * @Description: UI 公共方法
 */
import { PageEnum } from '@/enums/pagesEnums';
import themes from '@/styles/theme.less';
import { history } from '@umijs/max';
import { Modal, type ModalFuncProps, type UploadFile } from 'antd';

// 自定义 Confirm 弹框
export function modalConfirm({ title, content, onOk, ...rest }: ModalFuncProps) {
  Modal.confirm({
    ...rest,
    title: (
      <div>
        <img
          src="/assets/images/delete-icon.png"
          style={{ width: '120px', marginBottom: '24px' }}
          alt=""
        />
        <div style={{ color: themes.textColor, fontSize: '16px', fontWeight: 500 }}>{title}</div>
      </div>
    ),
    content: content && <div style={{ color: themes.textColor, fontSize: '15px' }}>{content}</div>,
    okText: '确认',
    cancelText: '取消',
    onOk: onOk,
  });
}

// 从事件中获取上传文件列表，用于 Upload + Form 中
export const getFileListFromEvent = (e: any) => {
  const fileList: UploadFile[] = (Array.isArray(e) ? e : e?.fileList) || [];
  return fileList.map((item) => {
    if (item.status === 'done') {
      const { response } = item;
      if (response?.code !== 200) {
        return {
          ...item,
          status: 'error',
        };
      }
    }
    return item;
  });
};

// 去登录页面
export const gotoLoginPage = (toHome: boolean = true) => {
  const { pathname, search } = window.location;
  const urlParams = new URLSearchParams();
  urlParams.append('redirect', pathname + search);
  const newSearch = toHome && pathname && pathname !== PageEnum.LOGIN ? '' : urlParams.toString();
  if (window.location.pathname !== PageEnum.LOGIN) {
    history.replace({
      pathname: PageEnum.LOGIN,
      search: newSearch,
    });
  }
};
