import { Space, Button, Select, Collapse } from 'antd';
import styles from '../../index.module.scss';
import api, { FilterOptionResp } from "../../api";
import { useState, useEffect } from 'react';

type Props = {
  onChange: (id: FilterOptionResp) => void;
};

export default function CategoryItem(props: Props) {
  const [data, setData] = useState<FilterOptionResp[]>([]);

  useEffect(() => {
    api.deviceModel().then((res) => {
      setData(res?.result || []);
    });
  }, []);

  const onClick = (item: FilterOptionResp) => {
    props.onChange({
      id: item.id,
      name: "型号：" + item.name,
    });
  };

  return (
    <div className={styles.filterItem}>
      <div className={styles.filterItemTitle}>型号：</div>
      <div
        className={`${styles.filterItemMain} ${
          data.length <= 10 && styles.disabled
        }`}
      >
        <Collapse ghost collapsible="icon" expandIconPosition="end">
          <Collapse.Panel
            header={
              <Space size={40}>
                {data.slice(0, 10).map((item) => {
                  return (
                    <Button
                      type="link"
                      key={item.id}
                      onClick={(e) => onClick(item)}
                    >
                      {item.name}
                    </Button>
                  );
                })}
              </Space>
            }
            key="1"
          >
            <Space size={40}>
              {data.slice(10).map((item) => {
                return (
                  <Button
                    type="link"
                    key={item.id}
                    onClick={(e) => onClick(item)}
                  >
                    {item.name}
                  </Button>
                );
              })}
            </Space>
          </Collapse.Panel>
        </Collapse>
      </div>
    </div>
  );
}