import { Text, View } from '@tarojs/components'
import { useDidShow } from '@tarojs/taro'
import { useEffect, useRef, useState } from 'react'
import type { CompanyParam } from '../companyItem'
import CompanyItem from '../companyItem'
import styles from './index.module.scss'
import Search from '@/components/search'
import { weightListApi } from '@/api/weightList'
import { goLink } from '@/common/common'

export default () => {
  const { fetchData } = weightListApi()
  const [list, setList] = useState<CompanyParam['value'][]>([])
  const getDataList = async() => {
    const res = await fetchData()
    setList(() => res.data?.list)
  }
  const getDataStatus = useRef(false)
  useEffect(() => {
    getDataStatus && getDataList()
    getDataStatus.current = true
  }, [])

  useDidShow(() => {
    getDataList()
  })

  return <View className={styles.company_main}>
    <View className={styles.company_search}>
      <View className={styles['code_list_search--code']}><Search placeholder="请输入单据抬头/客户/单号" /></View>
    </View>
    <View className={styles.company_con}>
      {list?.map((item) => {
        return <View key={item.id}>
          <CompanyItem value={item} />
        </View>
      })}
    </View>
    <View className={styles.company_create}>
      <View className={styles.company_create_btn} onClick={() => goLink('/pages/weightListAdd/index?type=add')}>新建抬头</View>
    </View>
  </View>
}
