import axios from '@/common/services/axios-instance'

class ApplicationService {
  // 同步至数据库
  formSync2DB(id) {
    return axios({
      url: `api/framework/v1/onlineform/doDbSynch/${id}/normal`,
      method: 'post',
    })
  }

  // 复制表单
  copyModuleForm(params) {
    return axios({
      url: `api/framework/v1/we-app-module/copy`,
      method: 'get',
      params,
    })
  }

  changeModuleFormStatus(params) {
    return axios({
      url: `api/framework/v1/we-app-module/update-status`,
      method: 'get',
      params,
    })
  }

  // 获取上级菜单列表
  getMenuList(params) {
    return axios({
      url: 'api/framework/v1/onl-form-menu/submenu?parentId=' + params.parentId,
      method: 'get',
    })
  }

  // 获取应用菜单分类
  getAppMenuCategoryTree(params) {
    return axios({
      url: 'api/framework/v1/we-app-module-menu/tree',
      method: 'get',
      params: params,
    })
  }

  getMenuByCode(params) {
    return axios({
      url: `api/framework/v1/we-app-module-menu/menu`,
      method: 'get',
      params,
    })
  }

  // 获取模块树
  getModuleTree(params) {
    return axios({
      url: 'api/framework/v1/we-app-module/tree',
      method: 'get',
      params: params,
    })
  }

  // 菜单清缓存
  clearMenuCache() {
    return axios({
      url: 'api/framework/v1/we-app-module-menu/clear-cache',
      method: 'get',
    })
  }

  // 获取实体表单页面
  getEntityFormData(params) {
    return axios({
      url: 'api/framework/v1/page/xml/pagelist',
      method: 'get',
      params,
    }).then((res) => {
      return res.data
    })
  }

  getPageInfo(params) {
    return axios({
      url: 'api/framework/v1/page/xml/page',
      method: 'get',
      params,
    })
  }

  // 启用停用应用
  releaseApp(params, listType) {
    return axios({
      url: `api/framework/v1/${listType === 'share' ? 'we-app-node' : 'we-app'}/update-status`,
      method: 'get',
      params: params,
    })
  }

  // 初始化表单
  resetForm(ids) {
    return axios({
      url: `api/framework/v1/onlineform/disDbSyncBatch?ids=${ids}`,
      method: 'post',
    })
  }

  // 获取应用数量
  getAppCount() {
    return axios({
      url: 'api/framework/v1/we-app/tab-app-count',
      method: 'get',
    })
  }

  // 导入应用
  importApp(params) {
    return axios.post('api/framework/v1/we-app/import', params)
  }

  // 导入表单
  importForm(params) {
    return axios({
      url: `api/framework/v1/we-app-module/import`,
      method: 'post',
      data: params,
    })
  }

  // 获取表单信息
  getFormByModuleId(id) {
    return axios.get(`api/framework/v1/we-app-module/get-formid?id=${id}`).then((res) => res.data)
  }

  // 根据builder表单的formId获取表单设计
  getOnlineFormById(id) {
    return axios.get(`api/framework/v1/onlineform/getById/${id}/true`).then((res) => res.data)
  }

  // 获取事件
  getEventInfo(type, params) {
    const method = type === 'frontend' ? 'getFormFrontMethod' : 'getFormMethod'
    return axios({
      url: `api/framework/v1/onlineform/${method}/${params.id}`,
      method: 'get',
    })
  }

  // 保存事件
  saveEvent(type, data) {
    const method = type === 'frontend' ? 'saveFormFrontMethod' : 'saveFormMethod'
    return axios({
      url: `api/framework/v1/onlineform/${method}`,
      method: 'post',
      data,
    })
  }

  // 删除事件
  deleteEvents(params) {
    return axios({
      url: `api/framework/v1/onlineform/deleteFormMethods`,
      method: 'delete',
      params,
    })
  }

  getFormData(id) {
    return axios.get(`api/framework/v1/onlineform/getById/${id}/true`).then((res) => {
      return res.data
    })
  }

  // 获取自定义表单的列表
  getOnlFormListData(appId, formType) {
    const params = {
      maxResults: -1,
      startPosition: 0,
      expressions: [
        {
          dataType: 'str',
          name: 'formCategory',
          op: 'eq',
          stringValue: appId,
        },
        {
          dataType: 'str',
          name: 'isDbSynch',
          op: 'ne',
          stringValue: 'N',
        },
      ],
    }
    if (formType !== undefined) {
      // @ts-ignore
      params.expressions.push({
        dataType: 'long',
        name: 'formType',
        op: 'eq',
        longValue: formType,
      })
    }
    return axios({
      url: `api/framework/v1/onlineform/list`,
      method: 'post',
      data: params,
    }).then((res) => {
      return res.data.data.map((d) => {
        return {
          id: d.id,
          name: d.tableTxt,
          formType: d.formType,
        }
      })
    })
  }

  getOnlFormFieldsById(formId) {
    if (formId) {
      return this.getOnlineFormById(formId).then((data) => data.fields)
    }
    return Promise.resolve([])
  }

  getFormulaFields(formId) {
    if (formId) {
      return this.getOnlFormFieldsById(formId).then((fields) => {
        return fields
          ?.filter((f) => f.dataType !== 'OpinionView')
          .concat([{ caption: '文档唯一ID(系统)', name: 'id', dataType: 'string' }])
      })
    }
    return Promise.resolve([])
  }

  getFormConstants(formId) {
    if (formId) {
      return this.getOnlFormFieldsById(formId).then((fields) => {
        return fields
          .filter((f) => f.attr?.selectListItem)
          .map((f) => ({
            caption: f.caption,
            items: f.attr?.selectListItem,
          }))
      })
    }
    return Promise.resolve([])
  }
}

export default new ApplicationService()
