import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { EmailsVO, EmailsForm, EmailsQuery } from '@/api/emails/types';

/**
 * 获取邮件列表
 * @param query
 * @returns {*}
 */

export const listEmails = (query?: EmailsQuery): AxiosPromise<{records: EmailsVO[]}> => {
    return request({
        url: '/mail/page_list',
        method: 'post',
        data: query
    });
};

/**
 * 接收邮件列表
 * @param query
 * @returns {*}
 */

export const receiveEmails = () => {
    return request({
        url: '/mail/receive_mail',
        method: 'post',
        data: {},
        timeout: 0
    });
};


/**
 * 查询邮件详细
 * @param id
 */
export const getEmails = (mailId: string | number): AxiosPromise<EmailsVO> => {
    return request({
        url: '/mail/get_mail_detial',
        method: 'post',
        data: { mailId }
    });
};

/**
 * 修改邮件
 * @param data
 */
export const updateEmails = (data: EmailsForm) => {
    return request({
        url: '/emails',
        method: 'put',
        data: data
    });
};

/**
 * 删除邮件
 * @param data
 */
export const delEmails = (id: number | string) => {
    return request({
        url: '/mail/mark_trash',
        method: 'post',
        data: { emailId: id, isTrash: 1 }
    });
};

/**
 * 获取个人邮箱
 * @param data
 */
export const getUserEmails = () => {
    return request({
        url: '/smtp/pop/setting/personal_mail_account',
        method: 'post',
        data: {}
    });
}

/**
 * 获取邮件配置列表信息
 * @param data
 */
export const listEmailsSetting = (data: PageQuery) => {
    return request({
        url: '/smtp/pop/setting/pageList',
        method: 'post',
        data
    });
}

/**
 * 新增邮箱配置
 * @param data
 */
export const addEmailsSetting = (data: EmailsForm) => {
    return request({
        url: '/smtp/pop/setting/save',
        method: 'post',
        data
    });
};

/**
 * 删除邮箱配置
 * @param data
 */
export const delEmailsSetting = (id: string | number) => {
    return request({
        url: '/smtp/pop/setting/delete',
        method: 'post',
        data: { id }
    });
};

/**
 * 邮箱检测
 * @param data
 */
export const emailsTest = (data: EmailsForm) => {
    return request({
        url: '/smtp/pop/setting/test_connect',
        method: 'post',
        data
    });
};

/**
 * 获取邮箱端口配置
 * @param data
 */
export const getMailsPort = (mailAccount: string) => {
    return request({
        url: '/mail/mail_port_config',
        method: 'post',
        data: { mailAccount }
    });
};

/**
 * 发送邮件
 * @param data
 */
export const sendEmails = (data: EmailsForm) => {
    return request({
        url: '/mail/send_mail',
        method: 'post',
        data
    });
};

/**
 * 保存草稿
 * @param data
 */
export const saveDraft = (data: EmailsForm) => {
    return request({
        url: '/mail/save_draft',
        method: 'post',
        data
    });
};

/**
 * 获取邮件附件
 * @param query
 * @returns {*}
 */
export const attachmentEmails = (query?: EmailsQuery): AxiosPromise<{records: EmailsVO[]}> => {
    return request({
        url: '/mail/list_mail_attachment',
        method: 'post',
        data: query
    });
};

/**
 * 设为星标邮件
 * @param query
 * @returns {*}
 */

export const markStar = (query?: any): AxiosPromise<{records: EmailsVO[]}> => {
    return request({
        url: '/mail/mark_star',
        method: 'post',
        data: query
    });
};

/**
 * 全部标记为已读
 * @param query
 * @returns {*}
 */

export const markAllRead = (query?: any): AxiosPromise<{records: EmailsVO[]}> => {
    return request({
        url: '/mail/mark_all_read',
        method: 'post',
        data: query
    });
};

/**
 * 标记为已读
 * @param query
 * @returns {*}
 */

export const markRead = (query?: any): AxiosPromise<{records: EmailsVO[]}> => {
    return request({
        url: '/mail/mark_read',
        method: 'post',
        data: query
    });
};

/**
 * 删除邮件/移入垃圾箱
 * @param id
 */
