// React Imports
import { useState } from 'react'

// MUI Imports
import Button from '@mui/material/Button'
import Divider from '@mui/material/Divider'
import Drawer from '@mui/material/Drawer'
import IconButton from '@mui/material/IconButton'
import MenuItem from '@mui/material/MenuItem'
import Typography from '@mui/material/Typography'

// Third-party Imports

// Type Imports

// Components Imports
import CustomTextField from '@core/components/mui/TextField'
import { useCategoriesMutation } from '../../../../../services/mutations/categories'
import { CategoryRequest } from '../../../../../types/services/category'

type Props = {
  open: boolean
  handleClose: () => void
}

const AddCategoryDrawer = (props: Props) => {
  // Props
  const { open, handleClose } = props

  const { mutate: createCategory, isPending: isCreating } = useCategoriesMutation().createCategory

  // States
  const [formData, setFormData] = useState<CategoryRequest>({
    name: '',
    description: '',
    business_type: ''
  })

  // Handle Form Submit
  const handleFormSubmit = (e: any) => {
    e.preventDefault()

    createCategory(formData, {
      onSuccess: () => {
        handleReset()
      }
    })
  }

  const handleInputChange = (e: any) => {
    setFormData({
      ...formData,
      [e.target.name]: e.target.value
    })
  }

  // Handle Form Reset
  const handleReset = () => {
    handleClose()
    setFormData({
      name: '',
      description: '',
      business_type: ''
    })
  }

  return (
    <Drawer
      open={open}
      anchor='right'
      variant='temporary'
      onClose={handleReset}
      ModalProps={{ keepMounted: true }}
      sx={{ '& .MuiDrawer-paper': { width: { xs: 300, sm: 400 } } }}
    >
      <div className='flex items-center justify-between pli-6 plb-5'>
        <Typography variant='h5'>Add Category</Typography>
        <IconButton size='small' onClick={handleReset}>
          <i className='tabler-x text-textSecondary text-2xl' />
        </IconButton>
      </div>
      <Divider />
      <div className='p-6'>
        <form onSubmit={handleFormSubmit} className='flex flex-col gap-5'>
          <CustomTextField
            fullWidth
            label='Title'
            name='name'
            value={formData.name}
            onChange={handleInputChange}
            placeholder='Minuman'
          />
          <CustomTextField
            select
            fullWidth
            label='Business Type'
            value={formData.business_type}
            onChange={e => setFormData({ ...formData, business_type: e.target.value })}
          >
            <MenuItem value='restaurant'>Restaurant</MenuItem>
          </CustomTextField>
          <CustomTextField
            fullWidth
            label='Description'
            value={formData.description}
            name='description'
            onChange={handleInputChange}
            multiline
            rows={4}
            placeholder='Write a Comment...'
          />
          <div className='flex items-center gap-4'>
            <Button variant='contained' type='submit' disabled={isCreating}>
              {isCreating ? 'Add...' : 'Add'}
            </Button>
            <Button variant='tonal' color='error' type='reset' onClick={handleReset}>
              Discard
            </Button>
          </div>
        </form>
      </div>
    </Drawer>
  )
}

export default AddCategoryDrawer
