import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
  useCurrentPermissions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseMetersMeterTypeEnum,
  HouseMetersUsageTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import MyInstrumentCreate from '../modals/InstrumentCreate';

export default function Index(props: MyBetaModalFormProps) {
  const navigate = useNavigate();
  const getCurrentPermissions = useCurrentPermissions();
  let toolBarRender = (action: any) => {
    return getCurrentPermissions(
      {
        create: (
          <MyInstrumentCreate
            key="Create"
            reload={action?.reload}
            item={props?.item}
            title="仪表"
          />
        ),
      },
      '/meter',
    );
  };
  let tableRender = (item: any, action: any) => {
    return getCurrentPermissions(
      {
        view: (
          <MyButtons.View
            title="查看"
            onClick={() => {
              navigate(`/meter/readings/show/${item.id}`);
            }}
          />
        ),
        delete: (
          <MyButtons.Delete
            onConfirm={() =>
              Apis.Meter.HouseMeters.Delete({ id: item.id }).then(() =>
                action?.reload(),
              )
            }
          />
        ),
      },
      '/meter',
    );
  };
  return (
    <ProTable
      {...MyProTableProps.props}
      headerTitle="已绑定仪表"
      request={async (params, sort) =>
        MyProTableProps.request(
          {
            ...params,
            asset_projects_id: props?.item?.asset_projects_id,
            usage_type: props?.item?.usage_type,
            meter_type: props?.item?.meter_type,
          },
          sort,
          Apis.Meter.HouseMeters.List,
        )
      }
      toolBarRender={(action) => [toolBarRender(action)]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '项目',
          dataIndex: ['asset_project', 'name'],
          // search: { transform: (value) => ({ project_name: value }) },
          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: 'charge_standard_name',
        //   hideInTable: true,
        //   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.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>
              <>{tableRender(item, action)}</>
            </Space>
          ),
        }),
      ]}
    />
  );
}
