require('./model');
require('./resource');
import {  BalancesheetForm, BalancesheetContentViewModel } from './model.js';
import { BaseViewModel } from '../master/baseViewModel.js';

export function BalancesheetViewModel() {
    var self = this;
    self.createBtn = function () {
        _h.kocnx()
            .openForm(new BalancesheetForm(self, false));
    }
    self.editBtn = function () {
        _h.kocnx()
            .openForm(new BalancesheetForm(self, true));
    }
    self.changeChildModel = function (target) {
        self.childModel(new BalancesheetContentViewModel());
        self.childModel().sheetId(target.id());
        self.childModel().init();
    }
    self.connectManBtn = function () {
        _h.kocnx().openConnectMan(13, "connectManCaption");
    };
    self.init = function () {
        self.fillTableProperties();
        self.getRecords();
    }
    ko.utils.extend(self, new BaseViewModel({
        entityName: "Balancesheet",
        apiService: {
            actionList: ["filterby",
                "post", "put", "delete"]
        },
        buttons: [
            { name: 'edit', access: true, icon: 'Edit', cMenu: true },
            { name: 'delete', access: true, icon: 'Delete', cMenu: true },
            { name: 'connectMan', access: true, icon: 'DatabaseLink', 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 }
        ],
        filters: [
            { name: 'balancesheetType', type: "select", source: balancesheetTypes },
            { name: 'currencyType', type: "select", source: currencyTypes }
        ],
        sortDirection: "Ascending",
        sortProperty: "id",
        context: self,
        cols: balancesheetModelRes,
        saveState: true,
        getList: true,
        contextMenu: true,
        selectRow: true,
        searchKeyType: "enter",
        tableClass: "table ko-tbl ko-tbl-master",
        tableStyle: "table-layout: fixed;",
        resizable: true
    }));
    self.btnVisibility = ko.computed(function () {
        var isSelected = self.isSelectedRecord();
        var inListView = self.showType() === "List";
        var permitPeriodEdit = _h.kocnx().permitPeriodEdit();
        if (isSelected) {
            self.visedit(permitPeriodEdit && !inListView);
            self.visdelete(permitPeriodEdit && !inListView);
        } else {
            self.visedit(false);
            self.visdelete(false);
        }
        self.vishelp(!inListView);
        self.visrefresh(!inListView);
        self.vistools(true);
        self.visconnectMan(!inListView);
        self.viscreate(permitPeriodEdit && !inListView);
    });
}