import httpClient from '@/common/http-client';
import axios from 'axios';

// 공지사항 등록
const insertNotice = (params) => {
  return httpClient.post('/api/v1/bo/homeMgt/insertNotice', params, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
};

// 공지사항 수정
const updateNotice = (params) => {
  return httpClient.post('/api/v1/bo/homeMgt/updateNotice', params, { withCredentials: false });
};

// 공지사항 삭제
const deleteNotice = (params) => {
  return httpClient.post('/api/v1/bo/homeMgt/deleteNotice', params, { withCredentials: false });
};

// 자주하는 질문 등록
const insertFaq = (params) => {
  return httpClient.post('/api/v1/bo/homeMgt/insertFaq', params, { withCredentials: false });
};

// 자주하는 질문  수정
const updateFaq = (params) => {
  return httpClient.post('/api/v1/bo/homeMgt/updateFaq', params, { withCredentials: false });
};

// 자주하는 질문  삭제
const deleteFaq = (params) => {
  return httpClient.post('/api/v1/bo/homeMgt/deleteFaq', params, { withCredentials: false });
};

const getImageUrl = (params) => {
  return httpClient.post('/api/v1/bo/homeMgt/getImageUrl', params, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
};

const fileDownload = (params) => {
  axios({
    method: 'POST',
    url: '/api/v1/bo/homeMgt/filedownload',
    responseType: 'blob',
    headers: '',
    data: params,
    params: params,
  })
    .then((response) => {
      const fileName = params.fileTitle;
      const data = response.data;
      if (!data) {
        return;
      }
      const url = window.URL.createObjectURL(new Blob([data]));
      const a = document.createElement('a');
      a.style.display = 'none';
      a.href = url;
      a.setAttribute('download', fileName);
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      window.URL.revokeObjectURL(url);
    })
    .catch((response) => {
      console.log(response);
    });
};

export default {
  insertNotice,
  updateNotice,
  deleteNotice,
  fileDownload,
  getImageUrl,
  insertFaq,
  updateFaq,
  deleteFaq,
};
