import ApiConnect from "../ApiConfig";

//Get All vendors

export const get_all_customers = async () => {
  // console.log(data)
  try {
    // if (!data.id) {
    //   throw new Error("data.id is empty or undefined");
    // }
    const result = await ApiConnect(`customers`, {
      method: "GET",
    });
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed get all customers api");

    return error;
  }
};
//Get a notification
export const get_customer = async (data) => {
  // console.log(data)
  try {
    if (!data.id) {
      throw new Error("data.id is empty or undefined");
    }
    const result = await ApiConnect(`customers/${data.id}`, {
      method: "GET",
      // data: data,
      //   headers: {
      //     Authorization: `Bearer ${data.token}`,
      //   },
    });
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed get customer api");

    return error;
  }
};
export const get_customer_login_id = async (data) => {
  // console.log(data)
  try {
    if (!data.id) {
      throw new Error("data.id is empty or undefined");
    }
    const result = await ApiConnect(`customers/login_id/${data.id}`, {
      method: "GET",
      // data: data,
      //   headers: {
      //     Authorization: `Bearer ${data.token}`,
      //   },
    });
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed get customer login api");

    return error;
  }
};
export const search_customer = async (data) => {
  // console.log(data)
  try {
    if (!data.searchData) {
      throw new Error("data.id is empty or undefined");
    }
    const result = await ApiConnect(`customers/search?q=${data.searchData}`, {
      method: "GET",
      // data: data,
      //   headers: {
      //     Authorization: `Bearer ${data.token}`,
      //   },
    });
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed get product api");

    return error;
  }
};
//CREATE customer
// export const create_customer = async (data) => {
//   // console.log(data)
//   try {
//     const result = await ApiConnect(`customers`, {
//       method: "POST",
//       data: data.body,
//       //   headers: {
//       //     Authorization: `Bearer ${data.token}`,
//       //   },
//     });
//     console.log("created");
//     return result;
//   } catch (error) {
//     console.log(error);
//     console.log(error + "failed create customer api");

//     return error;
//   }
// };
export const create_customer = async (data) => {
  try {
    const formData = new FormData();
    
    // Add your form data fields here
    formData.append('first_name', data.first_name);
    formData.append('last_name', data.last_name);
    formData.append('user_email', data.user_email);
    formData.append('phone_number', data.phone_number);
    formData.append('role', data.role);
    formData.append('login_id', data.login_id);


    const result = await ApiConnect('customers', {
      method: 'POST',
      data: formData, // Use the FormData object as the body
      // headers: {
      //   Authorization: `Bearer ${data.token}`,
      // },
    });

    console.log('Customer created successfully'+ result.status);
    return result;
  } catch (error) {
    console.error('Error creating customer:', error);
    return error;
  }
};
export const update_customer = async (data) => {
  // console.log(data)
  try {
    if (!data.id) {
      throw new Error("data.id is empty or undefined");
    }
    console.log(data.body);

    const result = await ApiConnect(`customers/${data.id}`, {
      method: "PATCH",
      data: data.body,
      //   headers: {
      //     Authorization: `Bearer ${data.token}`,
      //   },
    });
    return result;
  } catch (error) {
    console.log(error + "failed update customer api");
    console.log(data);

    return error;
  }
};
export const uploadImage = async (imageData) => {
  try {
    const formData = new FormData();

    // Add image data to FormData
    formData.append('image_id', imageData.image_id);
    formData.append('category', imageData.category);
    formData.append('image', imageData.imageFile); // Assuming 'imageFile' contains the image file

    const result = await ApiConnect('upload_image', {
      method: 'POST',
      data: formData,
      // Add headers or authorization if required
    });

    console.log('Image uploaded successfully', result.status);
    return result;
  } catch (error) {
    console.error('Error uploading image:', error);
    return error;
  }
};