import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import { useEffect, useRef } from 'react';
import GridCreate from '../modals/GridCreate';
import GridCreateUpdate from '../modals/GridUpdate';

export default function Index({ ...rest }) {
  const navigate = useNavigate();
  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, asset_projects_id: rest.item?.asset_projects_id },
            sort,
            Apis.Grid.Grids.List,
          )
        }
        toolBarRender={(action) => [
          <GridCreate key="Create" item={rest.item} reload={action?.reload} />,
        ]}
        search={false}
        columns={[
          {
            title: '网格ID',
            dataIndex: 'id',
          },
          {
            title: '网格名称',
            dataIndex: 'name',
          },
          {
            title: '网格标识',
            dataIndex: 'grid_mark',
          },
          MyColumns.CreatedAt(),
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <GridCreateUpdate item={item} reload={action?.reload} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Asset.AssetBuildings.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
