/**
 * @name 租赁合同管理
 */
import React, { useMemo, useRef, useState } from 'react';
import * as antd from 'antd';
import { ConfigProvider } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import {
  type AnnotationSourceDocument,
  type AnnotationViewerOptions
} from '@axhub/annotation';
import { PrototypeAnnotationHost } from '../../common/prototype-annotation-host';
import 'antd/dist/reset.css';
import '../vehicle-management/style.css';
import './styles/index.css';
import '../contract-template-management/contract-template-catalog.js';
import LeaseContractApp from './LeaseContractManagement.jsx';
import annotationSourceDocument from './annotation-source.json';

type LeaseContractPageId = 'list' | 'create';

type LeaseContractNavigationRef = {
  openCreate?: () => void;
  backToList?: () => void;
};

declare global {
  interface Window {
    React: typeof React;
    antd: typeof antd;
  }
}

window.React = React;
window.antd = antd;

dayjs.locale('zh-cn');

/** 与 vehicle-management / ONE-OS 主题一致 */
const vmTheme = {
  token: {
    colorPrimary: '#32a06e',
    colorLink: '#32a06e',
    colorLinkHover: '#3fb87c',
    borderRadius: 8,
    fontFamily:
      'Inter, -apple-system, BlinkMacSystemFont, "SF Pro Display", "PingFang SC", "Microsoft YaHei", sans-serif',
    fontSize: 14,
    colorText: '#18181b',
    colorTextSecondary: '#52525b',
    colorBorder: '#e5e7eb',
    colorBgContainer: '#ffffff',
  },
  components: {
    Table: {
      headerBg: '#f6f7f7',
      headerColor: '#71717a',
      rowHoverBg: '#f6f7f7',
      borderColor: '#e5e7eb',
      cellPaddingBlock: 6,
      cellPaddingInline: 10,
    },
    Card: {
      borderRadiusLG: 12,
    },
    InputNumber: {
      handleVisible: true,
    },
    DatePicker: {
      cellActiveWithRangeBg: 'rgba(50, 160, 110, 0.12)',
      cellHoverWithRangeBg: 'rgba(50, 160, 110, 0.08)',
      cellRangeBorderColor: '#32a06e',
      activeBorderColor: '#32a06e',
      hoverBorderColor: '#3fb87c',
    },
  },
};

export default function LeaseContractManagementPage() {
  const [annotationPageId, setAnnotationPageId] = useState<LeaseContractPageId>('list');
  const navigationRef = useRef<LeaseContractNavigationRef>({});

  const annotationOptions = useMemo<AnnotationViewerOptions>(
    () => ({
      showToolbar: true,
      showThemeToggle: true,
      showColorFilter: true,
      emptyWhenNoData: false,
      toolbarEdge: 'right',
      currentPageId: annotationPageId,
    }),
    [annotationPageId],
  );

  return (
    <ConfigProvider theme={vmTheme} locale={zhCN}>
      <LeaseContractApp
        navigationRef={navigationRef}
        onPageViewChange={setAnnotationPageId}
      />
      <PrototypeAnnotationHost
        source={annotationSourceDocument as AnnotationSourceDocument}
        options={annotationOptions}
      />
    </ConfigProvider>
  );
}
