import { Button, FormLabel } from '@mui/material';
import FormControl from '@mui/material/FormControl';
import TextField from '@mui/material/TextField';
import { ChangeEvent, FormEvent, useState } from 'react';
import Drug from '../../interfaces/Drug';
import getEmptyDrug from '../../utils/DrugUtils';
import { useNavigate } from 'react-router-dom';
import axios from 'axios';

export default function DrugFrom(props: { updateDrug?: Drug }) {
  const [drug, setDrug] = useState(props.updateDrug ?? getEmptyDrug());
  const navigate = useNavigate();

  const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    setDrug({
      ...drug,
      [e.target.name]: e.target.value
    });
  };

  const handleSubmit = (e: FormEvent<HTMLElement>) => {
    e.preventDefault();
    if (drug.id === undefined || drug.id === -1) {
      addDrug(drug);
    } else {
      updateDrug(drug);
    }
    navigate('/');
  };

  const addDrug = async (drug: Drug) => {
    try {
      axios.post('https://silkroadapi.azurewebsites.net/Drug/add-drug', drug, {
        withCredentials: true
      });
    } catch (error) {
      console.error(error);
    }
  };

  const updateDrug = async (drug: Drug) => {
    try {
      axios.put(
        'https://silkroadapi.azurewebsites.net/Drug/update-drug',
        drug,
        { withCredentials: true }
      );
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <FormControl
        sx={{
          width: '70%',
          marginLeft: '15%',
          marginRight: '15%',
          marginBottom: '30px',
          marginTop: '30px'
        }}
      >
        <FormLabel>Name</FormLabel>
        <TextField
          type='text'
          required
          onChange={handleChange}
          name='name'
          title='Name'
          value={drug.name}
        />
        <FormLabel>Stock</FormLabel>
        <TextField
          type='number'
          required
          onChange={handleChange}
          name='stock'
          title='Stock'
          value={drug.stock}
        />
        <FormLabel>Price</FormLabel>
        <TextField
          type='number'
          required
          onChange={handleChange}
          name='price'
          title='Price'
          value={drug.price}
        />
        <FormLabel>Manufacturer</FormLabel>
        <TextField
          type='text'
          required
          onChange={handleChange}
          name='manufacturer'
          title='Manufacturer'
          value={drug.manufacturer}
        />
        <FormLabel>Description</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='description'
          title='Description'
          value={drug.description}
        />
        <FormLabel>Side Effects</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='sideEffects'
          title='Side Effects'
          value={drug.sideEffects}
        />
        <FormLabel>Interactions</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='interactions'
          title='Interactions'
          value={drug.interactions}
        />
        <FormLabel>Dosage</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='dosage'
          title='Dosage'
          value={drug.dosage}
        />
        <FormLabel>Contraindications</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='contraindications'
          title='Contraindications'
          value={drug.contraindications}
        />
        <FormLabel>Warnings</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='warnings'
          title='Warnings'
          value={drug.warnings}
        />
        <FormLabel>Storage</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='storage'
          title='Storage'
          value={drug.storage}
        />
        <FormLabel>Pregnancy</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='pregnancy'
          title='Pregnancy'
          value={drug.pregnancy}
        />
        <FormLabel>Breastfeeding</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='breastfeeding'
          title='Breastfeeding'
          value={drug.breastfeeding}
        />
        <FormLabel>Overdose</FormLabel>
        <TextField
          type='text'
          multiline
          rows={4}
          required
          onChange={handleChange}
          name='overdose'
          title='Overdose'
          value={drug.overdose}
        />
        <Button variant='outlined' color='secondary' type='submit'>
          Submit
        </Button>
      </FormControl>
    </form>
  );
}
