import { Button, Image, Input, RichText, ScrollView, Text, View } from '@tarojs/components'
import Taro, { useDidShow, usePullDownRefresh, useReady } from '@tarojs/taro'
import classnames from 'classnames'
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import Goods from '../goods'
import styles from './index.module.scss'
import Popup from '@/components/popup'
import { GetProductKindListApi } from '@/api/material'
import { getFilterData } from '@/common/util'
import { alert, goLink } from '@/common/common'
import { Mallproductinstruct } from '@/api/favorite'

interface Props {
  showPopup: boolean
  closePopup: () => void
  productName?: string
  productIds?: number
}

const FeaturePopup = (param: Props) => {
  const { productIds, showPopup } = param
  const { fetchData } = Mallproductinstruct()

  const [nodes, setnodes] = useState<string>('')

  const [list, setlist] = useState<any>([])
  const getRich = async() => {
    Taro.showLoading({
      title: '加载中...',
    })

    const res = await fetchData({ product_id: productIds })
    if (res.data) {
      setnodes(res.data.instructions)
      setlist([...res.data.product_list])
      Taro.hideLoading()
    }
    else {
      Taro.hideLoading()
    }
  }

  useEffect(() => {
    if (showPopup) {
      getRich()
    }
  }, [showPopup])

  return (
    <Popup showTitle={false} show={param.showPopup} onClose={() => param.closePopup?.()}>
      <ScrollView scrollY className={styles.sollBox}>
        <View className={styles.main}>
          <View className={styles.title}>{param.productName}</View>
          <View className={styles.line}></View>
          <RichText nodes={nodes}></RichText>
          <View className={styles.topFont}>推荐面料</View>
          {
            list.map((item, index) => {
              return (
                <Goods clickItem={() => goLink(`/pages/details/index?id=${item.id}`)} key={index} data={item}></Goods>
              )
            })
          }
        </View>
      </ScrollView>
    </Popup>
  )
}

export default memo(FeaturePopup)
