import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  ApprovalInstancesStatusEnum,
  ApprovalTemplatesTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Audit from './modals/Audit';
export default function Index({ title = '我的待办' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="my_apply_list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              status: ApprovalInstancesStatusEnum.Pending.value,
            },
            sort,
            Apis.Approval.ApprovalInstances.PendingList,
          )
        }
        headerTitle={title}
        columns={[
          MyColumns.ID({ search: false }),
          {
            title: '申请事项',
            dataIndex: ['approval_instance', 'title'],
            search: false,
          },
          {
            title: '关联项目',
            dataIndex: ['approval_instance', 'asset_project', 'name'],
            search: false,
          },
          MyColumns.EnumTag({
            title: '状态',
            dataIndex: ['approval_instance', 'status'],
            valueEnum: ApprovalInstancesStatusEnum,
          }),
          MyColumns.EnumTag({
            dataIndex: ['approval_instance', 'type'],
            title: '业务类型',
            valueEnum: ApprovalTemplatesTypeEnum,
            search: false,
          }),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <Audit item={item} reload={action?.reload} title={title} />
                <MyButtons.Default
                  title="撤销"
                  isConfirm
                  description="确认撤销该申请吗？"
                  disabled={item.status !== 'Pending'}
                  color="danger"
                  variant="solid"
                  onConfirm={() =>
                    Apis.Approval.ApprovalInstances.Cancel({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
