import request from '../utils/request'
import { projectName } from '../config'
let hi_request=null;

if(window.HIVUI_SETTING&&window.HIVUI_SETTING.request){
  hi_request=window.HIVUI_SETTING.request;
}
const saveUrl = (import.meta.env.VITE_APP_SAVEDATA||process.env.VITE_APP_SAVEDATA).replace("[projectName]",projectName)
const queryUrl = (import.meta.env.VITE_APP_QUERYDATA||process.env.VITE_APP_QUERYDATA).replace("[projectName]",projectName)
function _request(opt){
  if(hi_request){
    return hi_request(opt);
  }else{
    return request(opt);
  }
}
export function fetchList(query) {
  return _request({
    url: '/portal/list',
    method: 'get',
    params: query
  })
}

export function fetchColumns(query) {
  return _request({
    url: '/portal/columns',
    method: 'get',
    params: query
  })
}

export function getPortal() {
  /*return _request({
    url: queryUrl,
    method: 'post',
    data: qs.stringify({
      modelFilePath: `${projectName}.pro/ptpz.dir/xtgn.dir/sy.modeldir/datadir.datadir/symksj.store/BizEntity_SBuu3vUc.entity`,
      __funcpath: `/${projectName}/xtgl/xtfg/xtsy/funcdir/sygn.func`,
      pageNum: -1,
      pageSize: -1
    })
  })*/
}

//取系统门户
export function getSysPortal() {
  /*return _request({
    url: queryUrl,
    method: 'post',
    data: qs.stringify({
      modelFilePath: `${projectName}.pro/ptpz.dir/xtgn.dir/sy.modeldir/datadir.datadir/symkglysjj.store/SqlEntity_fb3qb9Op.sqlentity`,
      __funcpath: `/${projectName}/xtgl/xtfg/xtsy/funcdir/sygn.func`,
      pageNum: -1,
      pageSize: -1
    })
  })*/
}



export function savePortal(insert, update, remove) {
  let body = []

  if (insert) {
    for (let i = 0; i < insert.length; i++) {
      insert[i]["$state"] = "rsInsert"
      body.push(insert[i])
    }

  }

  if (update) {
    for (let i = 0; i < update.length; i++) {
      update[i]["$state"] = "rsUpdate"
      body.push(update[i])
    }

  }
  if (remove) {
    for (let i = 0; i < remove.length; i++) {
      remove[i]["$state"] = "rsDelete"
      body.push(remove[i])
    }
  }
  body = [
    {
      "datas": body,
      "dsName": "BizEntity_SBuu3vUc",
      "modelFilePath": `${projectName}.pro/ptpz.dir/xtgn.dir/sy.modeldir/datadir.datadir/symksj.store/BizEntity_SBuu3vUc.entity`,
    }]
  /*return _request({
    url: saveUrl,
    method: 'post',
    data: qs.stringify({
      __funcpath: `/${projectName}/xtgl/xtfg/xtsy/funcdir/sygn.func`,
      __body:JSON.stringify(body)
    })
  })*/
}

export function addPortal(insert) {
    let inserts = []
    inserts.push(insert)
    return savePortal(inserts)
}
export function updatePortal(oldPack,updatePack) {
  let updates = []
  updatePack["$old"] = oldPack
  updates.push(updatePack)
  return savePortal(null,updates)
}
