import { defHttp } from '@jdlinker/func';
import { useMessage } from '@jdlinker/func';
const { createConfirm } = useMessage();

export const modify = (id: string, data: any) => defHttp.put({ url: `/scene/${id}`, data }, { isTransformResponse: false });

export const save = (data: any) => defHttp.post({ url: `/scene`, data }, { isTransformResponse: false });

export const detail = (id: string) => defHttp.get({ url: `/scene/${id}` }, { isTransformResponse: false });

export const list = (data: any) => {
  // const params = data?.sorts;
  delete data.terms;
  const params = { ...data };
  return defHttp.post({ url: '/scene/list', data, params }, { isTransformResponse: false });
};

export const query = (data: any) => defHttp.get({ url: '/scene/list', data }, { isTransformResponse: false });

export const _query = (data: any) => defHttp.post({ url: '/scene/_query', data }, { isTransformResponse: false });

export const deleteOne = (id: string, handleSuccess) =>
  defHttp.delete({ url: `/scene/delete/${id}` }).then(() => handleSuccess());

export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: '/scene/deleteBatch', data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
};

export const _delete = (id: string) => defHttp.delete({ url: `/scene/${id}` });

export const _action = (id: string, type: '_disable' | '_enable') => defHttp.put({ url: `/scene/${id}/${type}` });

/**
 * 手动触发
 * @param id
 * @returns
 */
export const _execute = (id: string) => defHttp.post({ url: `/scene/${id}/_execute` });

// 内置参数
export const queryBuiltInParams = (data: any, params?: any) => defHttp.post({ url: `/scene/parse-variables`, data, params });

export const getParseTerm = (data: Record<string, any>) =>
  defHttp.post({ url: `/scene/parse-term-column`, data }, { isTransformResponse: false });

export const queryAlarmList = (data: any) => {
  const params = data?.sorts;
  return defHttp.post({ url: `/alarm/config/list`, data, params }, { isTransformResponse: false });
};
