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

export default function Index(props: MyBetaModalFormProps) {
  const navigate = useNavigate();
  return (
    <>
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, house_meters_id: props?.item?.id },
            sort,
            Apis.Meter.HouseMeterHasHouses.List,
          )
        }
        toolBarRender={(action) => [
          ...(props?.item?.meter_type ===
          HouseMetersMeterTypeEnum.HouseMeter.value
            ? [
                <MeterHasOne
                  key="MeterHasOne"
                  item={{ ...props?.item, size: 'middle' }}
                  reload={action?.reload}
                  title={'绑房'}
                />,
              ]
            : []),
          ...(props?.item?.meter_type ===
          HouseMetersMeterTypeEnum.CommonMeter.value
            ? [
                <MeterHasHouse
                  key="MeterHasHouse"
                  item={{ ...props?.item, size: 'middle' }}
                  reload={action?.reload}
                  title={'绑房'}
                />,
              ]
            : []),
        ]}
        search={false}
        // options={false}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          {
            title: '项目名称',
            dataIndex: 'project_name',
            search: false,
          },
          {
            title: '楼栋名称',
            dataIndex: 'building_name',
            search: false,
          },
          {
            title: '单元名称',
            dataIndex: 'unit_name',
            search: false,
          },
          {
            title: '房号',
            dataIndex: 'name',
            search: false,
          },
          {
            title: '房屋名称',
            dataIndex: 'full_name',
            search: false,
            render: (_, record) => {
              return (
                <MyButtons.View
                  title={`${record.full_name}`}
                  type="link"
                  onClick={() => {
                    navigate(
                      `/customer/archive/show/${record.asset_houses_id}`,
                    );
                  }}
                />
              );
            },
          },
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Meter.HouseMeterHasHouses.Delete({ id: item.id }).then(
                      () => action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
