import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
import { MyModal } from '@/components/MyModal';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { message, 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 [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]);
    },
    defaultSelectedRowKeys: props?.item?.id ? [props?.item?.id] : [],
  };

  return (
    <MyModal
      title={'选择签约主体'}
      width="1000px"
      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.Company.CompanySuppliers.List,
            )
          }
          rowSelection={{ type: 'radio', ...rowSelection }}
          options={false}
          tableAlertOptionRender={() => {
            return (
              <MyButtons.Default
                key="okSelect"
                size="middle"
                type="primary"
                onClick={() => {
                  let res: any = getSelectedRow;
                  if (!res?.legal_person) {
                    message.error('当前主体没有设置法人信息！');
                    return;
                  }
                  props?.onChange?.(res);
                  modalRef.current?.close();
                }}
                title="确定选项"
              />
            );
          }}
          columns={[
            MyColumns.ID({
              search: false,
            }),
            {
              title: '供应商名称',
              dataIndex: 'name',
            },
            {
              title: '公司名称',
              dataIndex: 'company_name',
              search: false,
            },
            {
              title: '法人',
              dataIndex: 'legal_person',
            },
            {
              title: '纳税人号',
              dataIndex: 'tax_number',
            },
            {
              title: '注册地址',
              search: false,
              render: (_, item: any) => {
                return `${item.province}${item.city}${item.area}${item.street}${item.address}`;
              },
            },
          ]}
        />
      }
    ></MyModal>
  );
}
