import request from '@/utils/request'

// 获取字典类型列表
export function getDictTypes(params) {
  return request({
    url: '/backend/dict/types',
    method: 'get',
    params
  })
}

// 根据ID获取字典类型
export function getDictTypeById(id) {
  return request({
    url: `/backend/dict/types/${id}`,
    method: 'get'
  })
}

// 添加字典类型
export function addDictType(data) {
  return request({
    url: '/backend/dict/types',
    method: 'post',
    data: {
      ...data,
      is_global: data.is_global !== undefined ? data.is_global : 0
    }
  })
}

// 更新字典类型
export function updateDictType(id, data) {
  return request({
    url: `/backend/dict/types/${id}`,
    method: 'put',
    data: {
      ...data,
      is_global: data.is_global !== undefined ? data.is_global : 0
    }
  })
}

// 删除字典类型
export function deleteDictType(id) {
  return request({
    url: `/backend/dict/types/${id}`,
    method: 'delete'
  })
}

// 获取字典项列表
export function getDictItems(params) {
  return request({
    url: '/backend/dict/items',
    method: 'get',
    params
  })
}

// 根据ID获取字典项
export function getDictItemById(id) {
  return request({
    url: `/backend/dict/items/${id}`,
    method: 'get'
  })
}

// 添加字典项
export function addDictItem(data) {
  return request({
    url: '/backend/dict/items',
    method: 'post',
    data
  })
}

// 更新字典项
export function updateDictItem(id, data) {
  return request({
    url: `/backend/dict/items/${id}`,
    method: 'put',
    data
  })
}

// 删除字典项
export function deleteDictItem(id) {
  return request({
    url: `/backend/dict/items/${id}`,
    method: 'delete'
  })
}

// 根据字典编码获取字典项（用于业务查询）
export function getDictItemsByCode(code, includeDisabled = false) {
  return request({
    url: `/backend/dict/items/code/${code}`,
    method: 'get',
    params: {
      include_disabled: includeDisabled ? '1' : '0'
    }
  })
}

// 批量更新字典项排序
export function batchUpdateDictItemSort(data) {
  return request({
    url: '/backend/dict/items/sort',
    method: 'put',
    data
  })
}

