import React, { useState } from 'react';
import { type Recipe } from "../types/Recipe"
import Modal from '../components/Modal.tsx'

interface CookbookRecipeTileProps {
  recipe: Recipe;
  handleDelete: (id: number | undefined) => void;
}

function CookbookRecipeTile({ recipe, handleDelete }: CookbookRecipeTileProps) {
  const [isModalOpen, setIsModalOpen] = useState(false);

  const openModal = () => setIsModalOpen(true);
  const closeModal = () => setIsModalOpen(false);
  const confirmDelete = () => {
    handleDelete(recipe.id);
    closeModal();
  };


  return (
    <div className="recipe-card m-2 bg-amber-300 p-4 rounded shadow">
      <div className="flex justify-between items-center recipe-name">
        <h3 className="font-bold"><a href={`/recipe/${recipe.id}`} className="text-blue-500">{recipe.name}</a></h3>
        <button onClick={openModal} className="text-red-500 focus:outline-none">
          <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-6 h-6">
            <path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
          </svg>
        </button>
      </div>
      <Modal
        isOpen={isModalOpen}
        onClose={closeModal}
        message="Are you sure you want to delete this recipe?"
        confirmAction={confirmDelete}
        cancelAction={closeModal}
      />
    </div>
  );
};

export default CookbookRecipeTile;
