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

async function materialList(category,company,division,token){
  const response = await axios.get(process.env.backend_url+`/api/materials?sort[0]=id:desc&filters[Category][$eq]=${category}${(company=='All')?'':`&filters[company][id][$eq]=${company}`}${(division=='All')?'':`&filters[division][id][$eq]=${division}`}&populate=*`,{
    headers: {'Authorization': `Bearer ${token}`}
  }).catch((e)=>{
    console.log(e.response.body);
  });
    return (response)?response.data.data.sort(function(a,b){
      return new Date(b.attributes.Presented) - new Date(a.attributes.Presented);
    }):[];
}

async function material(id,token){
  const response = await axios.get(process.env.backend_url+`/api/materials/${id}?populate=*`,{
    headers: {'Authorization': `Bearer ${token}`}
  }).catch((e)=>{
    console.log(e.response.body);
  });
    return (response)?response.data.data.attributes:null;
}

module.exports = {materialList,material};