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

export default function CompanyApps(props: MyBetaModalFormProps) {
  return (
    <>
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, companies_id: props?.item?.id },
            sort,
            Apis.Company.CompanyApps.List,
          )
        }
        toolBarRender={(action) => [
          <AppCreate
            key="Create"
            reload={action?.reload}
            item={props?.item}
            title="应用配置"
          />,
        ]}
        search={false}
        // options={false}
        columns={[
          MyColumns.ID(),

          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>
            ),
          }),
        ]}
      />
    </>
  );
}
