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

export default function Index({ title = '楼栋范围' }) {
  const navigate = useNavigate();

  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'grids',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="grids"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Grid.Grids.List)
        }
        toolBarRender={(action) => [
          <GridCreate key="Select" reload={action?.reload} title="楼栋划分" />,
        ]}
        columns={[
          {
            title: '关联项目',
            dataIndex: ['asset_project', 'name'],
            search: {
              transform: (value) => {
                return { project_name: value };
              },
            },
          },
          {
            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}>
                <GridUpdate 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>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
