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'
import config from '@/config'

const systemPrefix = config.deploymentStandAlone ? '' : 'cas/'

const prefix = systemPrefix + 'api/v1/base/area'

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

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

/**
 * 新增
 * @param params
 * @returns {Promise<any>}
 */
export const addArea = (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 editArea = (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 deleteArea = (id) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: prefix + '/delete/' + id,
        method: 'POST',
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getAllTree = (areaIds, isFull) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: prefix + '/tree',
        method: 'GET',
        params: { areaIds: areaIds, isFull: isFull },
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const getAsyncConfig = () => {
  let tokenParams = getTokenParams()
  let config = {
    enable: true,
    type: 'get',
    contentType: 'application/json',
    dataFilter: (treeId, parentNode, responseData) => {
      //console.log(responseData)
      return responseData.result
    },
    url: prefix + 'tree/async',
    autoParam: ['id=parentid'],
    otherParam: tokenParams,
  }
  return config
}
