import { queryStr } from '@/utils/query';
import { request } from 'umi';
import { api } from '../api'
export interface Props {

}
/**群发策略列表 */
// export async function getMpMsgGroupSendStrategy(params?: { date?: string, state?: number, appId?: string, pageNum: number, pageSize: number }) {
//   return request(`${api}/system/mpMsgGroupSendStrategy${queryStr(params)}`);
// }
export async function getMpMsgGroupSendStrategy(params?: { date?: string, state?: number, appId?: string, pageNum: number, pageSize: number }) {
  let { appId, ...param } = params as any
  return request(`${api}/system/mpMsgGroupSendStrategy/list`, {
    method: 'POST',
    data: { ...param, mpId: appId }
  });
}
/** 新增群发策略*/
export async function addMpMsgGroupSendStrategy(params: Props) {
  return request(`${api}/system/mpMsgGroupSendStrategy`, {
    method: 'POST',
    data: params,
  });
}
/** 启动禁用*/
export async function mpMsgGroupSendStrategy(params: { strategyId: number, enabled: boolean }) {
  const { strategyId, enabled } = params
  return request(`${api}/system/mpMsgGroupSendStrategy/modifyEnable/${strategyId}/${enabled}`, {
    method: 'PUT',
  });
}
/**编辑群发策略 */
export async function editMpMsgGroupSendStrategy(params: any) {
  const { strategyId, ...param } = params
  return request(`${api}/system/mpMsgGroupSendStrategy/${strategyId}`, {
    method: 'PUT',
    data: param,
  });
}
/**预览 */
export async function preview(params: any) {
  let { mpId, fansId, msgType, ...param } = params
  return request(`${api}/system/mpMsgGroupSendStrategy/preview/${mpId}/${fansId}/${msgType}`, {
    method: 'POST',
    data: param,
  });
}
/** 本地素材预览 */
export async function localPreviewApi({ sysMediaId, mpId, fansId }: { sysMediaId: number, mpId: number, fansId: number }) {
  return request(`${api}/system/SysMedia/preview/${sysMediaId}/${mpId}/${fansId}`, {
    method: 'PUT'
  });
}
/**刷新状态 */
export async function refreshSendResult(id: any) {
  return request(`${api}/system/mpMsgGroupSendStrategy/refreshSendResult/${id}`, {
    method: 'POST',
  });
}
/** 删除群发策略*/
export async function delMpMsgGroupSendStrategy(params: { id: number, list?: any }) {
  return request(`${api}/system/mpMsgGroupSendStrategy/${params.id}`, {
    method: 'DELETE',
    data: params?.list
  });
}
/**获取群发策略详情*/
export async function getDetails(strategyId: number) {
  return request(`${api}/system/mpMsgGroupSendStrategy/${strategyId}`)
}
/**强行执行群发消息*/
export async function getRestart(strategyId: number) {
  return request(`${api}/system/mpMsgGroupSendStrategy/restart/${strategyId}`)
}
/**一键重发群发消息*/
export async function executionNowApi(strategyId: number) {
  return request(`${api}/system/mpMsgGroupSendStrategy/executionNow/${strategyId}`)
}
/** 查询策略执行日志*/
export async function catLogs(strategyId: number) {
  return request(`${api}/system/mpMsgGroupSendStrategy/executionLog/${strategyId}`);
}
/** 删除策略执行日志*/
export async function delByLog(logid: any) {
  return request(`${api}/system/mpMsgGroupSendStrategy/delLog/${logid}`, {
    method: 'GET',
  });
}

/**========================================客服消息 */
/**导出模板 */
export async function exportKNewsExcle() {
  return request(`${api}/system/${encodeURIComponent('客服消息导入模板')}.xlsx`, {
    method: 'GET',
    responseType: 'blob',
  })
}

