/**
 * Created by Administrator on 10/14/2016.
 */
import App from '../plugins/common';

export default {
    data: function () {
        return {
            upcomingEvents: [],
            eventCategories: [],
        }
    },
    methods: {
        updateDatetimeByPeriod(events) {
            for (var i in events) {
                /* The unit of Event period if per day, e.g 2 means 2 day, 7 means one week(weekly). */
                if (events[i].hasOwnProperty('period')) {
                    let startTime = new Date(events[i].start_datetime).getTime();
                    let endTime = new Date(events[i].end_datetime).getTime();

                    if (Date.now() > startTime) {
                        let diff = Math.floor((Date.now() - startTime) / (events[i].period * 24 * 60 * 60 * 1000)) * (events[i].period * 24 * 60 * 60 * 1000);
                        let newStartTime = startTime + diff;
                        let newEndTime = endTime + diff;
                        events[i].start_datetime = new Date(newStartTime).toString();
                        events[i].end_datetime = new Date(newEndTime).toString();
                    }
                    events[i].recurringTitle = 'Recurring every' + events[i].period + 'days.';
                } else {
                    events[i].recurringTitle = '';
                }
            }
        },
        addDetailHrefPropertyToEvent(events) {
            if (Array.isArray(events)) {
                for (var i in events) {
                    events[i].url = App.ctx + '/event/' + events[i].id;
                }
            } else {
                events.url = App.ctx + '/event/' + events.id;
            }
        },
        findComingEvents: function () {
            this.$http.get(App.ctx + '/event/coming').then((response) => {
                this.upcomingEvents = response.body;
            }, (response) => {

            });
        },
        findEventCategories() {
            this.$http.get(App.ctx + '/events/categories').then((response) => {
                this.eventCategories = response.body;
            }, (response) => {

            });
        },
    }
}