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

export default function Index({ title = '合同类型' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="contract_types"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Contract.ContractTypes.List,
          )
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              add: <Create key="Create" reload={action?.reload} title={title} />,
            }}
          />,
        ]}
        search={false}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          {
            title: '名称',
            dataIndex: 'name',
          },
          MyColumns.Boolean({
            dataIndex: 'is_enabled',
            title: '是否启用',
            search: false,
          }),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, _index, action) => (
              <MyTableActions
                actions={{
                  update: (
                    <Update item={item} reload={action?.reload} title={title} />
                  ),
                  delete: (
                    <MyButtons.Delete
                      onConfirm={() =>
                        Apis.Contract.ContractTypes.Delete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
