import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  useCurrentPermissions,
} 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';
import ResetReading from './modals/ResetReading';

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

  let tableRender = (item: any, action: any) => {
    return getCurrentPermissions({
      view: (
        <MyButtons.View
          title="查看"
          onClick={() => {
            navigate(`/instrument/readings/show/${item.id}`);
          }}
        />
      ),
      reading: (
        <NormalReading
          item={{ ...item, size: 'small' }}
          title="抄表"
          reload={action?.reload}
        />
      ),
      reset: (
        <ResetReading
          item={{ ...item, title: '重置', size: 'small' }}
          title="重置"
          reload={action?.reload}
        />
      ),
    });
  };
  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={() => [
          // <ReadingCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        headerTitle="抄表数据"
        columns={[
          MyColumns.ID({ 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: 'initial_value',
            search: false,
          },
          {
            title: '最新抄表读数',
            dataIndex: 'latest_value',
            search: false,
          },
          {
            title: '初始抄表时间',
            dataIndex: 'initial_time',
            search: false,
          },
          {
            title: '最新抄表时间',
            dataIndex: 'latest_time',
            search: false,
          },

          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <>{tableRender(item, action)}</>

                {/* <NormalReading
                  item={{ ...item, size: 'small' }}
                  title="抄表"
                  reload={action?.reload}
                />
                <ResetReading
                  item={{ ...item, title: '重置', size: 'small' }}
                  title="重置"
                  reload={action?.reload}
                /> */}
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
