import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  ContractArchivesConfidentialityLevelEnum,
  ContractArchivesPeriodTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import Save from '../modals/Save';
import Show from '../modals/Show';
import TransfersCreate from '../modals/TransfersCreate';
import Update from '../modals/Update';

export default function Index({ title = '归档记录' }) {
  const navigate = useNavigate();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="archives_finished"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Contract.ContractArchives.List,
          )
        }
        headerTitle="归档记录"
        toolBarRender={(action) => [
          <MyButtons.Default
            key="back"
            title="返回未归档"
            type="primary"
            size="middle"
            onClick={() => {
              navigate('/contract/contract_archives');
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          {
            title: '归档编号',
            dataIndex: 'code',
          },
          {
            title: '合同名称',
            dataIndex: 'name',
          },
          MyColumns.EnumTag({
            title: '保密等级',
            dataIndex: 'confidentiality_level',
            valueEnum: ContractArchivesConfidentialityLevelEnum,
            search: false,
          }),
          MyColumns.EnumTag({
            title: '期限类型',
            dataIndex: 'period_type',
            valueEnum: ContractArchivesPeriodTypeEnum,
            search: false,
          }),
          {
            title: '保管年限',
            dataIndex: 'storage_years',
            valueType: 'digit',
            search: false,
          },
          {
            title: '文件盒编号',
            dataIndex: 'box_number',
          },
          {
            title: '文件柜编号',
            dataIndex: 'cabinet_number',
          },
          {
            title: '归档日期',
            dataIndex: 'archive_date',
            valueType: 'date',
            sorter: true,
            search: false,
          },
          {
            title: '保管人',
            dataIndex: 'keeper_name',
            search: false,
          },
          {
            title: '文件信息',
            render: (_, item: any) => {
              return item?.archive_files?.map((res: any, index: number) => {
                return (
                  <div key={`item_${index}`}>
                    文件：{res?.name}，已借:{res?.borrowed_number}份，遗失:
                    {res?.lost_number}份
                  </div>
                );
              });
            },
          },
          MyColumns.Option({
            render: (_, item: any, _index, action) => (
              <Space size="small">
                <Show item={item} />
                <Update item={item} reload={action?.reload} title="编辑" />
                <Save item={item} reload={action?.reload} title="封存" />
                <TransfersCreate
                  item={item}
                  reload={action?.reload}
                  title="转交"
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Contract.ContractArchives.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
