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

// 공통 코드.
const getCommCode = (params) => {
  return httpClient.post('/api/v1/bo/comm/getCode', params, { withCredentials: false });
};

const adminList = (params) => {
  return httpClient.post('/api/v1/bo/sendNumMgt/adminList', params, { withCredentials: false });
};

const numberDetail = (params) => {
  return httpClient.post('/api/v1/bo/sendNumMgt/numberDetail', params, { withCredentials: false });
};

const apprDetail = (params) => {
  return httpClient.post('/api/v1/bo/sendNumMgt/apprDetail', params, { withCredentials: false });
};

const deleteNumber = (params) => {
  return httpClient.post('/api/v1/bo/sendNumMgt/deleteNumber', params, { withCredentials: false });
};

// 사업자가 등록 요청.
const insertNumber = (adminId, custNm, bRegNo, nmineeDivCd, numberInputs, bizrAuthYn, custSeq) => {
  let formData = new FormData();
  const row = {};
  row.adminId = adminId;
  row.custNm = custNm;
  row.bregno = bRegNo;
  row.nmineeDivCd = nmineeDivCd;
  row.list = numberInputs;
  row.bizrAuthYn = bizrAuthYn;
  row.custSeq = custSeq;

  formData.append('key', new Blob([JSON.stringify(row)], { type: 'application/json' }));

  let url = '/api/v1/bo/sendNumMgt/insertNumber';
  return httpClient.post(url, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
      'Show-Layer': 'Yes',
    },
  });
};

// 사업자가 등록 요청.
const insertNumber1 = (
  communicationFile,
  tenureFile,
  businessFile,
  identificationCardFile,
  identificationEvidenceFile,
  adminId,
  custNm,
  bRegNo,
  nmineeDivCd,
  numberInputs,
  bizrAuthYn,
  custSeq
) => {
  let formData = new FormData();
  formData.append('communicationFile', communicationFile);
  formData.append('tenureFile', tenureFile);
  formData.append('businessFile', businessFile);
  formData.append('identificationCardFile', identificationCardFile);
  formData.append('identificationEvidenceFile', identificationEvidenceFile);

  const row = {};
  row.adminId = adminId;
  row.custNm = custNm;
  row.bregno = bRegNo;
  row.nmineeDivCd = nmineeDivCd;
  row.list = numberInputs;
  row.bizrAuthYn = bizrAuthYn;
  row.custSeq = custSeq;

  formData.append('key', new Blob([JSON.stringify(row)], { type: 'application/json' }));

  let url = '/api/v1/bo/sendNumMgt/insertNumber';
  return httpClient.post(url, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
      'Show-Layer': 'Yes',
    },
  });
};

// 타사업자가 등록 요청.
const insertNumber2 = (
  communicationFile,
  delegationFile,
  attorneyFile,
  otherBusinessFile,
  delegatedIdentificationFile,
  delegateCertificateFile,
  delegatedBusinessFile,
  authorizedBusinessFile,
  //corporateCertificateFile,
  identificationCardFile,
  identificationEvidenceFile,
  commDeputyCertificateFile,
  commDeputyIdFile,
  adminId,
  custNm,
  bRegNo,
  nmineeDivCd,
  numberInputs,
  bizrAuthYn,
  custSeq
) => {
  let formData = new FormData();
  formData.append('communicationFile', communicationFile);
  formData.append('delegationFile', delegationFile);
  formData.append('attorneyFile', attorneyFile);
  formData.append('otherBusinessFile', otherBusinessFile);
  formData.append('delegatedIdentificationFile', delegatedIdentificationFile);
  formData.append('delegateCertificateFile', delegateCertificateFile);
  formData.append('delegatedBusinessFile', delegatedBusinessFile);
  formData.append('authorizedBusinessFile', authorizedBusinessFile);
  //formData.append('corporateCertificateFile', corporateCertificateFile);
  formData.append('identificationCardFile', identificationCardFile);
  formData.append('identificationEvidenceFile', identificationEvidenceFile);
  formData.append('commDeputyCertificateFile', commDeputyCertificateFile);
  formData.append('commDeputyIdFile', commDeputyIdFile);
  const row = {};
  row.adminId = adminId;
  row.custNm = custNm;
  row.bregno = bRegNo;
  row.nmineeDivCd = nmineeDivCd;
  row.list = numberInputs;
  row.bizrAuthYn = bizrAuthYn;
  row.custSeq = custSeq;

  formData.append('key', new Blob([JSON.stringify(row)], { type: 'application/json' }));

  let url = '/api/v1/bo/sendNumMgt/insertNumber';

  return httpClient.post(url, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
      'Show-Layer': 'Yes',
    },
  });
};

const updateAppr = (params) => {
  return httpClient.post('/api/v1/bo/sendNumMgt/updateAppr', params, { withCredentials: false });
};

const fileDownload = (params) => {
  axios({
    method: 'POST',
    url: '/api/v1/bo/sendNumMgt/filedownload',
    responseType: 'blob',
    headers: '',
    data: params,
    params: params,
  })
    .then((response) => {
      const fileName = params.docTitle;
      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 {
  getCommCode,
  numberDetail,
  adminList,
  insertNumber,
  insertNumber1,
  insertNumber2,
  apprDetail,
  updateAppr,
  deleteNumber,
  fileDownload,
};
