import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { MyExport } from '@/components/MyExport';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import MyCreate from './modals/MyCreate';
import MyUpdate from './modals/MyUpdate';

export default function Index({ title = '页面标题' }) {
  const navigate = useNavigate();
  const [params, setParams] = useState({});

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="example-list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (query, sort) => {
          setParams(query);
          return MyProTableProps.request(
            query,
            sort,
            Apis.Module.Resource.List,
          );
        }}
        headerTitle="列表标题"
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              export: (
                <MyExport
                  key="export"
                  item={params}
                  download={Apis.Module.Resource}
                />
              ),
              add: (
                <MyCreate key="create" reload={action?.reload} title={title} />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '名称',
            dataIndex: 'name',
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyTableActions
                  actions={{
                    view: (
                      <MyButtons.View
                        key="view"
                        title="详情"
                        onClick={() => {
                          navigate(`/module/resource/show/${item.id}`);
                        }}
                      />
                    ),
                    update: (
                      <MyUpdate
                        key="update"
                        item={item}
                        title={title}
                        reload={action?.reload}
                      />
                    ),
                    delete: (
                      <MyButtons.Delete
                        key="delete"
                        onConfirm={() =>
                          Apis.Module.Resource.Delete({ id: item.id }).then(
                            () => action?.reload(),
                          )
                        }
                      />
                    ),
                  }}
                  maxVisible={2}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
