import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Image, Space } from 'antd';
import BrandsCreate from './modals/BrandsCreate';
import BrandUpdate from './modals/BrandUpdate';

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

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

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company-brands"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Company.CompanyPropertyBrands.List,
          )
        }
        toolBarRender={(action) => [
          <BrandsCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '机构',
            dataIndex: ['company', 'name'],
            width: 300,
          },
          {
            title: '品牌名称',
            dataIndex: 'name',
            width: 200,
          },
          {
            title: 'logo',
            search: false,
            render: (_, item) => {
              return (
                <Space>
                  {item?.logo?.[0] && (
                    <Image
                      height={30}
                      src={item?.logo[0]?.url}
                      placeholder="正面"
                    />
                  )}
                </Space>
              );
            },
            width: 100,
          },
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <BrandUpdate
                  item={{ ...item, companies_id: item?.id }}
                  reload={action?.reload}
                  title="品牌"
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Company.CompanyPropertyBrands.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
