import { MyPageContainer } from '@/common';
import { Apis } from '@/gen/Apis';
import { ProCard } from '@ant-design/pro-components';
import { useParams } from '@umijs/max';
import { Tabs } from 'antd';
import { useEffect, useState } from 'react';
import MeteInfo from '../components/MeteInfo';
import HasHouse from '../table/HasHouse';
import ReadingLIst from '../table/ReadingLIst';
import Tasks from '../table/Tasks';

export default function Show({ title = '仪表详情' }) {
  const { id } = useParams<{ id: string }>();
  const [data, setShow] = useState<any>({});

  const loadShow = () => {
    let paramsId: any = { id: id ?? 0 };
    Apis.Meter.HouseMeters.Show(paramsId).then((res) => {
      setShow(res?.data);
      // 更新标签页标题
      // if (res?.data?.name) {
      //   addTab({
      //     key: `meter-detail-${id}`,
      //     label: title,
      //     path: `/mete/readings/show/${id}`,
      //   });
      // }
    });
  };

  useEffect(() => {
    loadShow();
  }, [id]);

  let items = [
    {
      label: '抄表数据',
      key: 'Readings',
      closable: false,
      children: (
        <ReadingLIst
          item={{ ...data, id: id }}
          reload={() => {
            loadShow();
          }}
        />
      ),
    },
    {
      label: '仪表任务',
      key: 'Tasks',
      closable: false,
      children: <Tasks item={{ ...data, id: id }} />,
    },
    {
      label: '关联房屋',
      key: 'HasHouse',
      closable: false,
      children: <HasHouse item={{ ...data, id: id }} />,
    },
  ];

  return (
    <MyPageContainer
      title={`仪表:${data?.name}` || title}
      enableTabs={true}
      tabKey={`meter-detail-${id}`}
      tabLabel={`仪表:${data?.name}` || title}
    >
      <MeteInfo item={data} reload={loadShow} />
      <ProCard style={{ marginTop: 16 }}>
        <Tabs type="card" items={items} defaultActiveKey="1" />
      </ProCard>
    </MyPageContainer>
  );
}