export const trashEmails = (query?: any): AxiosPromise<{records: EmailsVO[]}> => {
    return request({
        url: '/mail/mark_trash',
        method: 'post',
        data: query
    });
};


/**
 * 彻底删除邮件
 * @param ids
 * @returns {*}
 */

export const completelyDelEmails = (ids: any): AxiosPromise<EmailsVO> => {
    return request({
        url: '/mail/deleteIds',
        method: 'post',
        data: { ids }
    });
};

/**
 * 删除草稿
 * @param ids
 * @returns {*}
 */

export const completelyDelDraft = (ids: any): AxiosPromise<EmailsVO> => {
    return request({
        url: '/mail/deleteDraftIds',
        method: 'post',
        data: {ids}
    });
};

/**
 * 获取客户联系人邮箱
 * @param data
 */
export const getCustomerEmails = (param: {scopeMenuId: string, pageSize: number, pageIndex: number, blurry?: string}) => {
    return request({
        url: '/mail/get_mail_customer_liaison',
        method: 'post',
        data: param
    });
};

/**
 * 获取同事邮箱
 * @param data
 */
export const getColleagueEmails = () => {
    return request({
        url: '/mail/get_contact_mail',
        method: 'post',
        data: {}
    });
};

/**
 * 设置默认邮箱
 * @param data
 */
export const setDefault = (id: string | number) => {
    return request({
        url: '/smtp/pop/setting/defaultUse',
        method: 'post',
        data: { id }
    });
};


/**
 * 获取邮件各类型未读数
 * @param data
 */
export const getNoReadCount = () => {
    return request({
        url: '/mail/get_unread_count',
        method: 'post',
        data: {}
    });
};

/**
 * 获取邮件新邮件以及未读数
 * @param data
 */
export const getNewMailCount = () => {
    return request({
        url: '/mail/get_new_mail',
        method: 'post',
        data: {}
    });
};

/**
 * 下载附件
 * @param query
 * @returns {*}
 */
export const fileDownload = (query: any, type?: string) => {
    return request({
        url: '/mail/' + ((type == 'draft' || type == 'senting') ? 'draft_download' : 'download'),
        method: 'post',
        data: query,
        responseType: "blob",
        responseEncoding: "utf8"
    });
};

/**
 * 预览附件URL（给 window.open 使用）
 * 注意：保持与旧 sales 项目一致的 /sales 前缀，走 vite proxy。
 */
export const filePreviewUrl = (id: string | number, type?: string) => {
    return (
        '/sales/mail/' +
        ((type == 'draft' || type == 'senting') ? 'draft_download' : 'download') +
        '?attachmentId=' + id
    );
};

/**
 * 打包下载附件
 * @param query
 * @returns {*}
 */
export const downloadZip = (query: any) => {
    return request({
        url: '/mail/download_zip',
        method: 'post',
        data: query,
        responseType: "blob",
        responseEncoding: "utf8"
    });
};

/**
 * 详情里面获取邮件记录
 * @param data
 */
export const getMailList = (data) => {
    return request({
        url: '/mail/email_list',
        method: 'post',
        data
    });
};

/**
 * 详情里面记录查看
 * @param data
 */
export const setClickRecord = (customerMailId: string | number) => {
    return request({
        url: '/mail/click/click_record',
        method: 'post',
        data: { customerMailId }
    });
};

/**
 * 详情里面查看点击记录
 * @param data
 */
export const getRecord = (data) => {
    return request({
        url: '/mail/click/page_list',
        method: 'post',
        data
    });
};

/**
 * 获取邮件具体内容
 * @param data
 */
export const getEmailDetailContent = (data) => {
    return request({
        url: '/mail/getMailDetail',
        method: 'post',
        data
    });
};

/**
 * 邮件模板列表
 * @param data
 */
export const getEmailTemplateList = (data) => {
    return request({
        url: '/email/templates/page_list',
        method: 'post',
        data
    });
};

/**
 * 添加邮件模板
 * @param data
 */
export const addEmailTemplate = (data) => {
    return request({
        url: '/email/templates/save',
        method: 'post',
        data
    });
};

/**
 * 删除邮件模板
 * @param data
 */
