import {
  post,
  get
} from '../utils/http'

export function getPart(params) {
  return post(`/DxPart/find/recursion`, params)
}

export function expandBOM(id, level = -1) {
  return post(`/DxPart/expandBOM`, { partId: id, level: level })
}

export function findByNumber(params) {
  return get(`/DxPart/findByNumber`, params)
}

export function getPartByES(params) {
  return post(`/DxDocument/search`, params)
}

export function addPart(params) {
  return post(`/DxPart/recursion`, params)
}

export function addBaseline(params) {
  return post(`/DxBaseline/recursion`, params)
}

export function getBaseline(params) {
  return post(`/DxBaseline/find/recursion`, params)
}

export function getBaselineMemeber(params) {
  return post(`/DxBaseline/getMemberMatrix`, params)
}

export function checkoutPart(objId) {
  return post(`/DxPart/version/${objId}/checkoutOverride`)
}

export function undoCheckout(objId) {
  return post(`/DxPart/version/${objId}/undoCheckoutOverride`)
}

export function checkinPart(objId) {
  return post(`/DxPart/version/${objId}/checkin`)
}

export function getLinks(params) {
  return post(`/usageLinks/search`, params)
}

// 保存CA数据
export function saveCAData(params) {
  return post('/DxChangeActivity/recursion', params)
}

export function getPartDetail({ id, openProps }) {
  const params = {
    'openProps': [],
    'pageFrom': 1,
    'pageSize': 10,
    'searchItems': {
      'children': [
      ],
      'items': [
        {
          'fieldName': 'id',
          'operator': 'EQ',
          'value': id
        }
      ],
      'operator': 'AND'
    }
  }
  params.openProps = openProps
  return post('/DxPart/find/recursion', params)
}

// #region 20.10.25 fangming 临时操作关系,希望前段架构设计通用接口
// 获取关系数据
export function loadLink(objId, linkPropName) {
  const partSearchItem = {
    searchItems: {
      items: [{
        fieldName: 'id',
        operator: 'EQ',
        value: objId
      }]
    },
    pageFrom: 1,
    pageSize: 20,
    openProps: [{
      name: linkPropName,
      pageFrom: 1,
      pageSize: 10,
      openProps: [{
        name: 'target',
        pageFrom: 1,
        pageSize: 10
      }]
    }]
  }
  return new Promise((resolve, reject) => {
    post(`/usageLinks/search`, partSearchItem).then((r) => {
      alert(r)
      resolve(r)
    }).catch(error => {
      reject(error)
    })
  })
}

// 移除有关系
export function removeLinks(objId, linkPropName, linkIds) {
  const part = {
    operator: 'NO_CHANGE',
    id: objId
  }
  part[linkPropName] = linkIds.map(linkId => {
    return {
      operator: 'REMOVE',
      id: linkId
    }
  })
  return post(`/DxPart/recursion`, part)
}

// 添加关系
export function addLinks(objId, linkPropName, targetIds) {
  const part = {
    operator: 'NO_CHANGE',
    id: objId
  }
  part[linkPropName] = targetIds.map(linkId => {
    return {
      operator: 'ADD',
      target: {
        operator: 'NO_CHANGE',
        id: linkId
      }
    }
  })
  return post(`/DxPart/recursion`, part)
}

// 更新关系属性
export function updateLink(objId, linkPropName, linkData) {
  const part = {
    operator: 'NO_CHANGE',
    id: objId
  }
  part[linkPropName] = linkData
  return post(`/DxPart/recursion`, part)
}

export function getWorkingCopy(modelName, params) {
  return get(`/${modelName}/version/getWorkingCopy/${params}`)
}

export function getLayouts(typeKey, layoutType) {
  return new Promise(async(resolve, reject) => {
    get('/DxModelComponent/findModelComponent', { modelDefName: typeKey, layoutType }).then(res => {
      if (res.items && res.items.content) {
        resolve({
          items: res.items.content
        })
      }
    })
  })
}

// #endregion