// src/Jobs.js

import { useState, useRef } from 'react'
import { Button, Flex, Space, Modal, Dropdown } from 'antd'
import NewJob from './Jobs/NewJob.jsx'
import useColumnVisibility from '../hooks/useColumnVisibility.jsx'
import PlusIcon from '../../Icons/PlusIcon.jsx'
import ReloadIcon from '../../Icons/ReloadIcon.jsx'
import ObjectTable from '../common/ObjectTable.jsx'
import useViewMode from '../hooks/useViewMode.jsx'
import ObjectTableViewButton from '../common/ObjectTableViewButton'
import FilterSidebarButton from '../common/FilterSidebarButton'
import ColumnViewButton from '../common/ColumnViewButton.jsx'
import useFilterSidebarVisibility from '../hooks/useFilterSidebarVisibility.jsx'
import ExportListButton from '../common/ExportListButton.jsx'

const Jobs = () => {
  const [newJobOpen, setNewJobOpen] = useState(false)
  const tableRef = useRef()
  const [viewMode, setViewMode] = useViewMode('job')

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

  const [showFilterSidebar, setShowFilterSidebar] =
    useFilterSidebarVisibility('Jobs')

  const actionItems = {
    items: [
      {
        label: 'New Print Job',
        key: 'newJob',
        icon: <PlusIcon />
      },
      { type: 'divider' },
      {
        label: 'Reload List',
        key: 'reloadList',
        icon: <ReloadIcon />
      }
    ],
    onClick: ({ key }) => {
      if (key === 'newJob') {
        showNewJobModal()
      } else if (key === 'reloadList') {
        tableRef.current?.reload()
      }
    }
  }

  const showNewJobModal = () => {
    setNewJobOpen(true)
  }

  return (
    <>
      <Flex vertical={'true'} gap='large' className='h-100'>
        <Flex justify={'space-between'} style={{ minHeight: 0 }}>
          <Space size='small'>
            <Dropdown menu={actionItems}>
              <Button>Actions</Button>
            </Dropdown>
            <ColumnViewButton
              type='job'
              disabled={false}
              visibleState={columnVisibility}
              updateVisibleState={setColumnVisibility}
            />
            <ExportListButton objectType='job' />
          </Space>

          <Space>
            <FilterSidebarButton
              active={showFilterSidebar}
              onClick={() => setShowFilterSidebar(!showFilterSidebar)}
            />
            <ObjectTableViewButton
              viewMode={viewMode}
              setViewMode={setViewMode}
            />
          </Space>
        </Flex>

        <ObjectTable
          ref={tableRef}
          type={'job'}
          visibleColumns={columnVisibility}
          cards={viewMode === 'cards'}
          showFilterSidebar={showFilterSidebar}
          expandHeight={true}
        />
      </Flex>
      <Modal
        open={newJobOpen}
        footer={null}
        width={700}
        destroyOnHidden={true}
        onCancel={() => {
          setNewJobOpen(false)
        }}
      >
        <NewJob
          onOk={() => {
            setNewJobOpen(false)
            tableRef.current?.reload()
          }}
          reset={newJobOpen}
        />
      </Modal>
    </>
  )
}

export default Jobs
