/*
 * @Author:lily
 * @Date: 2022-10-31 17:14:23
 * @LastEditors: error: git config user.name && git config user.email & please set dead value or install git
 * @Description:自定义表单模块接口方法
 */
import request from '@/utils/request';
import { Http, useHttp } from '@crami/http';
import { getInfo, ICP_CONFIG } from '@remote/icpx-platform/utils';
import { message } from '@crami/ui';
import { Base64 } from 'js-base64';
/**
 * 新增表单项
 * @returns
 */
export async function addItem() {
  return request.request<any, any>({
    url: '/system/fun/getApplication',
    method: 'POST',
  });
}

/**
 * 保存表单数据
 * @param templateId 模板id
 * @param data 表单数据
 * @param modelCode 表单模型
 * @param saveVersionInfo 版本信息
 * @returns Promise
 */
export function saveFormData(
  modelCode: string,
  templateId: string,
  data: any,
  saveVersionInfo: any,
) {
  return useHttp({
    url: '/system/modelForm/saveFormData',
    method: 'post',
    data: {
      templateId: templateId,
      data: JSON.stringify(data),
      mode: saveVersionInfo.mode,
      isPrimary: saveVersionInfo.isPrimary,
      versionId: saveVersionInfo.versionId,
      objType: modelCode,
      lang: getInfo(ICP_CONFIG).lang || 'zhs',
      companyId: getInfo(ICP_CONFIG).companyId,
      connect: getInfo(ICP_CONFIG).connect,
    },
  });
}

/**
 * 获取表单设计信息
 * @param modelCode 模型id
 * @returns
 */
export async function getFormDesignData(templateId: string, versionId?: string) {
  // TODO: 修改成用useHttp调用
  return useHttp({
    url: '/system/modelForm/getFormDesignData',
    method: 'POST',
    data: {
      // ObjType: modelCode,
      templateId,
      versionId,
      lang: getInfo(ICP_CONFIG).lang || 'zhs',
      companyId: getInfo(ICP_CONFIG).companyId,
      connect: getInfo(ICP_CONFIG).connect,
    },
  });
}

/**
 * @description: 表单模板发布
 * @param {string} templateId 模板id
 * @param {boolean} publish 发布状态，true发布，false取消发布
 * @return {*} Promise
 */
export async function publishTemplate(templateId: string, publish: boolean) {
  return useHttp({
    params: {
      templateId,
      publish,
      lang: getInfo(ICP_CONFIG).lang || 'zhs',
      companyId: getInfo(ICP_CONFIG).companyId,
      connect: getInfo(ICP_CONFIG).connect,
    },
    method: 'post',
    url: '/api/system/modelForm/publish',
  });
}

/**
 * @description: 设为主版本
 * @param {string} templateId 模板id
 * @param {string} versionId 版本id
 * @return {*} Promise
 */
export function setMainVersion(templateId: string, versionId: string) {
  return useHttp({
    params: {
      templateId,
      versionId,
      lang: getInfo(ICP_CONFIG).lang || 'zhs',
      companyId: getInfo(ICP_CONFIG).companyId,
      connect: getInfo(ICP_CONFIG).connect,
    },
    method: 'post',
    url: '/api/system/modelForm/setMain',
  });
}
/**
 * @description: 获取模型中表单模板的数量
 * @param {string} objectType 模型名
 * @return {*}
 */
export function getModelTemplateNum(objectType: string) {
  return useHttp({
    url: '/api/system/modelForm/modelTableValueCount',
    method: 'post',
    params: {
      objectType,
      lang: getInfo(ICP_CONFIG).lang || 'zhs',
      companyId: getInfo(ICP_CONFIG).companyId,
      connect: getInfo(ICP_CONFIG).connect,
    },
  });
}
/**
 * @description: 获取表单模型下可用的-表单模板id
 * @param {string} objectType 表单模型
 * @return {*}
 */
export function getModelDesignList(objectType: string) {
  return useHttp({
    url: '/api/system/modelForm/getModelDesignList',
    method: 'POST',
    params: {
      objectType,
      lang: getInfo(ICP_CONFIG).lang || 'zhs',
      companyId: getInfo(ICP_CONFIG).companyId,
      connect: getInfo(ICP_CONFIG).connect,
    },
  });
}

/**
 * @description: 删除表单实例
 * @param {string} objectId 表单实例id
 * @return {*}
 */
export function deleteFbObjectData(objectId: string) {
  return useHttp({
    url: '/api/system/modelForm/deleteFbObjectData',
    method: 'post',
    params: {
      objectId,
      lang: getInfo(ICP_CONFIG).lang || 'zhs',
      companyId: getInfo(ICP_CONFIG).companyId,
      connect: getInfo(ICP_CONFIG).connect,
    },
  });
}
/**
 * @description: 导出表单
 * @param {*} id 表单id
 * @param {*} fileName 文件名
 * @return {*} void
 */
