import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
import { MyModal } from '@/components/MyModal';
import { Apis } from '@/gen/Apis';
import { QuestionsTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { type TableProps } from 'antd';
import { useRef, useState } from 'react';
interface DataType {
  key: React.Key;
  id: React.Key;
  is_enabled: boolean;
}
export default function SurveysSelectList(
  props: MyBetaModalFormProps & {
    onChange?: (selectedRows: DataType[]) => void;
  },
) {
  const modalRef = useRef<any>();
  const [getSelectedRow, setSelectedRow] = useState<any>([]);
  const rowSelection: TableProps<any>['rowSelection'] = {
    onChange: (selectedRowKeys: React.Key[], selectedRows: DataType[]) => {
      setSelectedRow(selectedRows);
    },
    getCheckboxProps: (record: DataType) => ({
      disabled: !record.is_enabled, // Column configuration not to be checked
      checked: props?.item?.list?.some((item: any) => {
        console.log(item, record);
        return item?.id === record?.id;
      }),
    }),
    defaultSelectedRowKeys:
      props?.item?.list?.map((item: any) => item?.id) || [],
  };
  return (
    <MyModal
      title={'添加题目'}
      width="1000px"
      myRef={modalRef}
      size="middle"
      onOpen={() => {
        setSelectedRow(props?.item?.list);
        console.log(props?.item?.list);
      }}
      node={
        <ProTable
          {...MyProTableProps.props}
          request={async (params, sort) =>
            MyProTableProps.request(params, sort, Apis.Survey.Questions.List)
          }
          rowSelection={{ type: 'checkbox', ...rowSelection }}
          options={false}
          tableAlertOptionRender={() => {
            return (
              <MyButtons.Default
                key="okSelect"
                size="middle"
                type="primary"
                onClick={() => {
                  getSelectedRow?.forEach((item: any) => {
                    item.question_id = item?.id;
                  });
                  props?.onChange?.(getSelectedRow);
                  modalRef.current?.close();
                }}
                title="确定选项"
              />
            );
          }}
          columns={[
            MyColumns.ID({
              search: false,
            }),
            { dataIndex: 'title', title: '题目' },
            MyColumns.EnumTag({
              title: '类型',
              dataIndex: 'type',
              valueEnum: QuestionsTypeEnum,
            }),
            { dataIndex: 'placeholder', title: '提示', search: false },
            MyColumns.Boolean({
              dataIndex: 'required',
              title: '是否必答',
              search: false,
            }),
            MyColumns.Boolean({
              dataIndex: 'is_enabled',
              title: '是否启用',
            }),
          ]}
        />
      }
    ></MyModal>
  );
}
