import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { ApprovalTemplatesTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import BatchModifyApprover from './modals/BatchModifyApprover';
import Create from './modals/Create';
import Show from './modals/Show';
import Update from './modals/Update';

export default function Index({ title = '审批模板' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="contract_types"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Approval.ApprovalTemplates.List,
          )
        }
        headerTitle={title}
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              batchModifyApprover: (
                <BatchModifyApprover
                  key="BatchModifyApprover"
                  reload={action?.reload}
                  title="批量修改审批人"
                />
              ),
              add: (
                <Create key="Create" reload={action?.reload} title={title} />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          MyColumns.EnumTag({
            title: '业务类型',
            dataIndex: 'type',
            valueEnum: ApprovalTemplatesTypeEnum,
          }),
          {
            title: '模版名称',
            dataIndex: 'name',
          },
          {
            title: '模版编码',
            dataIndex: 'code',
            search: false,
          },

          MyColumns.ToggleEnabled({
            onToggleEnabled: Apis.Approval.ApprovalTemplates.ToggleEnabled,
            search: false,
          }),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, _index, action) => (
              <MyTableActions
                actions={{
                  show: (
                    <Show item={item} reload={action?.reload} title={title} />
                  ),
                  update: (
                    <Update item={item} reload={action?.reload} title={title} />
                  ),
                  delete: (
                    <MyButtons.Delete
                      onConfirm={() =>
                        Apis.Approval.ApprovalTemplates.SoftDelete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
