import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import GridCreate from './modals/GridCreate';
import GridMannger from './modals/GridMannger';

import GridUpdate from './modals/GridUpdate';

export default function Index({ title = '楼栋管家' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="grids"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="楼栋划分"
        tooltip="楼栋划分的信息管理，请在【项目列表】的【配置】中进行操作，影响住宅的房屋账单管理"
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Grid.Grids.List)
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              select: (
                <GridCreate
                  key="Select"
                  reload={action?.reload}
                  title="楼栋划分"
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '项目名称',
            dataIndex: ['asset_project', 'name'],
            search: {
              transform: (value) => {
                return { project_name: value };
              },
            },
          },
          {
            title: '板块名称',
            dataIndex: 'name',
          },
          {
            title: '楼栋管家',
            dataIndex: ['company_employee', 'name'],
            render: (_, item: any) =>
              `${item?.company_employee?.name || ''}-${
                item?.company_employee?.phone || ''
              }`,
            search: { transform: (value) => ({ employee_name: value }) },
          },
          // MyColumns.CreatedAt(),
          // MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyTableActions
                  actions={{
                    update: <GridUpdate item={item} reload={action?.reload} />,
                    mannger: (
                      <GridMannger item={item} reload={action?.reload} />
                    ),
                    delete: (
                      <MyButtons.Delete
                        onConfirm={() =>
                          Apis.Grid.Grids.Delete({ id: item.id }).then(() =>
                            action?.reload(),
                          )
                        }
                      />
                    ),
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
