/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

let BootstrapVue = require('bootstrap-vue');
Vue.use(BootstrapVue);

let VueCoolSelect = require("vue-cool-select");
Vue.use(VueCoolSelect, {
    theme: "bootstrap",
});

import {Datetime}  from 'vue-datetime';
Vue.component('datetime',Datetime);


/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

// const files = require.context('./', true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))

//Vue.component('example-component', require('./components/ExampleComponent.vue').default);

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

(function () {
    let handlers = require('lib/handlers');
    let entropy = require('lib/entropy');

    var MinimumEntropy = 50;
    if(null != document.getElementById("basic"))
    {
        console.info("Found element 'basic' - initializing vue");
        var basic = new Vue({
            el: '#basic'
        });
    }
    if(null != document.getElementById("main"))
    {
        console.info("Found element 'main' - initializing vue");
        var app = new Vue({
            el: '#main',
            data: {
            domains: [],
            tlspolicies: [],
            newtls: {
                domain: '',
                policy: 'none',
                params: ''
            },
            accounts: [],
            siteaccounts: [],
            newaccount: {
                username: '',
                enabled: true,
                quota: 0,
                pass1: '',
                pass2: '',
                sendonly: false,
                validators: {
                username: null,
                pass1: null,
                pass2: null
                }
            },
            newsiteaccount: {
                username: '',
                enabled: true,
                quota: 0,
                pass1: '',
                pass2: '',
                sendonly: false,
                validators: {
                username: null,
                pass1: null,
                pass2: null
                }
            },
            newalias: {
                source: '',
                enabled: true,
                destination: '',
                comment: '',
                validators: {
                source: null,
                destination: null
                }
            },
            aliases: [],
            users: [],
            admins: [],
            newadmin: {
                user: null,
                role: 'view'
            },
            active_domain: null,
            creating_domain: false,
            create_domain_name: ''
            },
            created: function created() {
            this.refresh();
            },
            computed: {
            user: function user() {
                return window.InitialState.User;
            },
            isSiteAdmin: function isSiteAdmin() {
                return window.InitialState.IsSiteAdmin;
            },
            availableUsers: function availableUsers() {
                return this.users.filter(function (usr) {
                var _iteratorNormalCompletion = true;
                var _didIteratorError = false;
                var _iteratorError = undefined;

                try {
                    for (var _iterator = app.admins[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                    var u = _step.value;

                    if (u.id == usr.id) {
                        return false;
                    }
                    }
                } catch (err) {
                    _didIteratorError = true;
                    _iteratorError = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion && _iterator["return"] != null) {
                        _iterator["return"]();
                    }
                    } finally {
                    if (_didIteratorError) {
                        throw _iteratorError;
                    }
                    }
                }

                return true;
                });
            },
            domainRole: function domainRole() {
                var _iteratorNormalCompletion2 = true;
                var _didIteratorError2 = false;
                var _iteratorError2 = undefined;

                try {
                for (var _iterator2 = app.admins[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
                    var u = _step2.value;

                    if (u.id == app.user.id) {
                    return u.pivot.role;
                    }
                }
                } catch (err) {
                _didIteratorError2 = true;
                _iteratorError2 = err;
                } finally {
                try {
                    if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
                    _iterator2["return"]();
                    }
                } finally {
                    if (_didIteratorError2) {
                    throw _iteratorError2;
                    }
                }
                }

                return null;
            }
            },
            methods: {
            /*****************
             * 
             * Operational
             * 
             * ***************/
            refresh: function refresh() {
                return axios.post("/domains", {
                action: 'list'
                }).then(function (response) {
                var domains = response.data;
                var _iteratorNormalCompletion3 = true;
                var _didIteratorError3 = false;
                var _iteratorError3 = undefined;

                try {
                    for (var _iterator3 = domains[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
                    var d = _step3.value;
                    app.domain_tidy(d);
                    }
                } catch (err) {
                    _didIteratorError3 = true;
                    _iteratorError3 = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion3 && _iterator3["return"] != null) {
                        _iterator3["return"]();
                    }
                    } finally {
                    if (_didIteratorError3) {
                        throw _iteratorError3;
                    }
                    }
                }

                app.domains = domains;
                app.siteaccounts_list();
                });
            },
            select_domain: function select_domain(domain) {
                app.active_domain = domain;
                app.creating_domain = false;
                app.accounts_list();
                app.aliases_list();
                app.admins_list();
            },
            select_create_domain: function select_create_domain() {
                app.creating_domain = true;
                app.active_domain = null;
            },

            /*****************
             * 
             * Domain
             * 
             * ***************/
            domain_tidy: function domain_tidy(d) // tidy_domaininfo(d)
            {
                d.newname = '';
                d.validators = {
                name: null
                };
                d.changed = {
                name: false
                };
            },
            domain_exists: function domain_exists(dname) {
                //console.info('check exists',d);
                var _iteratorNormalCompletion4 = true;
                var _didIteratorError4 = false;
                var _iteratorError4 = undefined;

                try {
                for (var _iterator4 = app.domains[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
                    var d = _step4.value;
                    console.info;

                    if (dname == d.domain) {
                    return true;
                    }
                }
                } catch (err) {
                _didIteratorError4 = true;
                _iteratorError4 = err;
                } finally {
                try {
                    if (!_iteratorNormalCompletion4 && _iterator4["return"] != null) {
                    _iterator4["return"]();
                    }
                } finally {
                    if (_didIteratorError4) {
                    throw _iteratorError4;
                    }
                }
                }

                return false;
            },
            create_domain: function create_domain() {
                axios.post("/domains", {
                action: 'create',
                domain: app.create_domain_name
                }).then(handlers.buttonsuccess(app.$refs.createDomainButton)).then(function () {
                app.refresh().then(function () {
                    var _iteratorNormalCompletion5 = true;
                    var _didIteratorError5 = false;
                    var _iteratorError5 = undefined;

                    try {
                    for (var _iterator5 = app.domains[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
                        var d = _step5.value;
                        console.info;

                        if (app.create_domain_name == d.domain) {
                        app.select_domain(d);
                        }
                    }
                    } catch (err) {
                    _didIteratorError5 = true;
                    _iteratorError5 = err;
                    } finally {
                    try {
                        if (!_iteratorNormalCompletion5 && _iterator5["return"] != null) {
                        _iterator5["return"]();
                        }
                    } finally {
                        if (_didIteratorError5) {
                        throw _iteratorError5;
                        }
                    }
                    }

                    app.create_domain_name = '';
                });
                })["catch"](handlers.buttonerror(app.$refs.createDomainButton));
            },
            domain_validate_newname: function domain_validate_newname() {
                app.active_domain.validators.name = !app.domain_exists(app.active_domain.newname);
            },
            domain_rename: function domain_rename(event) {
                axios.post("/domains", {
                action: 'rename',
                domain: app.active_domain.domain,
                newname: app.active_domain.newname
                }).then(function () {
                app.active_domain.domain = app.active_domain.newname;
                app.domain_tidy(app.active_domain);
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            domain_delete: function domain_delete(event) {
                axios.post("/domains", {
                action: 'drop',
                domain: app.active_domain.domain
                }).then(handlers.buttonsuccess(event.target)).then(function () {
                active_domain = null;
                app.refresh();
                })["catch"](handlers.buttonerror(event.target));
            },

            /*****************
             * 
             * TLS
             * 
             * ***************/
            tls_tidy: function tls_tidy(p) {
                p.changed = {
                policy: false,
                params: false
                };
            },
            tls_list: function tls_list() {
                axios.post("/tls", {
                action: 'list'
                }).then(function (response) {
                console.info(response);
                var tlsparams = response.data;
                var _iteratorNormalCompletion6 = true;
                var _didIteratorError6 = false;
                var _iteratorError6 = undefined;

                try {
                    for (var _iterator6 = tlsparams[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
                    var p = _step6.value;
                    app.tls_tidy(p);
                    }
                } catch (err) {
                    _didIteratorError6 = true;
                    _iteratorError6 = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion6 && _iterator6["return"] != null) {
                        _iterator6["return"]();
                    }
                    } finally {
                    if (_didIteratorError6) {
                        throw _iteratorError6;
                    }
                    }
                }

                app.tlspolicies = tlsparams;
                });
            },
            tls_add: function tls_add(event) {
                axios.post("/tls", {
                action: 'setpolicy',
                domain: app.newtls.domain,
                policy: app.newtls.policy,
                params: app.newtls.params
                }).then(function (response) {
                var tls = response.data;
                app.tls_tidy(tls);
                app.tlspolicies.push(tls);
                app.newtls.domain = '';
                app.newtls.policy = '';
                app.newtls.params = '';
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            tls_set: function tls_set(p, event) {
                axios.post("/tls", {
                action: 'setpolicy',
                domain: p.domain,
                policy: p.policy,
                params: p.params
                }).then(function (response) {}).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            tls_del: function tls_del(p, event) {
                axios.post("/tls", {
                action: 'delpolicy',
                domain: p.domain
                }).then(function (response) {
                app.tls_list();
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },

            /*****************
             * 
             * Accounts
             * 
             * ***************/
            account_tidy: function account_tidy(a) {
                // tidy_accountinfo(a)
                a.changeusername = false;
                a.validators = {
                username: null,
                pass1: null,
                pass2: null
                };
                a.pass1 = '';
                a.pass2 = '';
                a.enabled = !!a.enabled;
                a.sendonly = !!a.sendonly;
                a.newusername = a.username;
            },
            accounts_list: function accounts_list() {
                axios.post("/accounts", {
                action: 'list',
                domain: app.active_domain.domain
                }).then(function (response) {
                var accounts = response.data;
                var _iteratorNormalCompletion7 = true;
                var _didIteratorError7 = false;
                var _iteratorError7 = undefined;

                try {
                    for (var _iterator7 = accounts[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
                    var a = _step7.value;
                    app.account_tidy(a);
                    }
                } catch (err) {
                    _didIteratorError7 = true;
                    _iteratorError7 = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion7 && _iterator7["return"] != null) {
                        _iterator7["return"]();
                    }
                    } finally {
                    if (_didIteratorError7) {
                        throw _iteratorError7;
                    }
                    }
                }

                app.accounts = accounts;
                console.info(app.accounts);
                });
            },
            account_check_username: function account_check_username(a) {
                // check_username(a) {
                //event.preventDefault();
                if (a.username == a.newusername) {
                a.validators.username = true;
                } else {
                axios.post("/accounts", {
                    action: 'checkusername',
                    domain: app.active_domain.domain,
                    id: a.id,
                    username: a.newusername
                }).then(function (response) {
                    if (!response.data) {
                    a.validators.username = false;
                    } else {
                    a.validators.username = true;
                    }
                });
                }
            },
            account_check_newusername: function account_check_newusername() {
                // check_newusername() {
                //event.preventDefault();
                axios.post("/accounts", {
                action: 'checkusername',
                domain: app.active_domain.domain,
                username: app.newaccount.username
                }).then(function (response) {
                if (!response.data) {
                    app.newaccount.validators.username = false;
                } else {
                    app.newaccount.validators.username = true;
                }
                });
            },
            account_check_password: function account_check_password(a) {
                //check_password(a) {
                if (a.pass1.length == 0 && a.pass2.length == 0) {
                a.validators.pass1 = null;
                a.validators.pass2 = null;
                } else if (entropy.calculate(a.pass1) < MinimumEntropy) {
                a.validators.pass1 = false;

                if (a.pass2.length <= 0) {
                    a.validators.pass2 = null;
                } else {
                    a.validators.pass2 = false;
                }
                } else if (a.pass1 !== a.pass2) {
                a.validators.pass1 = true;
                a.validators.pass2 = false;
                } else {
                a.validators.pass1 = true;
                a.validators.pass2 = true;
                }
            },
            account_check_newpassword: function account_check_newpassword() {
                //check_newpassword() {
                if (entropy.calculate(app.newaccount.pass1) < MinimumEntropy) {
                app.newaccount.validators.pass1 = false;

                if (app.newaccount.pass2.length <= 0) {
                    app.newaccount.validators.pass2 = null;
                } else {
                    app.newaccount.validators.pass2 = false;
                }
                } else if (app.newaccount.pass1 !== app.newaccount.pass2) {
                app.newaccount.validators.pass1 = true;
                app.newaccount.validators.pass2 = false;
                } else {
                app.newaccount.validators.pass1 = true;
                app.newaccount.validators.pass2 = true;
                }
            },
            account_create: function account_create(event) {
                //create_account(event){
                axios.post("/accounts", {
                action: 'create',
                domain: app.active_domain.domain,
                username: app.newaccount.username,
                quota: app.newaccount.quota,
                enabled: app.newaccount.enabled,
                sendonly: app.newaccount.sendonly,
                password: app.newaccount.pass1
                }).then(function (response) {
                app.newaccount = {
                    username: '',
                    enabled: true,
                    quota: 0,
                    pass1: '',
                    pass2: '',
                    sendonly: false,
                    validators: {
                    username: null,
                    pass1: null,
                    pass2: null
                    }
                };
                var a = response.data[0];
                app.account_tidy(a);
                app.accounts.push(a);
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            account_update: function account_update(a, event) {
                //update_account(a,event){
                axios.post("/accounts", {
                action: 'update',
                id: a.id,
                domain: app.active_domain.domain,
                username: a.newusername,
                quota: a.quota,
                enabled: a.enabled,
                sendonly: a.sendonly,
                password: a.pass1
                }).then(function (response) {
                a.username = response.data[0].username;
                a.validators.username = null;
                a.validators.pass1 = null;
                a.validators.pass2 = null;
                a.changeusername = false;
                a.pass1 = '';
                a.pass2 = '';
                app.$forceUpdate();
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            account_delete: function account_delete(a, event) {
                //delete_account(a,event){
                axios.post("/accounts", {
                action: 'delete',
                id: a.id,
                domain: app.active_domain.domain
                }).then(function (response) {
                app.accounts_list();
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },

            /*****************
             * 
             * Siteaccounts
             * 
             * ***************/
            siteaccounts_list: function siteaccounts_list() {
                axios.post("/siteaccounts", {
                action: 'list'
                }).then(function (response) {
                var accounts = response.data;
                var _iteratorNormalCompletion8 = true;
                var _didIteratorError8 = false;
                var _iteratorError8 = undefined;

                try {
                    for (var _iterator8 = accounts[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
                    var a = _step8.value;
                    app.account_tidy(a);
                    }
                } catch (err) {
                    _didIteratorError8 = true;
                    _iteratorError8 = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion8 && _iterator8["return"] != null) {
                        _iterator8["return"]();
                    }
                    } finally {
                    if (_didIteratorError8) {
                        throw _iteratorError8;
                    }
                    }
                }

                app.siteaccounts = accounts;
                console.info(app.accounts);
                });
            },
            siteaccount_check_username: function siteaccount_check_username(a) {
                // check_username(a) {
                //event.preventDefault();
                if (a.username == a.newusername) {
                a.validators.username = true;
                } else {
                axios.post("/siteaccounts", {
                    action: 'checkusername',
                    id: a.id,
                    username: a.newusername
                }).then(function (response) {
                    if (!response.data) {
                    a.validators.username = false;
                    } else {
                    a.validators.username = true;
                    }
                });
                }
            },
            siteaccount_check_newusername: function siteaccount_check_newusername() {
                // check_newusername() {
                //event.preventDefault();
                axios.post("/siteaccounts", {
                action: 'checkusername',
                username: app.newsiteaccount.username
                }).then(function (response) {
                if (!response.data) {
                    app.newsiteaccount.validators.username = false;
                } else {
                    app.newsiteaccount.validators.username = true;
                }
                });
            },
            siteaccount_check_password: function siteaccount_check_password(a) {
                //check_password(a) {
                if (a.pass1.length == 0 && a.pass2.length == 0) {
                a.validators.pass1 = null;
                a.validators.pass2 = null;
                } else if (entropy.calculate(a.pass1) < MinimumEntropy) {
                a.validators.pass1 = false;

                if (a.pass2.length <= 0) {
                    a.validators.pass2 = null;
                } else {
                    a.validators.pass2 = false;
                }
                } else if (a.pass1 !== a.pass2) {
                a.validators.pass1 = true;
                a.validators.pass2 = false;
                } else {
                a.validators.pass1 = true;
                a.validators.pass2 = true;
                }
            },
            siteaccount_check_newpassword: function siteaccount_check_newpassword() {
                //check_newpassword() {
                if (entropy.calculate(app.newsiteaccount.pass1) < MinimumEntropy) {
                app.newsiteaccount.validators.pass1 = false;

                if (app.newsiteaccount.pass2.length <= 0) {
                    app.newsiteaccount.validators.pass2 = null;
                } else {
                    app.newsiteaccount.validators.pass2 = false;
                }
                } else if (app.newsiteaccount.pass1 !== app.newsiteaccount.pass2) {
                app.newsiteaccount.validators.pass1 = true;
                app.newsiteaccount.validators.pass2 = false;
                } else {
                app.newsiteaccount.validators.pass1 = true;
                app.newsiteaccount.validators.pass2 = true;
                }
            },
            siteaccount_create: function siteaccount_create(event) {
                //create_account(event){
                axios.post("/siteaccounts", {
                action: 'create',
                username: app.newsiteaccount.username,
                quota: app.newsiteaccount.quota,
                enabled: app.newsiteaccount.enabled,
                sendonly: app.newsiteaccount.sendonly,
                password: app.newsiteaccount.pass1
                }).then(function (response) {
                app.newsiteaccount = {
                    username: '',
                    enabled: true,
                    quota: 0,
                    pass1: '',
                    pass2: '',
                    sendonly: false,
                    validators: {
                    username: null,
                    pass1: null,
                    pass2: null
                    }
                };
                var a = response.data[0];
                app.account_tidy(a);
                app.siteaccounts.push(a);
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            siteaccount_update: function siteaccount_update(a, event) {
                //update_account(a,event){
                axios.post("/siteaccounts", {
                action: 'update',
                id: a.id,
                username: a.newusername,
                quota: a.quota,
                enabled: a.enabled,
                sendonly: a.sendonly,
                password: a.pass1
                }).then(function (response) {
                a.username = response.data[0].username;
                a.validators.username = null;
                a.validators.pass1 = null;
                a.validators.pass2 = null;
                a.changeusername = false;
                a.pass1 = '';
                a.pass2 = '';
                app.$forceUpdate();
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            siteaccount_delete: function siteaccount_delete(a, event) {
                //delete_account(a,event){
                axios.post("/siteaccounts", {
                action: 'delete',
                id: a.id,
                domain: app.active_domain.domain
                }).then(function (response) {
                app.siteaccounts_list();
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },

            /*****************
             * 
             * Alias
             * 
             * ***************/
            alias_tidy: function alias_tidy(a) {
                //tidy_alias(a)
                var _iteratorNormalCompletion9 = true;
                var _didIteratorError9 = false;
                var _iteratorError9 = undefined;

                try {
                for (var _iterator9 = a.dest[Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) {
                    var d = _step9.value;
                    d.enabled = !!d.enabled;
                    d.changed = {
                    dest: false,
                    comment: false,
                    enabled: false
                    };
                }
                } catch (err) {
                _didIteratorError9 = true;
                _iteratorError9 = err;
                } finally {
                try {
                    if (!_iteratorNormalCompletion9 && _iterator9["return"] != null) {
                    _iterator9["return"]();
                    }
                } finally {
                    if (_didIteratorError9) {
                    throw _iteratorError9;
                    }
                }
                }

                a.newdest = {
                destination: '',
                comment: '',
                enabled: true
                };
            },
            aliases_list: function aliases_list() {
                axios.post("/aliases", {
                action: 'list',
                domain: app.active_domain.domain
                }).then(function (response) {
                console.info(response.data);
                var aliases = response.data;
                var _iteratorNormalCompletion10 = true;
                var _didIteratorError10 = false;
                var _iteratorError10 = undefined;

                try {
                    for (var _iterator10 = aliases[Symbol.iterator](), _step10; !(_iteratorNormalCompletion10 = (_step10 = _iterator10.next()).done); _iteratorNormalCompletion10 = true) {
                    var a = _step10.value;
                    app.alias_tidy(a);
                    }
                } catch (err) {
                    _didIteratorError10 = true;
                    _iteratorError10 = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion10 && _iterator10["return"] != null) {
                        _iterator10["return"]();
                    }
                    } finally {
                    if (_didIteratorError10) {
                        throw _iteratorError10;
                    }
                    }
                }

                app.aliases = aliases;
                });
            },
            alias_dest_changed: function alias_dest_changed(d, field) {
                d.changed[field] = true;
            },
            alias_update_dest: function alias_update_dest(a, d, event) {
                axios.post("/aliases", {
                action: 'update_dest',
                domain: app.active_domain.domain,
                id: d.id,
                destination: d.destination,
                enabled: d.enabled,
                comment: d.comment
                }).then(function (response) {
                d.changed.dest = false;
                d.changed.enabled = false;
                d.changed.enabled = false;
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            alias_create: function alias_create(event) {
                axios.post("/aliases", {
                action: 'add_dest',
                domain: app.active_domain.domain,
                source: app.newalias.source,
                destination: app.newalias.destination,
                enabled: app.newalias.enabled,
                comment: app.newalias.comment
                }).then(function (response) {
                var aliases = response.data;
                var _iteratorNormalCompletion11 = true;
                var _didIteratorError11 = false;
                var _iteratorError11 = undefined;

                try {
                    for (var _iterator11 = aliases[Symbol.iterator](), _step11; !(_iteratorNormalCompletion11 = (_step11 = _iterator11.next()).done); _iteratorNormalCompletion11 = true) {
                    var al = _step11.value;
                    app.alias_tidy(al);
                    app.aliases.push(al);
                    }
                } catch (err) {
                    _didIteratorError11 = true;
                    _iteratorError11 = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion11 && _iterator11["return"] != null) {
                        _iterator11["return"]();
                    }
                    } finally {
                    if (_didIteratorError11) {
                        throw _iteratorError11;
                    }
                    }
                }

                app.newalias.destination = '';
                app.newalias.enabled = true;
                app.newalias.comment = '';
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            alias_add_dest: function alias_add_dest(a, event) {
                axios.post("/aliases", {
                action: 'add_dest',
                domain: app.active_domain.domain,
                source: a.source,
                destination: a.newdest.destination,
                enabled: a.newdest.enabled,
                comment: a.newdest.comment
                }).then(function (response) {
                var aliases = response.data;
                var _iteratorNormalCompletion12 = true;
                var _didIteratorError12 = false;
                var _iteratorError12 = undefined;

                try {
                    for (var _iterator12 = aliases[Symbol.iterator](), _step12; !(_iteratorNormalCompletion12 = (_step12 = _iterator12.next()).done); _iteratorNormalCompletion12 = true) {
                    var al = _step12.value;
                    app.alias_tidy(al);
                    var _iteratorNormalCompletion13 = true;
                    var _didIteratorError13 = false;
                    var _iteratorError13 = undefined;

                    try {
                        for (var _iterator13 = al.dest[Symbol.iterator](), _step13; !(_iteratorNormalCompletion13 = (_step13 = _iterator13.next()).done); _iteratorNormalCompletion13 = true) {
                        var d = _step13.value;
                        a.dest.push(d);
                        }
                    } catch (err) {
                        _didIteratorError13 = true;
                        _iteratorError13 = err;
                    } finally {
                        try {
                        if (!_iteratorNormalCompletion13 && _iterator13["return"] != null) {
                            _iterator13["return"]();
                        }
                        } finally {
                        if (_didIteratorError13) {
                            throw _iteratorError13;
                        }
                        }
                    }
                    }
                } catch (err) {
                    _didIteratorError12 = true;
                    _iteratorError12 = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion12 && _iterator12["return"] != null) {
                        _iterator12["return"]();
                    }
                    } finally {
                    if (_didIteratorError12) {
                        throw _iteratorError12;
                    }
                    }
                }

                a.newdest.destination = '';
                a.newdest.enabled = true;
                a.newdest.comment = '';
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            alias_del_dest: function alias_del_dest(a, d, event) {
                axios.post("/aliases", {
                action: 'delete_dest',
                domain: app.active_domain.domain,
                id: d.id
                }).then(function (response) {
                if (a.dest.length > 1) {
                    var idx = null;

                    for (var i in a.dest) {
                    if (a.dest[i].id == d.id) {
                        idx = i;
                        break;
                    }
                    }

                    if (idx !== null) {
                    a.dest.splice(idx, 1);
                    }
                } else {
                    app.aliases_list(); // refresh aliases list
                }
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            alias_check_newsource: function alias_check_newsource() {
                //check_newsource()
                axios.post("/aliases", {
                action: 'checksource',
                domain: app.active_domain.domain,
                source: app.newalias.source
                }).then(function (response) {
                if (!response.data) {
                    app.newalias.validators.source = false;
                } else {
                    app.newalias.validators.source = true;
                }
                });
            },

            /*****************
             * 
             * Domain Administrators
             * 
             * ***************/
            admin_tidy: function admin_tidy(a) {
                // tidy_admininfo(a)
                a.changed = {
                role: false
                };
            },
            admins_list: function admins_list() {
                axios.post("/domains", {
                action: 'listadmins',
                domain: app.active_domain.domain
                }).then(function (response) {
                var admins = response.data;
                var _iteratorNormalCompletion14 = true;
                var _didIteratorError14 = false;
                var _iteratorError14 = undefined;

                try {
                    for (var _iterator14 = admins[Symbol.iterator](), _step14; !(_iteratorNormalCompletion14 = (_step14 = _iterator14.next()).done); _iteratorNormalCompletion14 = true) {
                    var a = _step14.value;
                    app.admin_tidy(a);
                    }
                } catch (err) {
                    _didIteratorError14 = true;
                    _iteratorError14 = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion14 && _iterator14["return"] != null) {
                        _iterator14["return"]();
                    }
                    } finally {
                    if (_didIteratorError14) {
                        throw _iteratorError14;
                    }
                    }
                }

                app.admins = admins;
                });
                axios.post("/domains", {
                action: 'listusers',
                domain: app.active_domain.domain
                }).then(function (response) {
                app.users = response.data;
                });
            },
            admin_add: function admin_add(event) {
                axios.post("/domains", {
                action: 'setadmin',
                domain: app.active_domain.domain,
                userid: app.newadmin.user,
                role: app.newadmin.role
                }).then(function (response) {
                var usr = {};
                var _iteratorNormalCompletion15 = true;
                var _didIteratorError15 = false;
                var _iteratorError15 = undefined;

                try {
                    for (var _iterator15 = app.users[Symbol.iterator](), _step15; !(_iteratorNormalCompletion15 = (_step15 = _iterator15.next()).done); _iteratorNormalCompletion15 = true) {
                    var u = _step15.value;

                    if (u.id == app.newadmin.user) {
                        usr = u;
                    }
                    }
                } catch (err) {
                    _didIteratorError15 = true;
                    _iteratorError15 = err;
                } finally {
                    try {
                    if (!_iteratorNormalCompletion15 && _iterator15["return"] != null) {
                        _iterator15["return"]();
                    }
                    } finally {
                    if (_didIteratorError15) {
                        throw _iteratorError15;
                    }
                    }
                }

                usr.pivot = {
                    role: app.newadmin.role
                };
                app.admin_tidy(usr);
                app.admins.push(usr);
                app.newadmin.user = null;
                app.newadmin.role = 'view';
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            admin_update: function admin_update(a, event) {
                axios.post("/domains", {
                action: 'setadmin',
                domain: app.active_domain.domain,
                userid: a.id,
                role: a.pivot.role
                }).then(function (response) {
                a.changed.role = false;
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            },
            admin_del: function admin_del(a, event) {
                axios.post("/domains", {
                action: 'deladmin',
                domain: app.active_domain.domain,
                userid: a.id
                }).then(function (response) {
                var idx = null;

                for (var i in app.admins) {
                    if (app.admins[i].id == a.id) {
                    idx = i;
                    break;
                    }
                }

                if (idx !== null) {
                    app.admins.splice(idx, 1);
                }
                }).then(handlers.buttonsuccess(event.target))["catch"](handlers.buttonerror(event.target));
            }
            }
        });
    }
    if(null != document.getElementById("chpass"))
    {
        console.info("Found element 'chpass' - initializing vue");
        var chpass = new Vue({
            el: '#chpass',
            data: {
            username: "",
            pass: "",
            newpass1: "",
            newpass2: "",
            validation: {
                username: null,
                pass: null,
                newpass1: null,
                newpass2: null
            },
            processing: false,
            resultstatus: null,
            resultmsg: ""
            },
            methods: {
            input_validate: function input_validate() {
                var self = this;
                self.validation.username = self.username.length > 0 ? true : null;
                self.validation.pass = self.pass.length > 0 ? true : null; // validate password

                if (entropy.calculate(self.newpass1) < MinimumEntropy) {
                self.validation.newpass1 = self.newpass1.length <= 0 ? null : false;
                ;
                self.validation.newpass2 = self.newpass2.length <= 0 ? null : false;
                } else if (self.newpass1 !== self.newpass2) {
                self.validation.newpass1 = true;
                self.validation.newpass2 = false;
                } else {
                self.validation.newpass1 = true;
                self.validation.newpass2 = true;
                }
            },
            flash: function flash(msg, status, duration) {
                var self = this;
                self.resultmsg = msg;
                self.resultstatus = status;
                self.$refs.tooltip.$emit('open');
                setTimeout(function () {
                self.resultstatus = null;
                self.resultmsg = "";
                self.$refs.tooltip.$emit('close');
                }, duration);
            },
            submit: function submit(event) {
                var self = this;
                axios.post("/accountpassword", {
                username: self.username,
                pass: self.pass,
                newpass: self.newpass1
                }).then(function (response) {
                self.processing = false;
                self.success = true;
                self.username = "";
                self.pass = "";
                self.newpass1 = "";
                self.newpass2 = "";
                self.validation = {
                    username: null,
                    pass: null,
                    newpass1: null,
                    newpass2: null
                };
                self.flash(response.data.msg, true, 5000);
                })["catch"](function (error) {
                self.processing = false;
                self.flash(error.response.data.errors[0], false, 5000);
                });
                self.processing = true;
            }
            }
        });
    }
})();
