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

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, asset_projects_id: rest.item?.id },
            sort,
            Apis.Grid.Grids.List,
          )
        }
        toolBarRender={(action) => [
          <GridCreate
            key="Create"
            item={rest.item}
            reload={action?.reload}
            title="单元划分"
          />,
        ]}
        search={false}
        columns={[
          {
            title: 'ID',
            dataIndex: 'id',
          },
          {
            title: '板块名称',
            dataIndex: 'name',
          },
          {
            title: '板块标识',
            dataIndex: 'grid_mark',
          },
          {
            title: '楼栋管家',
            dataIndex: ['company_employee', 'name'],
            render: (_, item: any) =>
              `${item?.company_employee?.name || ''}-${
                item?.company_employee?.phone || ''
              }`,
          },
          // MyColumns.CreatedAt(),
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <GridCreateUpdate item={item} reload={action?.reload} />
                <GridMannger item={item} reload={action?.reload} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Grid.Grids.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