export function exportFormFile(id, fileName) {
  const url = '/api/system/modelForm/exportExcel?param=';
  const param = {
    formDataId: id,
    lang: getInfo(ICP_CONFIG).lang || 'zhs',
    companyId: getInfo(ICP_CONFIG).companyId,
    connect: getInfo(ICP_CONFIG).connect,
  };
  window.open(url + Base64.encode(JSON.stringify(param)));
  // Http.get<any>({
  //   url: url + Base64.encode(JSON.stringify(param)),
  //   responseType: 'blob',
  // }).then(data => {
  //   if (data.code === 200) {
  //     const content = data.data;
  //     const blob = new Blob([content]);
  //     if ('download' in document.createElement('a')) {
  //       const elink = document.createElement('a');
  //       elink.download = fileName;
  //       elink.style.display = 'none';
  //       elink.href = URL.createObjectURL(blob);
  //       document.body.appendChild(elink);
  //       elink.click();
  //       URL.revokeObjectURL(elink.href); // 释放URL 对象
  //       document.body.removeChild(elink);
  //     }
  //   } else {
  //     message.error(data.msg);
  //   }
  // });
}

/**
 * @description: 获取模型中的字段
 * @param {string} objectType 模型名
 * @param {string} templateId 模板id
 * @return {*}
 */
export function getFormUsedFields(objectType: string, templateId: string) {
  return useHttp({
    url: '/api/system/modelForm/getFormUsedFields',
    method: 'post',
    params: {
      companyId: getInfo(ICP_CONFIG)?.companyId,
      connect: getInfo(ICP_CONFIG)?.connect,
      lang: getInfo(ICP_CONFIG).lang || 'zhs',
      objectType,
      templateId,
    },
  });
}
/**
 * @description: 获取表单模型列表
 * @return {*}
 */
export function getFormModelList() {
  return useHttp({
    url: '/api/system/modelForm/getModelListByFlowFormPermission',
    method: 'POST',
    params: {
      companyId: getInfo(ICP_CONFIG)?.companyId,
      connect: getInfo(ICP_CONFIG)?.connect,
      lang: getInfo(ICP_CONFIG)?.lang || 'zhs',
    },
  });
}
/**
 * @description: 获取模型下拉选项属性值
 * @param {string} objectType 表单模型
 * @param {string} fieldName 字段名
 * @return {*}
 */
export function getModelEnumList(objectType: string, fieldName: string) {
  return useHttp({
    url: '/api/system/modelForm/getModelEnumList',
    method: 'post',
    params: {
      companyId: getInfo(ICP_CONFIG)?.companyId,
      connect: getInfo(ICP_CONFIG)?.connect,
      lang: getInfo(ICP_CONFIG)?.lang,
      objectType,
      fieldName,
    },
  });
}

/**
 * @description: 新建/编辑表单分类
 * @param {string} name 分类名称
 * @param {string} objectId 分类id,若有此参数则为编辑，否则为新建分类
 * @return {*}
 */
export function addFormCategory(name: string, objectId?: string) {
  return useHttp({
    url: '/api/system/modelForm/addFormCate',
    method: 'post',
    params: {
      companyId: getInfo(ICP_CONFIG)?.companyId,
      connect: getInfo(ICP_CONFIG)?.connect,
      lang: getInfo(ICP_CONFIG)?.lang,
      name,
      objectId,
    },
  });
}
/**
 * @description: 根据分类Id删除数据
 * @param {string} objectId 分类Id
 * @return {*}
 */
export function deleteFormCategory(objectId: string) {
  return useHttp({
    url: '/api/system/modelForm/deleteFormCate',
    method: 'post',
    params: {
      companyId: getInfo(ICP_CONFIG)?.companyId,
      connect: getInfo(ICP_CONFIG)?.connect,
      lang: getInfo(ICP_CONFIG)?.lang,
      objectId,
    },
  });
}
/**
 * @description: 添加表单到某分类
 * @param {string} objectId 分类Id
 * @param {string} modelName 模型名称
 * @return {*}
 */
export function addModelToCategory(objectId: string, modelName: string) {
  return useHttp({
    url: '/api/system/modelForm/addModelToCate',
    method: 'post',
    params: {
      companyId: getInfo(ICP_CONFIG)?.companyId,
      connect: getInfo(ICP_CONFIG)?.connect,
      lang: getInfo(ICP_CONFIG)?.lang,
      objectId,
      modelName,
    },
  });
}

/**
 * @description: 获取可选择的分类
 * @return {*} 返回分类列表
 */
export function getFormCateList(modelName?) {
  return useHttp({
    url: '/api/system/modelForm/getFormCateList  ',
    method: 'post',
    params: {
      companyId: getInfo(ICP_CONFIG)?.companyId,
      connect: getInfo(ICP_CONFIG)?.connect,
      lang: getInfo(ICP_CONFIG)?.lang,
      // modelName,
    },
  });
}
