import {
  MyButtons,
  MyColumns,
  MyImportModal,
  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 CompanyCreate from './modals/MeterCreate';
import MeterHasHouse from './modals/MeterHasHouse';
import MeterHasOne from './modals/MeterHasOne';
import CompanyUpdate from './modals/MeterUpdate';

export default function Index({ title = '仪表管理' }) {
  const navigate = useNavigate();

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

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="house_meters"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Meter.HouseMeters.List)
        }
        toolBarRender={(action) => [
          <MyImportModal
            key="ImportHouse"
            title="导入仪表"
            type="danger"
            size="middle"
            templateApi={Apis.Meter.HouseMeters.DownloadTemplate}
            importApi={Apis.Meter.HouseMeters.Import}
            reload={action?.reload}
          />,
          <MyImportModal
            key="ImportHouse"
            title="导入关联房屋"
            type="danger"
            size="middle"
            templateApi={Apis.Meter.HouseMeterHasHouses.DownloadTemplate}
            importApi={Apis.Meter.HouseMeterHasHouses.Import}
            reload={action?.reload}
          />,
          <CompanyCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '机构',
            dataIndex: ['company', 'name'],
            search: false,
          },
          {
            title: '项目',
            dataIndex: ['asset_project', 'name'],
            search: false,
          },
          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: 'house_meter_has_houses_count',
            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.Meter.HouseMeters.Restore,
            onSoftDelete: Apis.Meter.HouseMeters.SoftDelete,
            search: false,
          }),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyButtons.View
                  title="查看"
                  onClick={() => {
                    navigate(`/meter/readings/show/${item.id}`);
                  }}
                />
                <CompanyUpdate
                  item={{ ...item, type: 'link' }}
                  title="机构"
                  reload={action?.reload}
                />
                {item.meter_type ===
                  HouseMetersMeterTypeEnum.HouseMeter.value && (
                  <MeterHasOne
                    item={{ ...item, size: 'small' }}
                    reload={action?.reload}
                    title={'绑房'}
                  />
                )}
                {item.meter_type ===
                  HouseMetersMeterTypeEnum.CommonMeter.value && (
                  <MeterHasHouse
                    item={{ ...item, size: 'small' }}
                    reload={action?.reload}
                    title={'绑房'}
                  />
                )}
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Meter.HouseMeters.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
