import ngModule from '../module';

class Controller {
    constructor($scope, $http, $stateParams) {
        this.$stateParams = $stateParams;
        this.$scope = $scope;
        this.$http = $http;
        this.searchValue = '';
        this.filter = {};
    }

    onSearch() {
        this.$scope.$applyAsync(() => {
            this.$scope.treeview.refresh();
        });
    }

    exprBuilder(param, value) {
        switch (param) {
        case 'search':
            return {name: {like: `%${value}%`}};
        }
    }

    onSelection(item, isIncluded) {
        let node = Object.assign({}, item);
        node.isIncluded = isIncluded;
        node.childs = []; // Data too large

        const path = '/agency/api/ZoneIncludeds/toggleIsIncluded';
        const params = {zoneFk: this.zone.id, item: node};
        this.$http.post(path, params);
    }
}

Controller.$inject = ['$scope', '$http', '$stateParams'];

ngModule.component('vnZoneLocation', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        zone: '<'
    },
    require: {
        card: '^vnZoneCard'
    }
});