import axios from "axios"


axios.interceptors.request.use(
    config => {
        config.headers = {
            Accept: "application/json",
            "Access-Control-Allow-Origin": "*",
            // "Access-Control-Expose-Headers": "Access-Control-Allow-Origin",
            // "Access-Control-Allow-Headers": "Origin, X-API-KEY, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Headers, Authorization, observe, enctype, Content-Length, X-Csrf-Token",
            // "Content-Type": "application/json;charset=UTF-8",
            'Authorization': (localStorage && localStorage.getItem('token')) ? `Bearer ${localStorage.getItem('token')}` : ''
          };
    // config.headers['Authorization'] = `Bearer ${localStorage.getItem('token')}`;
    // config.baseURL = process.env.REACT_APP_MAIN_API
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

const postAuxEnd = (path, postData, media=false) => {
    const basePath = media ? process.env.REACT_APP_MAIN_API : process.env.REACT_APP_MAIN_API
    return axios.post(`${basePath}/office${path}`, postData).then(res => {
        return res
    }).catch(err => {
        throw new Error(err);
    })
}

const getAuxEnd = (path, reqData= null) => {
    const basePath = process.env.REACT_APP_MAIN_API
    return axios.get(`${basePath}/office${path}`,{ params: reqData }).then(res => {
        return res
        // localStorage.clear();
        // window.location.href = `/login?sessionExpired=true`;
    }).catch(err => {
        throw new Error(err);
        // throw new Error(err.response.data.message);
        // return err
    })
}

// FUNCTION TO LOGIN USER IN
export const loginUser = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/login', postData, false)
}

// FUNCTION TO GET DASHBOARD DATA
export const getDashData = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/dashboard`, postData)
}

// FUNCTION TO GET CUSTOMERS
export const getCustomers = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/customers`, postData)
}

// FUNCTION TO GET BILLINGS
export const getBillings = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/billings`, postData)
}

// FUNCTION TO GET SUBSCRIPTIONS
export const getSubscriptions = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/subcriptions`, postData)
}

// FUNCTION TO GET RIGHT SIDEBAR DATA
export const getRightSidebar = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/right-sidebar`, postData)
}

// FUNCTION TO GET RECENT SIGNUP DATA
export const getRecentSignup = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/recent-signup`, postData)
}

// FUNCTION TO GET PRODUCTS DATA
export const getProducts = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/products`, postData)
}

// FUNCTION TO GET USERS DATA
export const getUsers = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/users`, postData)
}

// FUNCTION TO GET PRODUCTS TEMPLATE DATA
export const getProductsTemplate = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/products-templates`, postData)
}

// FUNCTION TO UPDATE TEMPLATE
export const updateTemplate = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/template/set-template', postData, false)
}

// FUNCTION TO ADD CUSTOM TEMPLATE
export const addCustomTemplate = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/template/custom-add', postData, false)
}

// FUNCTION TO GET CUSTOM TEMPLATE DATA
export const getCustomTemplate = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/custom-templates`, postData)
}

// FUNCTION TO VIEW SELECTED ACCOUNT DATA
export const getAccountView = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/account-view`, postData)
}

// FUNCTION TO GET SUBSCRIPTIONS VIEW DATA
export const getSubscriptionsView = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/subscription-view`, postData)
}


// FUNCTION TO GET LOANS TABLE
export const getLoans = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/loans`, postData)
}

// FUNCTION TO GET REPAYMENTS TABLE
export const getRepayments = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/repayments`, postData)
}

// FUNCTION TO GET OFFERS LIST TABLE
export const getOffers = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/offers`, postData)
}

// FUNCTION TO GET REPAYMENT SCHEDULE TABLE
export const getRepaymentSchedule = (reqData) => {
    const postData = { ...reqData }
    return getAuxEnd(`/repayment-schedules`, postData)
}