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

type Props = {
  onChange: (id: FilterOptionResp) => void
  typeName: string
  dataValue: InfoList[]
}

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

  useEffect(() => {
    setData(props.dataValue || [])
  }, [])

  const onClick = (item: FilterOptionResp) => {
    props.onChange({
      id: item.id,
      name: `${props.typeName}：` + item.name,
    })
  }

  return (
    <div className={styles.filterItem}>
      <div className={styles.filterItemTitle}>{props.typeName}：</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>
  )
}
