import { useNavigate, useParams } from 'react-router-dom';
import ManuForm from '../components/Shared/ManuForm';
import Header from '../components/Shared/Header';
import axios from 'axios';
import { useEffect, useState } from 'react';
import Manufacturer from '../interfaces/Manufacturer';

const UpdateManu = () => {
  const id = parseInt(useParams().id ?? '-1');
  const [drug, setDrug] = useState<Manufacturer | null>(null);
  const navigate = useNavigate();

  useEffect(() => {
    const fetchDrugById = async (id: number) => {
      try {
        const drugcox = await axios.get<Manufacturer>(
          `https://silkroadapi.azurewebsites.net/Manufacturer/get-manufacturer/${id}`,
          { withCredentials: true }
        );
        setDrug(drugcox.data);
      } catch (error) {
        console.error(error);
        navigate('/error');
      }
    };
    fetchDrugById(id);
  }, [id, navigate]);
  return (
    drug && (
      <>
        <Header />
        <ManuForm updateDrug={drug} />
      </>
    )
  );
};

export default UpdateManu;
