import { Input, ScrollView, Text, View } from '@tarojs/components'
import { memo, useCallback, useMemo, useRef } from 'react'
import classnames from 'classnames'
import styles from './index.module.scss'
import Popup from '@/components/popup'
import TextareaEnhance from '@/components/textareaEnhance'
import { CreateFavoriteApi, MoveFavoriteProductApi } from '@/api/favorite'
import { alert, goLink } from '@/common/common'
import LabAndImg from '@/components/LabAndImg'
import IconFont from '@/components/iconfont/iconfont'
import Divider from '@/components/divider'

// 原因选择
interface ReasonInfoParam {
  show?: boolean // 显示
  onClose?: () => void // 关闭
  collectionList: any[]
  ids?: any[]
  sourceId: number
  onSuccess?: () => void
}
const MoveCollectionPopup = (props: ReasonInfoParam) => {
  const {
    show = false, onClose, collectionList, ids, sourceId, onSuccess,
  } = props
  // 移动面料
  const { fetchData: fetchDataMoveFavoriteProduct } = MoveFavoriteProductApi()
  const handleMove = async(val) => {
    if (ids?.length == 0) { return alert.none('请选择要移动面料') }
    const res = await fetchDataMoveFavoriteProduct({
      source_favorite_id: sourceId,
      target_favorite_id: val.id,
      product_id: Array.from(ids!),
    })
    if (res.success) {
      onClose?.()
      onSuccess?.()
      alert.success('移动成功')
    }
    else {
      alert.error('移动失败')
    }
  }
  const handleNewCollection = () => {
    // 新建收藏夹
    goLink('/pages/collection/collectionDetail/index', { title: '新建收藏夹', isEdit: false, editData: null })
  }
  return (
    <Popup show={show} onClose={onClose} showTitle title="移动收藏夹" >
      <Divider direction="horizontal" customStyles={{ marginTop: '0px', marginBottom: '12px' }}></Divider>
      <ScrollView className={styles.scrollStyle} scrollY>
        <View className={styles.collection_con}>
          {
            collectionList.map((item, key) => (
              <>
                <View key={key} className={styles.collection_item} onClick={() => handleMove(item)}>
                  <View className={styles.collectionImage}></View>
                  <Text className={styles.text}>{item.name}</Text>
                </View>
                <Divider direction="horizontal" customStyles={{ margin: '12px 0' }}></Divider>
              </>
            ))
          }
          <View className={styles.collection_item} onClick={handleNewCollection}>
            <View className={styles.collectionImage}>
              <IconFont name="icon-xinzengshoucangjia" size={40}></IconFont>
            </View>
            <Text className={styles.text}>新建收藏夹</Text>
          </View>
          <Divider direction="horizontal" customStyles={{ margin: '12px 0' }}></Divider>

        </View>
      </ScrollView>
    </Popup>
  )
}
export default memo(MoveCollectionPopup)
