import { MyBetaModalFormProps, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseMeterReadingsOperationTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import CorrectReading from '../modals/CorrectReading';
import NormalReading from '../modals/NormalReading';
import ResetReading from '../modals/ResetReading';

export default function Index(props: MyBetaModalFormProps) {
  return (
    <>
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, house_meters_id: props?.item?.id },
            sort,
            Apis.Meter.HouseMeterReadings.List,
          )
        }
        toolBarRender={(action) => [
          <NormalReading
            key="Create"
            reload={action?.reload}
            item={props?.item}
            title="抄表"
          />,
          <ResetReading
            key="Reset"
            reload={action?.reload}
            item={props?.item}
            title="重置"
          />,
        ]}
        // search={false}
        // options={false}
        columns={[
          MyColumns.EnumTag({
            title: '操作类型',
            dataIndex: 'operation_type',
            valueEnum: HouseMeterReadingsOperationTypeEnum,
          }),
          {
            title: '抄表读数',
            dataIndex: 'current_reading',
            search: false,
          },
          {
            title: '抄表用量',
            dataIndex: 'usage_amount',
            search: false,
          },
          {
            title: '抄表时间',
            dataIndex: 'reading_time',
            search: false,
          },
          {
            title: '抄表人',
            dataIndex: ['company_employee', 'name'],
            search: false,
          },
          {
            title: '备注',
            dataIndex: 'remark',
            search: false,
          },
          {
            title: '账单状态',
            dataIndex: 'bill_status',
            render: (value) => (value ? '已生成' : '未生成'),
            search: false,
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                {/* 只有未生成账单的抄表记录,且不是重置读数，才显示修正按钮 */}
                {!item?.bill_status &&
                  item?.operation_type !==
                    HouseMeterReadingsOperationTypeEnum.ResetReading.value && (
                    <CorrectReading item={item} reload={action?.reload} />
                  )}
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
