import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { ApprovalInstancesStatusEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useSearchParams } from '@umijs/max';
import Create from './modals/Create';
import Show from './modals/Show';
import Update from './modals/Update';

export default function Index({ title = '其他用印' }) {
  const [searchParams, setSearchParams]: any = useSearchParams();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="contracts_list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) => {
          setSearchParams(params);
          return MyProTableProps.request(
            !searchParams?.get('page')
              ? {
                  ...params,
                }
              : params,
            sort,
            Apis.Contract.ContractOthers.List,
          );
        }}
        headerTitle="其他用印列表"
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              add: (
                <Create key="Create" reload={action?.reload} title={title} />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '关联项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          MyColumns.EnumTag({
            title: '审核状态',
            dataIndex: 'status',
            valueEnum: ApprovalInstancesStatusEnum,
          }),
          {
            title: '用印事宜',
            dataIndex: 'title',
            search: false,
          },
          {
            title: '申请说明',
            dataIndex: 'reason',
            search: false,
          },
          // MyColumns.EnumTag({
          //   title: '合同性质',
          //   dataIndex: 'contract_nature',
          //   valueEnum: ContractsContractNatureEnum,
          //   search: false,
          // }),

          // {
          //   title: '关联项目',
          //   dataIndex: ['asset_project', 'name'],
          //   search: false,
          // },
          MyColumns.Boolean({
            title: '是否用印',
            dataIndex: 'is_sealed',
          }),
          //company_employee
          {
            title: '申请人',
            dataIndex: 'company_employee',
            render: (_, item: any) => {
              return `${item.company_employee?.name || '-'}${
                item.company_employee?.phone || ''
              }`;
            },
            search: false,
          },
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, __, action) => (
              <MyTableActions
                actions={{
                  show: <Show key="Show" item={item} reload={action?.reload} />,
                  update: (
                    <Update
                      key="Update"
                      item={item}
                      reload={action?.reload}
                      title={title}
                    />
                  ),
                  useSeal: (
                    <MyButtons.Default
                      key="UseSeal"
                      title="登记"
                      type="primary"
                      disabled={item?.status !== 'Approved' || item?.is_sealed}
                      isConfirm={true}
                      description={`是否确认已用印？`}
                      onConfirm={() =>
                        Apis.Contract.ContractOthers.Seal({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
