import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';

import { HouseBillsTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useEffect, useRef } from 'react';
import ChargeStandardHasHouse from '../modals/ChargeStandardHasHouse';

export default function Index({ ...rest }) {
  const actionLooks = useRef<any>();
  useEffect(() => {
    actionLooks?.current.reloadAndRest();
  }, [rest.loadmore]);

  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        actionRef={actionLooks}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              house_charge_standards_id: rest.item?.house_charge_has_houses_id,
            },
            sort,
            Apis.HouseCharage.HouseChargeHasHouses.List,
          )
        }
        toolBarRender={(action) => [
          (rest?.item.charge_type === HouseBillsTypeEnum.PropertyFee.value ||
            rest?.item.charge_type ===
              HouseBillsTypeEnum.MaintenanceFund.value) && (
            <ChargeStandardHasHouse
              item={rest.item}
              reload={action?.reload}
              title={'绑房'}
            />
          ),
        ]}
        search={false}
        columns={[
          MyColumns.ID(),
          {
            title: '房屋名称',
            dataIndex: 'full_name',
          },
          {
            title: '项目名称',
            dataIndex: 'project_name',
          },
          {
            title: '楼栋名称',
            dataIndex: 'building_name',
          },
          {
            title: '单元名称',
            dataIndex: 'unit_name',
          },
          {
            title: '房号',
            dataIndex: 'name',
          },

          {
            title: '绑定时间',
            dataIndex: 'updated_at',
            search: false,
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.HouseCharage.HouseChargeHasHouses.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
