import { Input, ScrollView, Text, View } from '@tarojs/components'
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import classnames from 'classnames'
import styles from './index.module.scss'
import Popup from '@/components/popup'
import TextareaEnhance from '@/components/textareaEnhance'
import { CreateFavoriteApi, FavoriteListApi } from '@/api/favorite'
import { alert, goLink } from '@/common/common'
import { getFilterData } from '@/common/util'

// 原因选择
interface ReasonInfoParam {
  show?: boolean // 显示
  onClose?: () => void // 关闭
  onAdd?: (val: any) => void
}
const AddCollection = ({ show = false, onClose, onAdd }: ReasonInfoParam) => {
  // 获取列表
  const [list, setList] = useState([])
  const { fetchData: fetchDataList } = FavoriteListApi()
  const getFavoriteList = async() => {
    const res = await fetchDataList(getFilterData())
    setList(() => res.data.list)
  }
  useEffect(() => {
    if (show) { getFavoriteList() }
  }, [show])

  const onCreate = () => {
    onClose?.()
    goLink('/pages/collection/index')
  }

  return (
    <Popup show={show} onClose={onClose} showTitle={false}>
      <View className={styles.collection_con}>
        <View className={styles.header}>
          <Text className={styles.title}>选择收藏夹</Text>
          <View className={styles.miconfont_con}>
            <Text className={classnames(styles.miconfont, 'iconfont icon-jia')}></Text>
            <Text onClick={onCreate}>新建</Text>
          </View>
        </View>
        <ScrollView scrollY className={styles.scrollView}>
          {list?.map((item: any, key) => (
            <View onClick={() => onAdd?.(item)} className={styles.collection_item} key={key}>
              <View className={styles.name}>
                {item.name}
                <Text>（{item.product_color_list?.length || 0}）</Text>
              </View>
              <View className={styles.desc}>{item.remark}</View>
            </View>
          ))}
        </ScrollView>
      </View>
    </Popup>
  )
}
export default memo(AddCollection)
