import request from '@/utils/request'

// application/x-www-form-urlencoded表单形式
function urlencoded(data) {
  const newData = Object.keys(data).map((key) => {
    return `${key}=${encodeURIComponent(data[key])}`
  }).join('&')
  return newData
}

export function addDict(data) {
  return request({
    url: '/cms/dict',
    method: 'post',
    data
  })
}
export function listDict(data) {
  return request({
    url: '/cms/dict/list',
    method: 'post',
    data
  })
}
export function updateDict(data) {
  return request({
    url: '/cms/dict',
    method: 'put',
    data
  })
}
export function removeDict(params) {
  return request({
    url: '/cms/dict',
    method: 'delete',
    params
  })
}

export function addDictLabel(data) {
  return request({
    url: '/cms/dict/pushDictLabel',
    method: 'post',
    data
  })
}
export function listDictLabel(data) {
  return request({
    url: '/cms/dict/listDictLabel',
    method: 'post',
    data
  })
}
export function updateDictLabel(data) {
  return request({
    url: '/cms/dict/updateDictLabel',
    method: 'put',
    data
  })
}
export function removeDictLabel(params) {
  return request({
    url: '/cms/dict/deleteDictLabel',
    method: 'delete',
    params
  })
}
export function cleanCache() {
  return request({
    url: '/cms/dict/cleanAppDictLabelCache',
    method: 'get'
  })
}
/**
 * 保存违法行为大类
 */
export function saveIllegalAct(data) {
  return request({
    url: '/cmsDictCode/saveIllegalAct',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: urlencoded(data)
  })
}
/**
 * 违法行为中、小类保存
 */
export function saveIllegalActM(data) {
  return request({
    url: '/cmsDictCode/saveIllegalActM',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: urlencoded(data)
  })
}
/**
 * 根据类型获取字典代码列表
 * @param {data} 分页参数
 */
export function codeListPageByType(data) {
  return request({
    url: '/cmsDictCode/codeListPageByType',
    method: 'post',
    data
  })
}
/**
 * 违法行为更新
 */
export function updateDictCode(data) {
  return request({
    url: '/cmsDictCode/updateDictCode',
    method: 'post',
    data
  })
}
/**
 * 根据id获取子列表
 */
export function codeChildrenList(data) {
  return request({
    url: '/cmsDictCode/codeChildrenList',
    method: 'post',
    data
  })
}
/**
 * 获取字典对应级联选项数组
 */
export function getDictTreeList(type) {
  return request({
    url: '/cmsDictCode/getDictTreeList?type=' + type,
    method: 'get'
  })
}
