/* Minification failed. Returning unminified contents.
(109,112-113): run-time error JS1003: Expected ':': ,
(109,120-121): run-time error JS1003: Expected ':': }
(192,12-13): run-time error JS1003: Expected ':': (
(192,15-16): run-time error JS1100: Expected ',': {
(194,5-6): run-time error JS1002: Syntax error: }
(198,31-32): run-time error JS1010: Expected identifier: (
(203,115-116): run-time error JS1195: Expected expression: >
(211,14-15): run-time error JS1195: Expected expression: )
(212,9-10): run-time error JS1002: Syntax error: }
(214,28-29): run-time error JS1195: Expected expression: )
(214,30-31): run-time error JS1004: Expected ';': {
(217,10-11): run-time error JS1195: Expected expression: ,
(218,47-48): run-time error JS1004: Expected ';': {
(220,10-11): run-time error JS1195: Expected expression: ,
(221,52-53): run-time error JS1004: Expected ';': {
(224,5-6): run-time error JS1002: Syntax error: }
(227,5-6): run-time error JS1002: Syntax error: }
(222,13-51): run-time error JS1018: 'return' statement outside of function: return GetProviderColour(providerName)
(219,13-46): run-time error JS1018: 'return' statement outside of function: return GetCourseTypeIcon(subType)
 */
// Create axios class with default vars TEST AGAIn
var $http = axios.create({
    baseURL: baseUrl + '/api/',
    timeout: 500000,
    headers:{}
});

var $api = {
    // GET: /api/blog/{postID}/comments
    getComments: function(postId) {
        return $http.get("/blog/" + postId + "/comments");
    },
    // POST: /api/blog/{postId}/comments
    createComment: function(postId, data) {
        return $http.post("/blog/" + postId + "/comments", data);
    },
    // PATCH: /api/blog/{postId}/comments/{commentId}
    editComment: function(postId, commentId, data) {
        return $http.patch("/blog/" + postId + "/comments/" + commentId, data );
    },
    // DELETE: /api/blog/{postId}/comments/{commentId}
    deleteComment: function(postId, commentId) {
        return $http.delete("/blog/" + postId + "/comments/" + commentId);
    },
    // GET: /api/courses/catalog
    getCourseCatalog: function(searchTerm, courseTypes, partnerIds, dateOrder, selectedInterests, professions, startDate) {
        
        var url = "/courses/catalog?searchTerm=";

        if (searchTerm)
            url += searchTerm;

        if (courseTypes)
            url += "&courseTypes=" + courseTypes;

        if (partnerIds)
            url += "&partnerIds=" + partnerIds;

        if (dateOrder)
            url += "&dateOrder=" + dateOrder;

        if (selectedInterests)
            url += "&selectedInterests=" + selectedInterests;

        if (professions)
            url += "&professions=" + professions;

        if (startDate)
            url += "&startDate=" + startDate;

        return $http.get(url);
    },
    // GET: /api/users/{id}/login-token
    getUserLoginToken: function(id, identifier) {

        // Set custom auth header for this request
        return $http.get("/authorize/users/" + id + "/login-token?identifier=" + identifier);
    },
    // GET: /api/lms/cohorts/{id}/users
    getLmsCohortUsers: function (cohortId) {

        return $http.get("/lms/cohorts/" + cohortId.toString() + "/users");
    },
    // GET /api/lms/courses/categories
    getLmsCourseCategories: function () {

        return $http.get("/lms/courses/categories");
    },
    // POST: /api/lms/learning-groups
    createLmsGroup: function (data) {
        return $http.post("/lms/learning-groups", data);
    },
    // GET /api/authorize/user/token
    getUserApiToken: function () {
        return $http.get(apiUrl + "/authorize/users/token");
    },
    // GET /api/account/verify-email/{email}
    verifySignupEmail: function (email, checkEmailExists) {

        var url = "/account/verify-email/" + email;

        // Defaults to true, so if false add query param
        if (checkEmailExists === false) {
            url = url + "?checkEmailExists=false";
        }

        return $http.get(url);
    },
    // POST /api/accounts/verification/send
    sendVerificationEmail: function (email) {

        return $http.post("/account/verification/send", { email: email });
    },
    // POST /api/accounts/verification/complete
    completeEmailVerification: function (email, code) {

        return $http.post("/account/verification/complete", { email: email, code: code });
    },
    // POST /api/accounts/login/email
    loginWithEmail: function (email) {
        return $http.post("/account/login/email", { email: email });
    },
    // GET /api/accounts/management/subscriptions
    getUserSubscriptions: function (email) {
        return $http.get("/account/management/subscriptions");
    },
    // POST /api/accounts/management/subscriptions/{id}/update-payment-method
    updateSubscriptionPaymentMethod: function (id, token) {
        return $http.post("/account/management/subscriptions/" + id.toString() + "/update-payment-method", { id, token });
    }
};