/**导入模板批量创建 */
export async function importKnewsExcle(props: { data: FormDataEntryValue }) {
  return request(`${api}/system/mp/kefuMsgStrategy/import`, {
    method: 'POST',
    body: props.data
  })
}
/**编辑消息 */
export async function kefuMsgStrategyEdit(params: { strategyId: string }) {
  let { strategyId, ...props } = params
  return request(`${api}/system/mp/kefuMsgStrategy/${strategyId}`, {
    method: 'PUT',
    data: props,
  });
}
/**新建消息 */
export async function kefuMsgStrategyAdd(params: Props) {
  return request(`${api}/system/mp/kefuMsgStrategy`, {
    method: 'POST',
    data: params,
  });
}
/**删除消息 */
export async function kefuMsgStrategyDel(params: { strategyId: string, belongUserId: string }) {
  return request(`${api}/system/mp/kefuMsgStrategy/${params.strategyId}/${params.belongUserId}`, {
    method: 'DELETE',
  });
}
/**批量删除消息 */
export async function kefuMsgStrategyDels(params: { strategyId: string, belongUserId: string }) {
  return request(`${api}/system/mp/kefuMsgStrategy/batch/${params.strategyId}/${params.belongUserId}`, {
    method: 'DELETE',
  });
}
/**强制重启 */
export async function kefuMsgStrategyRestart(params: { strategyId: number, belongUserId: number }) {
  return request(`${api}/system/mp/kefuMsgStrategy/restart/${params.strategyId}/${params.belongUserId}`, {
    method: 'put',
  });
}
/**启用暂停客服消息 */
export async function kefuMsgStrategyOn(params: { enabled: number, strategyId?: string, belongUserId: number }) {
  return request(`${api}/system/mp/kefuMsgStrategy/${params.strategyId}/${params.belongUserId}/${params.enabled ? 'true' : 'false'}`, {
    method: 'PUT',
  });
}
/**获取消息列表 */
export async function kefuMsgStrategyGet(params: any) {
  return request(`${api}/system/mp/kefuMsgStrategy/listOfPage${queryStr(params)}`);
}
/**获取消息列表 */
export async function kefuMsgStrategyDetail(params: { strategyId: string, belongUserId: number }) {
  return request(`${api}/system/mp/kefuMsgStrategy/detail/${params.strategyId}/${params.belongUserId}`);
}
/**获取执行日志 */
export async function kefuMsgStrategyDays(params: { strategyId: number, pageNum: number, pageSize: number, mpId?: string, belongUserId: number }) {
  let { strategyId, ...props } = params
  return request(`${api}/system/mp/kefuMsgStrategy/execution/logs/${strategyId}/${queryStr(props)}`);
}
/**客服消息历史图文 */
export async function kefuMsgStrategyHistoryNews(params: { pageNum: number, pageSize: number }) {
  return request(`${api}/system/mp/kefuMsgStrategy/historyNews${queryStr(params)}`);
}
/**客服消息预览 */
export async function sendPreview(params: { fansIds?: string, mpIds: any[] }) {
  let { fansIds, ...param } = params
  return request(`${api}/system/mp/kefuMsgStrategy/preview/${param?.mpIds[0]}/${fansIds}`, {
    method: 'POST',
    data: param
  });
}
/**立即发送 */
export async function kefuMsgStrategyExecutionNow(params: { strategyId: string, belongUserId: number }) {
  return request(`${api}/system/mp/kefuMsgStrategy/executionNow/${params.strategyId}/${params.belongUserId}`);
}

/**
 *  模板消息
 *  */

/** 获取公众号模板列表 */
export async function templateMsgApi(params: { mpId: string }) {
  return request(`${api}/system/mp/templateMsg/list/${params.mpId}`);
}

/** 模板消息任务列表 */
export async function listOfPageApi(params: { mpId?: string, taskName?: string, executionStatus?: number, pageNum: number, pageSize: number }) {
  return request(`${api}/system/mp/templateMsg/strategy/listOfPage`, {
    method: 'POST',
    data: params
  });
}

/** 同步公众号模板列表 */
export async function syncTemplateApi(params: { mpId: string }) {
  return request(`${api}/system/mp/templateMsg/syncTemplate/${params.mpId}`, {
    method: 'PUT'
  });
}

/** 新建模板消息 */
export async function strategyApi(params: {
  mpId: string,
  templateId: number,
  taskName: string,
  fansAll: boolean,
  fansFilterTagType?: number,
  fansFilterTagids?: any[]
  contentFields: any[],
  url?: string,
  miniprogramAppid?: string,
  miniprogramPagepath?: string,
  triggerType: number,
  sendTime?: string,
  enabled: boolean,
  isUrl: boolean,
  fansFilterSex?: number
}) {
  return request(`${api}/system/mp/templateMsg/strategy`, {
    method: 'POST',
    data: params
  });
}

/** 修改模板消息 */
export async function modifyStrategyApi(params: {
  id: number,
  mpId: string,
  templateId: number,
  taskName: string,
  fansAll: boolean,
  fansFilterTagType?: number,
  fansFilterTagids?: any[]
  contentFields: any[],
  url?: string,
  miniprogramAppid?: string,
  miniprogramPagepath?: string,
  triggerType: number,
  sendTime?: string,
  enabled: boolean,
  isUrl: boolean,
  fansFilterSex?: number
}) {
  let { id, ...param } = params
  return request(`${api}/system/mp/templateMsg/strategy/${id}`, {
    method: 'PUT',
    data: param
  });
}

/** 删除模板消息 */
export async function temDelApi(params: { id: number }) {
  return request(`${api}/system/mp/templateMsg/strategy/${params.id}`, {
    method: 'DELETE'
  });
}

/** 模板消息预览 */
export async function temPreviewApi(params: {
  mpId: number,
  fansIds: number,
  templateId: number,
  taskName: string,
  fansAll: boolean,
  fansFilterTagType?: number,
  fansFilterTagids?: any[]
  contentFields: any[],
  url?: string,
  miniprogramAppid?: string,
  miniprogramPagepath?: string,
  triggerType: number,
  sendTime?: string,
  enabled: boolean,
  isUrl: boolean,
  fansFilterSex?: number
}) {
  let { mpId, fansIds, ...param } = params
  return request(`${api}/system/mp/templateMsg/preview/${mpId}/${fansIds}`, {
    method: 'post',
    data: param
  });
}