import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
import { MyModal } from '@/components/MyModal';
import { Apis } from '@/gen/Apis';
import { ContractArchivesConfidentialityLevelEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useRef, useState } from 'react';
import { Selects } from './Select';

interface DataType {
  key?: React.Key;
  id?: React.Key;
}
export default function CompanySealsSelectList(
  props: MyBetaModalFormProps & {
    onChange?: (selectedRows: DataType[]) => void;
  },
) {
  const modalRef = useRef<any>();
  // const [selectedDataRow, setSelectedDataRow] = useState<any>({});
  const [getSelectedRow, setSelectedRow] = useState<any>([]);
  const rowSelection: any = {
    onChange: (selectedRowKeys: React.Key[], selectedRows: DataType[]) => {
      console.log(selectedRows, 'selectedRows[0]');
      setSelectedRow(selectedRows);
    },
    getCheckboxProps: (record: any) => ({
      disabled: record.deleted_at,
      checked: props?.item?.some((item: any) => {
        console.log(item, record);
        return item?.id === record?.id;
      }),
    }),
    defaultSelectedRowKeys: props?.item?.map((item: any) => item?.id) || [],
  };

  return (
    <MyModal
      title={'选择合同'}
      width="800px"
      myRef={modalRef}
      size="middle"
      onOpen={() => {
        setSelectedRow(props?.item || []);
        console.log(props?.item, 'props?.item?.id');
      }}
      node={
        <ProTable
          {...MyProTableProps.props}
          request={async (params, sort) =>
            MyProTableProps.request(
              params,
              sort,
              Apis.Contract.ContractArchives.List,
            )
          }
          rowSelection={{ type: 'radio', ...rowSelection }}
          options={false}
          tableAlertOptionRender={() => {
            return (
              <MyButtons.Default
                key="okSelect"
                size="middle"
                type="primary"
                onClick={() => {
                  let res: any = getSelectedRow;
                  props?.onChange?.(res);
                  modalRef.current?.close();
                }}
                title="确定选项"
              />
            );
          }}
          columns={[
            // MyColumns.ID({ search: false }),
            Selects?.AssetProjects({
              title: '关联项目',
              dataIndex: ['contract', 'asset_projects_id'],
              hidden: true,
            }),
            {
              title: '关联项目',
              dataIndex: ['contract', 'project_name'],
              search: false,
            },
            {
              title: '合同名称',
              dataIndex: 'name',
            },
            {
              title: '合同编号',
              dataIndex: 'code',
              // render: (_, item: any) => (
              //   <CodeInfo
              //     item={{
              //       ...item,
              //       type: 'link',
              //       id: item?.id,
              //     }}
              //     title={item?.code}
              //   />
              // ),
            },

            MyColumns.EnumTag({
              title: '保密等级',
              dataIndex: 'confidentiality_level',
              valueEnum: ContractArchivesConfidentialityLevelEnum,
              search: false,
            }),
            // MyColumns.EnumTag({
            //   title: '期限类型',
            //   dataIndex: 'period_type',
            //   valueEnum: ContractArchivesPeriodTypeEnum,
            //   search: false,
            // }),
            {
              title: '文件信息',
              search: false,
              render: (_, item: any) => {
                return item?.contract_archive_files?.map((res: any) => {
                  return (
                    <div key={res?.id}>
                      {res?.name}，已借:{res?.borrowed_number}份，遗失:
                      {res?.lost_number}份
                    </div>
                  );
                });
              },
            },
            // {
            //   title: '保管年限',
            //   dataIndex: 'storage_years',
            //   valueType: 'digit',
            //   search: false,
            // },
            // {
            //   title: '案卷编号',
            //   dataIndex: 'file_number',
            // },
            // {
            //   title: '文件盒编号',
            //   dataIndex: 'box_number',
            // },
            // {
            //   title: '文件柜编号',
            //   dataIndex: 'cabinet_number',
            // },
            // {
            //   title: '归档年份',
            //   dataIndex: 'archive_year',
            //   valueType: 'dateYear',
            //   search: false,
            // },
            // {
            //   title: '归档日期',
            //   dataIndex: 'archive_date',
            //   valueType: 'date',
            //   search: false,
            // },
          ]}
        />
      }
    ></MyModal>
  );
}