export const delEmailTemplate = (ids: (string | number)[]) => {
    return request({
        url: '/email/templates/deleteIds',
        method: 'post',
        data: { ids }
    });
};

/**
 * 邮件黑名单列表
 * @param data
 */
export const getEmailBlackList = (data) => {
    return request({
        url: '/email/blacklist/page_list',
        method: 'post',
        data
    });
};

/**
 * 添加邮件黑名单
 * @param data
 */
export const addEmailBlackList = (data) => {
    return request({
        url: '/email/blacklist/save',
        method: 'post',
        data
    });
};

/**
 * 删除邮件黑名单
 * @param data
 */
export const delEmailBlackList = (ids: (string | number)[]) => {
    return request({
        url: '/email/blacklist/deleteIds',
        method: 'post',
        data: { ids }
    });
};

/**
 * 跟进邮件联查-批量修改跟进状态
 * @param data
 */
export const editFollowStatus = (data) => {
    return request({
        url: '/customer-follow-up-emails/edit-follow-status',
        method: 'post',
        data
    });
};

/**
 * 导出邮件
 * @param query
 * @returns {*}
 */
export const emilsExport = (query: any, type: string) => {
    return request({
        url: '/mail/download_mail_eml' + (type == 'batch' ? '_zip' : ''),
        method: 'post',
        data: query,
        responseType: "blob",
        responseEncoding: "utf8"
    });
};

/**
 * 邮件文件夹(不分页)
 * @param data
 */
export const getEmailFolder = () => {
    return request({
        url: '/user/email/folder/list',
        method: 'post',
        data: {}
    });
};

/**
 * 邮件文件夹列表
 * @param data
 */
export const getEmailFolderList = (data) => {
    return request({
        url: '/user/email/folder/page_list',
        method: 'post',
        data
    });
};

/**
 * 添加邮件文件夹
 * @param data
 */
export const addEmailFolder = (data) => {
    return request({
        url: '/user/email/folder/save',
        method: 'post',
        data
    });
};

/**
 * 删除邮件文件夹
 * @param data
 */
export const delEmailFolder = (ids: (string | number)[]) => {
    return request({
        url: '/user/email/folder/deleteIds',
        method: 'post',
        data: { ids }
    });
};

/**
 * 邮件文件夹设置列表(不分页)
 * @param data
 */
export const getEmailFolderRuleList = () => {
    return request({
        url: '/email/folder/rule/list',
        method: 'post',
        data: {}
    });
};

/**
 * 获取文件夹设置规则
 * @param data
 */
export const getFolderRule = () => {
    return request({
        url: '/email/folder/rule/get_folder_rule',
        method: 'post',
        data: {}
    });
};

/**
 * 设置文件夹规则
 * @param data
 */
export const setEmailFolderRule = (data) => {
    return request({
        url: '/email/folder/rule/saveBatch',
        method: 'post',
        data
    });
};

/**
 * 删除文件夹规则
 * @param data
 */
export const delEmailFolderRule = (ids: (string | number)[]) => {
    return request({
        url: '/email/folder/rule/deleteIds',
        method: 'post',
        data: { ids }
    });
};

/**
 * 移动文件到文件夹
 * @param data
 */
export const moveEmailToFolder = (data) => {
    return request({
        url: '/mail/mv_custom_folder',
        method: 'post',
        data
    });
};

/**
 * 邮件主题历史列表
 * @param data
 */
export const subjectRecordList = (data) => {
    return request({
        url: '/click/record/list',
        method: 'post',
        data
    });
};

/**
 * 记录邮件主题点击次数
 * @param data
 */
export const subjectRecordClick = (data) => {
    return request({
        url: '/click/record/click_record',
        method: 'post',
        data
    });
};

/**
 * 设置自动回复
 * @param data
 */
export const setAutomaticReply = (data) => {
    return request({
        url: '/smtp/pop/setting/setReplyContent',
        method: 'post',
        data
    });
};

/**
 * 陌生待处理修改处理状态
 * @param data
 */
export const setStrangeStatus = (data) => {
    return request({
        url: '/mail/mark_reply_msg_id',
        method: 'post',
        data
    });
};
