import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { CompanyReceiptAccountsPayChannelEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import ReceiptAccountCreate from './modals/ReceiptAccountCreate';
import ReceiptAccountUpdate from './modals/ReceiptAccountUpdate';

export default function Index({ title = '账号管理' }) {
  const navigate = useNavigate();
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company_receipt_accounts"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="账号管理（关联项目可选收款账号）"
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Company.CompanyReceiptAccounts.List,
          )
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              add: (
                <ReceiptAccountCreate
                  key="Create"
                  reload={action?.reload}
                  title={title}
                />
              ),
              config: (
                <MyButtons.Default
                  key="click"
                  type="primary"
                  onClick={() => {
                    navigate('/asset/accounts');
                  }}
                  size="middle"
                  title="去配置项目账号"
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          {
            title: '收款账户',
            dataIndex: 'company_name',
          },
          {
            title: '开户行',
            dataIndex: 'company_bank',
            search: false,
          },
          {
            title: '收款账号',
            dataIndex: 'company_account',
            search: false,
          },
          MyColumns.EnumTag({
            title: '收款渠道',
            dataIndex: 'pay_channel',
            valueEnum: CompanyReceiptAccountsPayChannelEnum,
            search: false,
          }),
          {
            title: '商户ID',
            dataIndex: 'merchant_id',
            search: false,
            render: (_, record) => record?.merchant_id || '-',
          },
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyTableActions
                  actions={{
                    update: (
                      <ReceiptAccountUpdate
                        item={item}
                        reload={action?.reload}
                        title="收款账号"
                      />
                    ),
                    delete: (
                      <MyButtons.Delete
                        onConfirm={() =>
                          Apis.Company.CompanyReceiptAccounts.Delete({
                            id: item.id,
                          }).then(() => action?.reload())
                        }
                      />
                    ),
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
