import axios from 'bc-common-ui-vue3/src/bc-components/bc-libs/api.request'
import { getTokenParams } from 'bc-common-ui-vue3/src/bc-components/bc-libs/token-util'

const prefix = 'api/v1/base/company'

/**
 * 分页查询方法
 * @param params
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getCompanyPage = (params) => {
  return axios.request({
    url: prefix + '/page',
    params: params,
    method: 'get',
  })
}

/**
 * 根据参数查询列表
 * @param params
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getCompanyList = (params) => {
  return axios.request({
    url: prefix + '/list',
    params: params,
    method: 'get',
  })
}

/**
 * 新增
 * @param params
 * @returns {Promise<any>}
 */
export const addCompany = (params) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: prefix + '/add',
        method: 'POST',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

/**
 * 修改
 * @param params
 * @returns {Promise<any>}
 */
export const editCompany = (params) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: prefix + '/merge',
        method: 'POST',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

/**
 * 删除
 * @param id
 * @returns {Promise<any>}
 */
export const deleteCompany = (id) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: prefix + '/delete/' + id,
        method: 'POST',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

/**
 * 分页查询方法
 * @param params
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getAsyncTree = (params) => {
  return axios.request({
    url: prefix + '/asyncTree',
    params: params,
    method: 'get',
  })
}

/**
 * 获得异步区域-单位 ztree的配置
 * @returns {{dataFilter: (function(*, *, *): *), enable: boolean, autoParam: [string], otherParam: {}, type: string, contentType: string, url: *}}
 */
export const getAreaCompanyTreeAsyncConfig = () => {
  let tokenParams = getTokenParams()
  //tokenParams.isFull = "false";
  let config = {
    enable: true,
    type: 'get',
    contentType: 'application/json',
    dataFilter: (treeId, parentNode, responseData) => {
      //console.log(responseData)
      return responseData.result
    },
    url: prefix + '/asyncTree',
    autoParam: ['id=parentId', 'type'],
    otherParam: tokenParams,
  }
  return config
}
