var { BaseViewModel } = require('../master/baseViewModel.js');
import { AccessForm } from './model';
require('./resource');

export function AccessViewModel() {
    var self = this;
    self.permissionList = [];
    self.getPermissionList = function (editMode, callback) {
        var record = self.getSelectedRecord();
        var model = {
            id: 0
        }
        if (editMode)
            model.id = record.id();
        self.apiService.request({
            name: "getSelectList",
            data: model,
            success: function (data) {
                callback(data);
            },
            messageType: 'getSelectList',
            messageKey: "PermissionList"
        });
    }
    self.createBtn = function () {
        self.getPermissionList(false, function (res) {
            _h.kocnx()
                .openForm(new AccessForm(self, false, res));
        });

    }
    self.editBtn = function () {
        self.getPermissionList(true, function (res) {
            _h.kocnx()
                .openForm(new AccessForm(self, true, res));
        });
    }
    self.init = function () {
        self.getRecords();
        self.fillTableProperties();
    }
    ko.utils.extend(self, new BaseViewModel({
        entityName: "Access",
        getList: true,
        apiService: {
            actionList: ["filterby",
                "post", "put", "getselectlist", "delete"]
        },
        filters: [
            { name: 'accessType', type: "select", source: accessTypes }
        ],
        buttons: [
            { name: 'edit', access: true, icon: 'Edit', cMenu: true },
            { name: 'delete', access: true, icon: 'Delete', cMenu: true },
            { name: 'help', access: false, cMenu: false },
            { name: 'refresh', access: false, cMenu: false },
            { name: 'tools', access: false, cMenu: false },
            { name: 'create', access: false, cMenu: false }
        ],
        sortDirection: "Ascending",
        sortProperty: "id",
        context: self,
        cols: accessModelRes,
        saveState: true,
        searchKeyType: "enter",
        selectRow: true,
        contextMenu: true
    }));
    self.btnVisibility = ko.computed(function () {
        var isSelected = self.isSelectedRecord();
        self.viscreate(true);
        self.visedit(isSelected);
        self.visdelete(isSelected);
        self.vishelp(true);
        self.visrefresh(true);
        self.vistools(true);
    });
}