import { MyPageContainer, useCurrentPermissions } from '@/common';
import { useNavigate, useSearchParams } from '@umijs/max';
import type { TabsProps } from 'antd';
import { Button, Tabs } from 'antd';
import { useEffect, useState } from 'react';
import IsVisited from './components/IsVisited';
import NoVisited from './components/NoVisited';

export default function Index({ title = '工单回访' }) {
  const getCurrentPermissions = useCurrentPermissions();

  const [searchParams] = useSearchParams();
  const [activeKey, setActiveKey] = useState('1');
  const navigate = useNavigate();
  const items: TabsProps['items'] = getCurrentPermissions({
    NoVisited: {
      key: 'NoVisited',
      label: '未回访工单',
      children: <NoVisited />,
    },
    IsVisited: {
      key: 'IsVisited',
      label: '已回访工单',
      children: <IsVisited />,
    },
  });

  useEffect(() => {
    if (searchParams?.get('key')) {
      setActiveKey(searchParams?.get('key') || '1');
    }
  }, []);

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="visited"
      tabLabel={title}
    >
      <Tabs
        defaultActiveKey={activeKey}
        items={items}
        type="card"
        tabBarExtraContent={{
          right: (
            <Button
              type="primary"
              onClick={() => navigate('/work_order/work_bi')}
            >
              返回工单BI
            </Button>
          ),
        }}
      />
    </MyPageContainer>
  );
}
