import { MyPageContainer, usePageTabs } 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 BillInfo from '../components/BillInfo';
import CancelledBill from '../table/CancelledBill';
import PaidBill from '../table/PaidBill';
import UnpaidBill from '../table/UnpaidBill';

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

  console.log(data, 'pppp');
  // 注册标签页
  const { addTab } = usePageTabs({
    tabKey: `bill-detail-${id}`,
    tabLabel: `${data?.asset_house?.name}账单` || title,
  });

  const loadShow = () => {
    let paramsId: any = { asset_houses_id: id ?? 0 };
    Apis.Bill.HouseBills.SummaryShow(paramsId).then((res) => {
      setShow(res?.data);
      // 更新标签页标题
      if (res?.data?.asset_house?.name) {
        addTab({
          key: `bill-detail-${id}`,
          label: `${data?.asset_house?.name}档案`,
          path: `/bills/${id}`,
        });
      }
    });
  };

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

  let items = [
    {
      label: '欠费账单',
      key: '1',
      closable: false,
      children: <UnpaidBill item={{ ...data, asset_houses_id: id }} />,
    },
    {
      label: '已付账单',
      key: '2',
      closable: false,
      children: <PaidBill item={{ ...data, asset_houses_id: id }} />,
    },
    {
      label: '作废账单',
      key: '3',
      closable: false,
      children: <CancelledBill item={{ ...data, asset_houses_id: id }} />,
    },
  ];

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