import React, { useCallback, useEffect, useMemo, useState } from 'react'
import styles from './index.module.scss'
import { A2ListType } from '@/pages/A2clue/data'
import { A2_APIdel, A2_APIgetList } from '@/store/action/A2clue'
import MyTable from '@/components/MyTable'
import { A2tableC } from '@/utils/tableData'
import { MessageFu } from '@/utils/message'
import { Button } from 'antd'
import MyPopconfirm from '@/components/MyPopconfirm'
import A2add from '@/pages/A2clue/A2add'
import { A1ListSelectType } from '../../data'

type Props = {
  sId: number
  listAllRes: A1ListSelectType
}

function A3tab3clue({ sId, listAllRes }: Props) {
  // 获取列表信息
  const [list, setList] = useState<A2ListType[]>([])

  const getList = useCallback(async () => {
    const res = await A2_APIgetList(
      {
        pageNum: 1,
        pageSize: 99999,
        martyrId: sId
      },
      true
    )
    if (res.code === 0) {
      setList(res.data.records || [])
    }
  }, [sId])

  useEffect(() => {
    getList()
  }, [getList])

  // 点击新增、编辑
  const [editId, setEditId] = useState(0)

  // 点击删除
  const delTableFu = useCallback(
    async (id: number) => {
      const res = await A2_APIdel(id)
      if (res.code === 0) {
        MessageFu.success('删除成功!')
        getList()
      }
    },
    [getList]
  )

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: '操作',
        render: (item: any) => {
          return (
            <>
              <Button size='small' type='text' onClick={() => setEditId(item.id)}>
                编辑
              </Button>
              <MyPopconfirm txtK='删除' onConfirm={() => delTableFu(item.id)} />
            </>
          )
        }
      }
    ]
  }, [delTableFu])

  return (
    <div className={styles.A3tab3clue}>
      <div className='A3t1btn'>
        <Button type='primary' onClick={() => setEditId(-1)}>
          新增线索
        </Button>
      </div>
      <MyTable list={list} columnsTemp={A2tableC} lastBtn={tableLastBtn} pagingInfo={false} />

      {editId ? (
        <A2add
          sId={editId}
          closeFu={() => setEditId(0)}
          addTableFu={getList}
          upTableFu={getList}
          listAllRes={listAllRes}
          martyrId={sId}
        />
      ) : null}
    </div>
  )
}

const MemoA3tab3clue = React.memo(A3tab3clue)

export default MemoA3tab3clue
