import { MyPageContainer, useCurrentPermissions } from '@/common';
import { useSearchParams } from '@umijs/max';
import type { TabsProps } from 'antd';
import { Tabs } from 'antd';
import { useEffect, useState } from 'react';
import CommonMeter from './common_meter';
import HouseMeter from './house_meter';

export default function Index({ title = '资源管理' }) {
  const [searchParams] = useSearchParams();
  const getCurrentPermissions = useCurrentPermissions();

  const [activeKey, setActiveKey] = useState('MyResources');
  const items: TabsProps['items'] = getCurrentPermissions({
    HouseMeter: {
      key: 'HouseMeter',
      label: '房屋表',
      children: <HouseMeter />,
    },
    CommonMeter: {
      key: 'CommonMeter',
      label: '公摊表',
      children: <CommonMeter />,
    },
  });
  useEffect(() => {
    if (searchParams?.get('key')) {
      setActiveKey(searchParams?.get('key') || '1');
    }
  }, []);

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="resources_page"
      tabLabel={title}
    >
      <Tabs type="card" defaultActiveKey={activeKey} items={items} />
    </MyPageContainer>
  );
}
