import apiClient from "@/infrastructure/apiClient";
import apiPath from "@/infrastructure/apiPath";
export default {
  async search(searchModel) {
    const params = JSON.parse(JSON.stringify(searchModel));
    var searchResult = await apiClient
      .post(apiPath.candidate.search, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async create(candidateModel) {
    let dataToSend = candidateModel;
    let config = {};
    if (candidateModel instanceof FormData) {
      config.headers = { "Content-Type": "multipart/form-data" };
    } else {
      dataToSend = JSON.parse(JSON.stringify(candidateModel));
    }
    var searchResult = await apiClient
      .post(apiPath.candidate.create, dataToSend, config)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getById(id) {
    var searchResult = await apiClient
      .get(apiPath.candidate.getById + id)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getReviewModel(id) {
    var searchResult = await apiClient
      .get(apiPath.candidate.getReviewModel + id)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async updateReview(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .put(apiPath.candidate.updateReview, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async delete(candidateId) {
    var roleResult = await apiClient
      .delete(apiPath.candidate.delete + candidateId)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async uploadCV(file) {
    const formData = new FormData();
    formData.append("file", file);
    try {
      const response = await apiClient.post("/Candidate/UploadCV", formData, {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      });
      return response.data;
    } catch (e) {
      console.log(e);
      return {};
    }
  },
};
