import { get, post } from "../http";

const moduleName = 'devicesUser'

const functionUris = {
  userList: "/getDevicesUserPages",
  addUser: "/addDevicesUser",
  updateUser: "/updateDevicesUser",
  getUser: "/getDevicesUser",
  deleteUser: "/delDevicesUser",
}

export default {
  userTopup(params) {
    return post(moduleName + "/manualTopUp", params)
  },
  pageChargeHistory(params) {
    return post(moduleName + "/viewingChargingHistory", params)
  },
  pageTopupHistory(params) {
    return post(moduleName + "/viewingTopUpHistory", params)
  }
}

export function fetchList(
  query = {
    limit: 10,
    page: 1,
    criteria: '',
  }
) {
  const { limit = 10, page = 1, criteria = '' } = query;
  return post(`${moduleName}${functionUris.userList}`, {
    pageVo: { criteria },
    pageSize: limit,
    pageNo: page,
  }).then((response) => {
    const { data, ...other } = response;
    const copyData = [];
    data.forEach(user => {
      const { userPk, ...other } = user;
      copyData.push({ userId: userPk, ...other });
    });
    return { ...other, data: copyData }
  })
}

export function addUser(params) {
  return post(`${moduleName}${functionUris.addUser}`, params)
}

export function updateUser(data) {
  return post(`${moduleName}${functionUris.updateUser}`, data)
}

export function getUser(userId) {
  return get(
    `${moduleName}${functionUris.getUser}`,
    {userPk: userId},
  )
}

export function deleteUser(userId) {
  return get(`${moduleName}${functionUris.deleteUser}`, {
    userPk: userId,
  })
}

export function deleteUserVehicle(vehiclePk) {
  return get(`${moduleName}/delUserVehicle`, { vehiclePk })
}