import ngModule from '../module';
import SearchPanel from 'core/components/searchbar/search-panel';

class Controller extends SearchPanel {
    constructor($, $element) {
        super($, $element);

        this.filter = this.$.filter;

        if (!this.dateParams)
            this.getUpcomingCampaing();
    }

    get dateParams() {
        if (this.$params.q) {
            const params = JSON.parse(this.$params.q);

            if (params.from || params.to)
                return true;
        }

        return false;
    }

    getUpcomingCampaing() {
        this.$http.get('Campaigns/upcoming').then(res => {
            this.filter.campaign = res.data.id;
        });
    }

    onChangeDate(value) {
        if (value)
            this.filter.campaign = null;
    }

    get campaignSelection() {
        return this._campaignSelection;
    }

    set campaignSelection(value) {
        this._campaignSelection = value;

        if (value) {
            const from = new Date(value.dated);
            from.setDate(from.getDate() - value.scopeDays);

            this.filter.to = value.dated;
            this.filter.from = from;
        }
    }
}

ngModule.vnComponent('vnConsumptionSearchPanel', {
    template: require('./index.html'),
    controller: Controller
});