import { fetchJSON, postForm } from '@/utils/request';
import { API_HOST, EMAIL_HOST } from '@/config';
import testData from './test1.json';

/**
 * 获取顾问签名
 */
export const salesSignature = async (opisn, lgc = 1) => {
  try {
    const html = await fetchJSON(`http://202.103.68.35/CustomerManager/english/mailsign.asp`, { WL_SN: opisn, LGC: lgc });
    const parser = new DOMParser();
    const doc = parser.parseFromString(html, 'text/html');
    const bodyContent = doc.body.innerHTML;
    return bodyContent;
  } catch (error) {
    return '';
  }
};

/**
 * 发送邮件
 */
export const postSendEmail = async (body) => {
  const { attaList, atta, content, ...bodyData } = body;
  const formData = new FormData();
  Object.keys(bodyData).forEach(function (key) {
    formData.append(key, bodyData[key]);
  });
  attaList.forEach(function (item) {
    formData.append('attachment', item);
  });
  const { result } = await postForm(`${EMAIL_HOST}/sendmail`, formData);
  return result;
};

/**
 * 重发邮件
 * @param {object} { mai_sn }
 */
export const postResendEmailAction = async (body) => {
  const { attaList, atta, content, ...bodyData } = body;
  const formData = new FormData();
  Object.keys(bodyData).forEach(function (key) {
    formData.append(key, bodyData[key]);
  });
  return await postForm(`${EMAIL_HOST}/email_resend`, formData);
};

/**
 * 邮件详情
 * @param {object} { mai_sn }
 */
export const getEmailDetailAction = async (param) => {
  const { result } = await fetchJSON(`${EMAIL_HOST}/getmail`, param);
  return { info: result.MailInfo?.[0] || {}, content: result.MailContent || '', attachments: result?.AttachList || [] };
}

/**
 * 主动收邮件, 单个账户
 * @param {object} { opi_sn, }
 */
export const getEmailFetchAction = async (param) => {
  const { opi_sn, } = param
  const { result } = await fetchJSON(`${EMAIL_HOST}/email_fetch`, {
    opi_sn,
  })
  return result
};
