import request from '@/utils/request'

function customParamsSerializer(params) {
  let result = '';
  for (let key in params) {
    if (params.hasOwnProperty(key)) {
      if (Array.isArray(params[key])) {
        params[key].forEach((item, index) => {
          if (item.key) {
            result +=
              `${key}[${index}][key]=${item.key}&${key}[${index}][op]=${item.op}&${key}[${index}][value]=${item.value}&`;

          } else {
            result += `${key}[${index}]=${item}&`
          }
        });
      } else {
        result += `${key}=${params[key]}&`;
      }
    }
  }
  return result.slice(0, -1);
}


export function save(data) {
  return request({
    url: '/api/admin/parameter/save',
    method: 'post',
    data
  })
}

export function store(data) {
  return request({
    url: '/api/admin/parameter/store',
    method: 'post',
    data
  })
}
export function getparameter(param, loading = true) {
  return request({
    url: '/api/admin/parameter/show',
    method: 'get',
    params: param,
    paramsSerializer: customParamsSerializer,
    isLoading: loading
  })
}

export function getparameteritem(number) {

  return request({
    url: '/api/admin/parameter/show',
    method: 'get',
    params: {
      number: number
    }
  })
}

export function listparameter(param) {
  return request({
    url: '/api/admin/parameter/index',
    method: 'get',
    params: param,
    paramsSerializer: customParamsSerializer,
  })
}
export function del(id) {
  return request({
    url: '/api/admin/parameter/delete',
    method: 'get',
    params: {
      id
    }
  })
}
export function delDetail(id) {
  return request({
    url: '/api/admin/parameter/detail-delete',
    method: 'get',
    params: {
      id
    }
  })
}
