import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';

import {
  HouseMetersMeterTypeEnum,
  HouseMetersUsageTypeEnum,
} from '@/gen/Enums';
import NormalReading from './modals/NormalReading';

export default function Index({ title = '抄表数据' }) {
  const navigate = useNavigate();

  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'house_meter_readings',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="house_meter_readings"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Meter.HouseMeters.List)
        }
        toolBarRender={(action) => [
          // <ReadingCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'meter_type',
            valueEnum: HouseMetersMeterTypeEnum,
            search: false,
          }),
          MyColumns.EnumTag({
            title: '使用类型',
            dataIndex: 'usage_type',
            valueEnum: HouseMetersUsageTypeEnum,
            search: false,
          }),
          {
            title: '仪表名称',
            dataIndex: 'name',
          },
          {
            title: '收费标准',
            dataIndex: ['charge_standard', 'name'],
            search: false,
          },
          {
            title: '初始抄表读数',
            dataIndex: 'initial_value',
            search: false,
          },
          {
            title: '最新抄表读数',
            dataIndex: 'latest_value',
            search: false,
          },
          {
            title: '初始抄表时间',
            dataIndex: 'initial_time',
            search: false,
          },
          {
            title: '最新抄表时间',
            dataIndex: 'latest_time',
            search: false,
          },
          {
            title: '备注',
            dataIndex: 'remark',
            search: false,
          },

          MyColumns.SoftDelete({
            onRestore: Apis.Company.Companies.Restore,
            onSoftDelete: Apis.Company.Companies.SoftDelete,
            search: false,
          }),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                {/* <CompanyShow item={item} title="查看" reload={action?.reload} /> */}
                <NormalReading
                  item={{ ...item, type: 'link' }}
                  title="抄表"
                  reload={action?.reload}
                />

                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Company.Companies.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
