const axios = require('axios').default;

async function getUsers(token,page,company=null,division=null){
  const response = await axios.get(process.env.backend_url+`/api/users?pagination[page]=${page}&pagination[pageSize]=9${company&&company!='All'?'&filters[company][id][$eq]='+company.toString():''}${division&&division!='All'?'&filters[division][id][$eq]='+division.toString():''}`,{
    headers: {'Authorization': `Bearer ${token}`}
  }).catch((e)=>{
    console.log(e.response.body);
  });
    return (response)?response.data:[];
}

async function setEditor(token,id,editor){
  const response = await axios.put(process.env.backend_url+`/api/users/${id}`,{
    role:editor==1?4:3
  },{
    headers: {'Authorization': `Bearer ${token}`},
  }).catch((e)=>{
    console.log('Error update role',e.response.data.error);
  });
    return (response)?true:false;
}

async function setState(token,id,state){
  const response = await axios.put(process.env.backend_url+`/api/users/${id}`,{
    blocked:state
  },{
    headers: {'Authorization': `Bearer ${token}`},
  }).catch((e)=>{
    console.log(token);
    console.log('Error update role',e.response.data.error);
  });
  return (response)?true:false;
}

async function deleteUser(token,id){
  const response = await axios.delete(process.env.backend_url+`/api/users/${id}`,{
    headers: {'Authorization': `Bearer ${token}`},
  }).catch((e)=>{
    console.log('Error update role',e.response.data.error);
  });
    return (response)?true:false;
}

module.exports = {getUsers,setEditor,deleteUser,setState};