var { BaseModel, BaseFormModel } = require('../master/baseModels.js');

require('knockout.validation');

export class Access extends BaseModel {
    constructor(model) {
        super(model);
    }
}
export class AccessForm extends BaseFormModel {
    constructor(viewModel, editMode, permissionList) {
        super(viewModel, editMode);
        this.permissions = ko.observableArray();
        this.setValidationObject();
        this.createForm();
        this.selector = "#tree_view_pr";
        this.configTools(permissionList);
    }
    configTools(list) {
        var self = this;
        setTimeout(function () {
            $(this.selector).treeView({
                tType: "Permission",
                location: "Access",
                nodes: list,
                rootId: "1",
                context: self
            });
        }.bind(this), 1);
        setTimeout(function () {
            var items = $(this.selector).treeView("selectedCheckedItems");
            this.refreshPermissions(items);
        }.bind(this), 300);
    }
    onClickItem() {
        var items = $(this.selector).treeView("selectedCheckedItems");
        this.refreshPermissions(items);
    }
    checkedBoxes() {
        $(this.selector).treeView("checkedBoxes");
        var items = $(this.selector).treeView("selectedCheckedItems");
        this.refreshPermissions(items);
    }
    uncheckedBoxes() {
        $(this.selector).treeView("uncheckedBoxes");
        var items = $(this.selector).treeView("selectedCheckedItems");
        this.refreshPermissions([]);
    }
    refreshPermissions(items) {
        this.permissions.removeAll();
        if (_h.any(items))
            for (var i = 0; i < items.length; i++) {
                this.permissions.push({ accessId: this.id(), permissionType: items[i] });
            }
    }
    setValidationObject() {
        this.validationObject = ko.validatedObservable({
            name: this.name.extend({
                required: true,
                validCharacterName: true,
                minLength: 3,
                maxLength: 100,
                remoteIsUniqueRule: {
                    params: [this.id, "Access", "name"]
                }
            }),
            accessType: this.accessType.extend({
                required: true
            }),
            description: this.description.extend({
                maxLength: 1000
            }),
            permissions: this.permissions.extend({
                atLeastOne: true
            })
        });
    }
    expandAll() {
        $(this.selector).treeView("expandAll");
    }
    collapseAll() {
        $(this.selector).treeView("collapseAll");
    }
    createForm() {
        const controls = [
            this.createLine({ t: 'txb', val: this.name },
                { t: 'slc', val: this.accessType, options: accessTypes }),
            this.createLine({ t: 'txa', val: this.description }, undefined, true, 30),
            this.createLine({ t: 'tm', templateName: 'access-permissions-form-t' }, undefined, true, 30, 0),

        ];
        this.formControls(controls);
        this.formButtons({
            name: 'footer-form-t', data: {
                buttons: [
                    { name: 'create', vis: !this.editMode(), click: this.parentVM.create },
                    { name: 'edit', vis: this.editMode, click: this.parentVM.edit },
                    { name: 'cancel', click: _h.kocnx().closeForm }]
            }
        });
    }
} 