import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { OrganizationsTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import OrganizationChange from './modals/OrganizationChange';
import OrganizationCreate from './modals/OrganizationCreate';
import OrganizationUpdate from './modals/OrganizationUpdate';

export default function Index({ title = '组织列表' }) {
  const navigate = useNavigate();

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

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company-organizations"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Company.Organizations.TreeList,
          )
        }
        toolBarRender={(action) => [
          <OrganizationCreate
            key="Create"
            reload={action?.reload}
            title={'组织'}
          />,
        ]}
        columns={[
          {
            title: '机构',
            dataIndex: ['company', 'name'],
            search: false,
          },
          {
            title: '组织ID',
            dataIndex: 'id',
            search: false,
          },
          {
            title: '组织名称',
            dataIndex: 'name',
          },
          MyColumns.EnumTag({
            title: '组织类型',
            dataIndex: 'type',
            valueEnum: OrganizationsTypeEnum,
            search: false,
          }),

          {
            title: '上级组织',
            dataIndex: ['organization_parent', 'name'],
            search: false,
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <OrganizationUpdate
                  item={{ ...item, companies_id: item?.id }}
                  reload={action?.reload}
                  title="组织"
                />
                <OrganizationChange
                  item={{ ...item, companies_id: item?.id }}
                  reload={action?.reload}
                  title="组织"
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Company.Organizations.Delete({ id: item.id }).then(
                      () => action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