// Add a request interceptor to add Tokens
$http.interceptors.request.use(
    function (config) {

        // Try set auth token
        var token = null;
        try {
            token = authToken;
        }
        catch (err) {
            console.log(err.message);
        }

        // TODO: storage auth
        if (token) {
            config.headers['Authorization'] = 'Bearer ' + token;
        }
        
        // Add in antiforgery token
        var antiforgeryToken = $('input[name="__RequestVerificationToken"]').val();
        if (antiforgeryToken)
            config.headers['XSRF-TOKEN'] = antiforgeryToken;

        return config;
    },
    function (error) {
        console.log("Axios interceptor error.");
    }
);;
// Date filter
// eg 
Vue.filter('formatDate', function (value, format) {
    if (value) {
        return moment(String(value)).format(format);
    }
});

// Truncate text
// eg {{data.content | truncate(300, '...')}}
Vue.filter('truncate', function (text, stop, clamp) {
    return text.slice(0, stop) + (stop < text.length ? clamp || '...' : '');
});;
// Storage helper
var $storage = {
    // Retrieve item from local storage
    getData: function (key) {
        try {
            var data = localStorage.getItem(key);
            var jsonData = JSON.parse(data);

            return jsonData;
        }
        catch(err){
            console.log(err.message);
            return null;
        }
    },
    // Set object in local storage
    setData: function (key, data) {
        localStorage.setItem(key, JSON.stringify(data));
    },
    // Remove item from local storage
    removeData: function (key) {
        localStorage.removeItem(key);
    }
};;
// Allied Health products app
var app = new Vue({
    // The app element
    el: '#allied-health-app',
    // App variables
    data: {
        loaded: false,
        loading: true,
        searchTerm: null,
        professions: "Dental Practitioner, Medical Radiation Practitioner, Midwife, Occupational Therapist, Optometrist, Pharmacist, Physiotherapist, Podiatrist, Psychologist, Practice Manager, Student, Other, Aboriginal and Torres Strait Islander Health Practitioner, Social Worker, Support Worker, Dietitian, Ambulance Officer, Chinese Medicine Practitioner, Chiropractor, Osteopath, Accredited Mental Health Social Worker, Clinical Psychologist, Counsellor, Educator, Exercise Physiologist, Manager, Mental Health Worker, Psychometrician, Researcher, Aged Care Staff, Paramedic",
        products: []
    },
    // Init function
    mounted() {
        this.getProducts();
    },
    // App methods
    methods: {  
        // Get products (courses) function
        getProducts: function () {

            this.loading = true;
            this.products = [];

            $api.getCourseCatalog(this.searchTerm, null, null, null, null, this.professions, null).then(response => {

                this.products = response.data;
                this.loading = false;
                this.loaded = true;

                // Load dynamic defered images
                loadDeferedImages();
            });
        },
        // Scroll to top of the app
        goToTop: function () {
            var ele = $("#allied-health-app");
            scrollToElement(ele, 10);
        },
        getCourseTypeIcon: function (subType) {
            return GetCourseTypeIcon(subType);
        },
        getProviderColour: function (providerName) {
            return GetProviderColour(providerName);
        }
    },
    // Computed variables
    computed: {  
    }
});;
