import axios from "axios";

export const createItem = async (settings) => {
  try {
    console.log(settings);

    const formData = new FormData();
    formData.append("room", settings.room);
    formData.append("brand", settings.brand);
    formData.append("model", settings.model);
    formData.append("price", settings.price);
    formData.append("purchaseDate", settings.purchaseDate);
    formData.append("link", settings.link);
    formData.append("description", settings.description);
    formData.append("image", settings.image);
    formData.append("invoice", settings.invoice);

    const response = await axios.post("/item", formData);

    return response.data;
  } catch (err) {
    console.error(err);
    return null;
  }
};

export const updateItem = async (settings) => {
  try {
    console.log(settings);

    const formData = new FormData();
    formData.append("brand", settings.brand);
    formData.append("model", settings.model);
    formData.append("price", settings.price);
    formData.append("description", settings.description);
    formData.append("purchaseDate", settings.purchaseDate);
    formData.append("link", settings.link);

    const response = await axios.put(`/item/${settings._id}`, formData);
    return response.data;
  } catch (err) {
    console.error(err);
    return null;
  }
};

export const deleteItem = async (id) => {
  try {
    console.log(id);
    const response = await axios.delete(`/item/${id}`);
    return response.data;
  } catch (err) {
    console.error(err);
    return null;
  }
};
