import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
import { MyModal } from '@/components/MyModal';
import { Apis } from '@/gen/Apis';
import { ResourceTypesCategoryEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { type TableProps, Image } from 'antd';
import { useRef, useState } from 'react';
interface DataType {
  key: React.Key;
  id: React.Key;
  is_enabled: boolean;
}
export default function ModalsResourceSelectList(
  props: MyBetaModalFormProps & {
    onChange?: (selectedRows: DataType[]) => void;
  },
) {
  const modalRef = useRef<any>();
  // const [selectedDataRow, setSelectedDataRow] = useState<any>({});
  const [getSelectedRow, setSelectedRow] = useState<any>([]);
  const rowSelection: TableProps<any>['rowSelection'] = {
    onChange: (selectedRowKeys: React.Key[], selectedRows: DataType[]) => {
      console.log(selectedRows[0], 'selectedRows[0]');
      setSelectedRow(selectedRows[0]);
    },
    getCheckboxProps: (record: any) => ({
      disabled: record.deleted_at,
      checked: props?.item?.resources_id === record?.id,
    }),
    defaultSelectedRowKeys: [props?.item?.resources_id],
  };

  return (
    <MyModal
      title={'选择资源'}
      width="1000px"
      myRef={modalRef}
      size="middle"
      onOpen={() => {
        setSelectedRow(props?.item);
      }}
      node={
        <ProTable
          {...MyProTableProps.props}
          request={async (params, sort) =>
            MyProTableProps.request(
              {
                ...params,
                ...props?.item,
              },
              sort,
              Apis.Resource.Resources.UsableResourceList,
            )
          }
          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,
            }),
            {
              title: '名称',
              dataIndex: 'name',
            },
            MyColumns.EnumTag({
              title: '类型',
              dataIndex: 'category',
              valueEnum: ResourceTypesCategoryEnum,
            }),
            // MyColumns.EnumTag({
            //   title: '状态',
            //   dataIndex: 'reservation_status',
            //   valueEnum: ResourcesReservationStatusEnum,
            // }),
            {
              title: '项目',
              search: false,
              dataIndex: ['asset_project', 'name'],
            },
            {
              title: '楼栋',
              search: false,
              dataIndex: ['asset_building', 'name'],
            },
            {
              title: '单元',
              search: false,
              dataIndex: ['asset_unit', 'name'],
            },
            {
              title: '封面',
              search: false,
              render: (_, item: any) => {
                return <Image src={item?.cover_image?.[0]?.url} height={50} />;
              },
            },
          ]}
        />
      }
    ></MyModal>
  );
}
