// src/SubJobs.js

import React, { useRef } from 'react'
import { Button, Flex, Space, Dropdown } from 'antd'
import useColumnVisibility from '../hooks/useColumnVisibility.js'
import ReloadIcon from '../../Icons/ReloadIcon.jsx'
import ObjectTable from '../common/ObjectTable.jsx'
import ListIcon from '../../Icons/ListIcon.jsx'
import GridIcon from '../../Icons/GridIcon.jsx'
import useViewMode from '../hooks/useViewMode.js'
import ColumnViewButton from '../common/ColumnViewButton.jsx'

const SubJobs = () => {
  const tableRef = useRef()
  const [viewMode, setViewMode] = useViewMode('subJob')

  const [columnVisibility, setColumnVisibility] = useColumnVisibility('subJob')

  const actionItems = {
    items: [
      {
        label: 'Reload List',
        key: 'reloadList',
        icon: <ReloadIcon />
      }
    ],
    onClick: ({ key }) => {
      if (key === 'reloadList') {
        tableRef.current?.reload()
      }
    }
  }

  return (
    <>
      <Flex vertical={'true'} gap='large' style={{ height: '100%' }}>
        <Flex justify={'space-between'}>
          <Space size='small'>
            <Dropdown menu={actionItems}>
              <Button>Actions</Button>
            </Dropdown>
            <ColumnViewButton
              type='subJob'
              disabled={false}
              visibleState={columnVisibility}
              updateVisibleState={setColumnVisibility}
            />
          </Space>

          <Space>
            <Button
              icon={viewMode === 'cards' ? <ListIcon /> : <GridIcon />}
              onClick={() =>
                setViewMode(viewMode === 'cards' ? 'list' : 'cards')
              }
            />
          </Space>
        </Flex>

        <ObjectTable
          ref={tableRef}
          type={'subJob'}
          visibleColumns={columnVisibility}
          cards={viewMode === 'cards'}
        />
      </Flex>
    </>
  )
}

export default SubJobs
