import { BaseModel, BaseFormModel, BaseInfoModel } from '../master/baseModels.js';
require('knockout.validation');
import { BaseViewModel } from '../master/baseViewModel.js';
import { TreeMapSelect } from '../master/model.js';
export class Balancesheet extends BaseModel {
    constructor(model) {
        super(model);
    }
}
export class BalancesheetContent extends BaseInfoModel {
    constructor(model) {
        super(model);
    }
}
export class BalancesheetForm extends BaseFormModel {
    constructor(viewModel, editMode) {
        super(viewModel, editMode);
        this.fileName = ko.observable();
        this.file = ko.observable();
        this.fileType = ko.observable();
        if (!editMode) {
            this.balancesheetType(2);
            this.currencyType(1);
        }
        this.setValidationObject();
        this.createForm();
    }
    setValidationObject() {
        var self = this;
        this.validationObject = ko.validatedObservable({
            name: self.name.extend({
                required: true,
                validCharacterName: true,
                minLength: 3,
                maxLength: 40,
                remoteIsUniqueRule: { params: [self.id, "Balancesheet", "name", "remotePlaceHolder"] }
            }),
            balancesheetType: self.balancesheetType.extend({
                required: true
            }),
            currencyType: self.currencyType.extend({
                required: true
            }),
            file: self.file.extend({
                checkExcel: { params: [self.fileName] }
            })
        });
    }
    handleFileChoose(files) {
        var file = files;
        var ext = _h.getExtension(file.name);
        _h.toBase64(file, function (res) {
            this.file(res);
        }.bind(this));
        switch (ext) {
            case "xls":
                this.fileType(8);
                break;
            case "xlsx":
                this.fileType(9);
                break;
            case "csv":
                this.fileType(0);
                break;
            default:
        }
        this.fileName(file.name);
    }
    createForm() {
        const controls = [
            this.createLine({ t: 'txb', val: this.name }, null),
            this.createLine({ t: 'slc', val: this.balancesheetType, options: balancesheetTypes },
                { t: 'slc', val: this.currencyType, options: currencyTypes, dis: this.editMode }, true, 30),
            this.createLine({ t: 'tm', templateName: 'balancesheet-upload-form-t' }, undefined, true, 30),
            this.createLine({ t: 'txa', val: this.description }, undefined, true, 30)
        ];
        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() === true, click: this.parentVM.edit },
                    { name: 'cancel', click: _h.kocnx().closeForm }]
            }
        });
    }
}
export function BalancesheetContentViewModel() {
    var self = this;
    self.treeSelectList = ko.observableArray();
    self.getTreeSelectList = function () {
        var model = {
            controllerName: 13,
            mappingType: 0
        };
        self.apiService.request({
            name: "getMappingTreeList",
            data: model,
            success: function (data) {
                self.treeSelectList.removeAll();
                if (_h.any(data))
                    for (var j = 0; j < data.length; j++) {
                        self.treeSelectList.push(new TreeMapSelect(data[j]));
                    }
                self.treeId(null);
            },
            error: function () {
                toast.error("Tree", 'getSelectList');
            }
        });
    }
    self.init = function () {
        self.fillTableProperties();
        self.getRecords();
        self.getTreeSelectList();
    };
    self.exportExcelBtn = function () {
        var filterModel = self.createFilterByModel();
        var model = {
            controllerName: 13,
            inputData: filterModel
        };
        _h.kocnx().createExcel(model);
    }
    ko.utils.extend(self,
        new BaseViewModel({
            entityName: "BalancesheetContent",
            apiService: {
                actionList: ["filterby"]
            },
            buttons: [
                { name: 'exportExcel', access: true, icon: 'ExportExcel', cMenu: false },
                { name: 'help', access: false, cMenu: false },
                { name: 'refresh', access: false, cMenu: false },
                { name: 'tools', access: false, cMenu: false }
            ],
            filters: [
                { name: 'treeId', type: "select", source: self.treeSelectList },
                { name: 'sheetId', clear: false }
            ],
            extraParams: ["totalCreditor", "totalDebtor"],
            sortDirection: "Ascending",
            sortProperty: "code",
            context: self,
            cols: balancesheetcontentModelRes,
            saveState: true,
            getList: true,
            contextMenu: false,
            selectRow: false,
            searchKeyType: "enter",
            tableClass: "table ko-tbl ko-tbl-child",
            tableStyle: "font-size: 11px;",
            resizable: true,
            footerTemplate: "balancesheetcontent-footertable-template",
            filterbyColumn: true
        }));
    self.btnVisibility = ko.computed(function () {
        self.visexportExcel(true);
        self.vishelp(true);
        self.visrefresh(true);
        self.vistools(true);
    });
}