import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { CompanyAppsAppTypeEnum, CompanyAppsModuleEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import AppCreate from './modals/AppCreate';
import AppUpdate from './modals/AppUpdate';

export default function Index({ title = '应用管理' }) {
  const navigate = useNavigate();

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

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company-apps"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Company.CompanyApps.List)
        }
        toolBarRender={(action) => [
          <AppCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '机构',
            dataIndex: ['company', 'name'],
          },
          MyColumns.EnumTag({
            title: '应用类型',
            dataIndex: 'app_type',
            valueEnum: CompanyAppsAppTypeEnum,
          }),
          MyColumns.EnumTag({
            title: '模块',
            dataIndex: 'module',
            valueEnum: CompanyAppsModuleEnum,
            search: false,
          }),
          {
            title: '应用ID',
            dataIndex: 'app_id',
            search: false,
            ellipsis: true,
          },
          // {
          //   title: '应用密钥',
          //   dataIndex: 'app_secret',
          //   search: false,
          //   ellipsis: true,
          //   // render: (text) => '****' + String(text).slice(-4),
          // },

          MyColumns.UpdatedAt(),
          // MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <AppUpdate
                  item={item}
                  reload={action?.reload}
                  title="应用配置"
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Company.CompanyApps.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
