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

export default function Index({ title = '岗位管理' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company-positions"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="岗位列表"
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Company.CompanyPositions.List,
          )
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              add: (
                <PositionCreate
                  key="Create"
                  reload={action?.reload}
                  title={title}
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          {
            title: '岗位名称',
            dataIndex: 'name',
            width: 200,
          },
          {
            title: '岗位编号',
            dataIndex: 'code',
            search: false,
            width: 200,
          },
          {
            title: '岗位说明',
            dataIndex: 'remark',
            search: false,
            width: 200,
          },
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyTableActions
                  actions={{
                    update: (
                      <PositionUpdate
                        item={item}
                        reload={action?.reload}
                        title="岗位"
                      />
                    ),
                    delete: (
                      <MyButtons.Delete
                        onConfirm={() =>
                          Apis.Company.CompanyPositions.Delete({
                            id: item.id,
                          }).then(() => action?.reload())
                        }
                      />
                    ),
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
