import { TypeD2list } from '@/pages/D_storeManage/D2storSet/type'
import { Checkbox, Modal } from 'antd'
import { FC, useEffect, useState } from 'react'

export interface StorageModalProps {
  value: number[]
  isModalOpen: boolean
  list: TypeD2list[]
  setIsModalOpen: (v: boolean) => void
  onChange: (v: number[]) => void
}

export const StorageModal: FC<StorageModalProps> = ({
  list,
  value,
  isModalOpen,
  setIsModalOpen,
  onChange
}) => {
  const [val, setVal] = useState<number[]>([])

  const handleOk = () => {
    onChange(val)
    setIsModalOpen(false)
  }

  const handleCancel = () => {
    setIsModalOpen(false)
  }

  useEffect(() => {
    setVal(value)
  }, [value, isModalOpen])

  return (
    <Modal
      title='统计库房'
      destroyOnClose
      open={isModalOpen}
      onOk={handleOk}
      onCancel={handleCancel}
    >
      <Checkbox.Group
        value={val}
        options={list.map(i => ({ label: i.name, value: i.id }))}
        onChange={v => setVal(v)}
      />
    </Modal>
  )
}
