import ngModule from '../module';
import Section from 'salix/components/section';
import UserError from 'core/lib/user-error';

export default class Controller extends Section {
    onSynchronizeAll() {
        this.vnApp.showSuccess(this.$t('Synchronizing in the background'));
        this.$http.patch(`UserAccounts/syncAll`)
            .then(() => this.vnApp.showSuccess(this.$t('Users synchronized!')));
    }

    onUserSync() {
        if (!this.syncUser)
            throw new UserError('Please enter the username');

        let params = {
            password: this.syncPassword,
            force: true
        };
        return this.$http.patch(`UserAccounts/${this.syncUser}/sync`, params)
            .then(() => this.vnApp.showSuccess(this.$t('User synchronized!')));
    }

    onSynchronizeRoles() {
        this.$http.patch(`RoleInherits/sync`)
            .then(() => this.vnApp.showSuccess(this.$t('Roles synchronized!')));
    }

    onSyncClose() {
        this.syncUser = '';
        this.syncPassword = '';
    }
}

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