import ngModule from '../../module';
import Component from 'core/lib/component';

class Controller extends Component {
    open($event) {
        if ($event.defaultPrevented) return;

        this.$.provinceDialog.show();
        $event.preventDefault();
    }

    onAccept() {
        try {
            if (!this.province.name)
                throw new Error(`The province name can't be empty`);
            if (!this.province.autonomyFk)
                throw new Error(`The autonomy can't be empty`);

            this.$http.patch(`provinces`, this.province).then(res => {
                this.vnApp.showMessage(this.$t('The province has been created'));
                this.emit('response', {$response: res.data});
            });
        } catch (e) {
            this.vnApp.showError(this.$t(e.message));
            return false;
        }
        return true;
    }
}

ngModule.vnComponent('vnGeoProvince', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        data: '<',
    }
});