import { defHttp } from '/@/utils/http/axios';
import {
  EmailParams,
  MessageResultModel,
  MessageTemplate,
  MessageTemplateParams,
  MessageTemplateResultModel,
  SmsParams,
} from '/@/api/message/model/templateModel';
import { MessageConfigResultModel } from '/@/api/message/model/configModel';

enum MessageTemplateApi {
  TEMPLATE_URL = '/template',
}

/**
 * 短信发送
 * @param params 发送参数
 */
export const sendSms = (params: SmsParams) => {
  return defHttp.post<MessageResultModel>({
    url: MessageTemplateApi.TEMPLATE_URL + '/send_sms',
    params,
  });
};

export const sendEmail = (params: EmailParams) => {
  return defHttp.post<MessageResultModel>({
    url: MessageTemplateApi.TEMPLATE_URL + '/send_email',
    params,
  });
};

export const messageTemplatePage = (params: MessageTemplateParams) =>
  defHttp.get<MessageTemplateResultModel>({
    url: MessageTemplateApi.TEMPLATE_URL,
    params,
  });

/**
 * 更新或保存MessageTemplate配置
 * @param params 参数
 * @param isUpdate 是否更新
 */
export const saveOrEditMessageTemplate = (params: MessageTemplate, isUpdate: boolean) => {
  if (isUpdate) {
    return defHttp.put<MessageTemplateResultModel>({
      url: MessageTemplateApi.TEMPLATE_URL,
      params,
    });
  } else {
    return defHttp.post<MessageTemplateResultModel>({
      url: MessageTemplateApi.TEMPLATE_URL,
      params,
    });
  }
};

/**
 * 删除MessageConfig
 * @param ids 删除id数组
 */
export const deleteMessageTemplate = (ids: string[]) =>
  defHttp.delete({
    url: MessageTemplateApi.TEMPLATE_URL,
    data: {
      ids: ids,
    },
  });
/**
 * 更新模板状态
 * @param id 配置id
 * @param status 状态
 */

interface dataType {
  id: string;
  templatePurpose: string;
  messageType: string;
  status: number;
  tenantId: string;
}
export const setMessageTemplateStatus = (data: dataType) =>
  defHttp.put<MessageConfigResultModel>({
    url: MessageTemplateApi.TEMPLATE_URL,
    data,
  });
