import { BaseViewModel } from '../master/baseViewModel.js';
import { SelectItem } from '../master/model';
import { GroupPeriodForm } from './model';
require('./model');
require('./resource');
export function GroupPeriodViewModel() {
    var self = this;
    self.companySelectList = ko.observableArray();
    self.getCompanySelectList = function () {
        self.apiService.request({
            name: "/api/common/getcompanylist",
            data: { controllerName: 37 },
            success: function (data) {
                self.companySelectList.removeAll();
                if (_h.any(data)) {
                    for (var j = 0; j < data.length; j++) {
                        var target = data[j];
                        self.companySelectList.push(new SelectItem(target));
                    }
                    self.companyId(null);
                }
            },
            error: function (message, level) {
                toast.error("Company", 'getSelectList', "GroupCompanies");
            }
        });
    }
    self.getPeriodDetail = function (callback) {
        self.apiService.request({
            name: "getPeriodDetail",
            data: { id: self.getSelectedRecord().id() },
            success: function (data) {
                callback(data);
            },
            error: function (message, level) {
                toast.error(self.entityName, 'get', "PeriodDetail");
                callback(null);
            }
        });
    }
    self.reviewFinancialBtn = function () {
        self.getPeriodDetail(function (data) {
            if (data !== null) {
                _h.kocnx()
                    .openForm(new GroupPeriodForm(self, true, data));
            }
        });
    }
    self.submittedBtn = function (parents, btnModel, e) {
        self.changeSubmitState(parents[1], 1, e);
    };
    self.afterDescSubmittedBtn = function (parents, btnModel, e) {
        self.changeSubmitState(parents[1], 3, e);
    };
    self.confirmedBtn = function (parents, btnModel, e) {
        self.changeSubmitState(parents[1], 2, e);
    };
    self.rejectedBtn = function () {
        _h.kocnx().formModel().showDescription(true);
    };
    self.changeSubmitState = function (item, action, e) {
        if (!item.validationObject.isValid()) {
            item.validationObject.errors.showAllMessages();
            return false;
        }
        var model = {
            id: item.id(),
            submitState: action,
            description: item.description()
        };
        self.apiService.request({
            splashIid: e,
            name: "putSubmit",
            data: model,
            success: function (data) {
                if (!_h.isNullOrEmpty(data)) {
                    _h.kocnx().closeForm();
                    self.afterUpdate(data.models);
                    toast.success(self.entityName, 'putUpdate', "SubmitState");
                }
            },
            error: function (message, level) {
                toast.error(self.entityName, 'putUpdate', "SubmitState");
            }
        });
    };
    self.init = function () {
        self.fillTableProperties();
        self.getRecords();
        self.getCompanySelectList();
    };
    ko.utils.extend(self, new BaseViewModel({
        entityName: "GroupPeriod",
        getList: true,
        apiService: {
            actionList: ["filterby", "getcompanies", "getperioddetail", "putsubmit"]
        },
        extraParams: [],
        buttons: [
            { name: 'reviewFinancial', access: true, icon: 'StatementOption', cMenu: true },
            { name: 'help', access: false, cMenu: false },
            { name: 'refresh', access: false, cMenu: false },
            { name: 'tools', access: false, cMenu: false },
        ],
        filters: [
            { name: 'companyId', type: "select", source: self.companySelectList },
            { name: 'submitState', type: "select", source: submitStates }
        ],
        sortDirection: "Ascending",
        sortProperty: "id",
        context: self,
        cols: groupperiodModelRes,
        searchKeyType: "enter",
        saveState: true,

        selectRow: true,
        contextMenu: true,
        resizable: true
    }));
    self.btnVisibility = ko.computed(function () {
        var isSelected = self.isSelectedRecord();
        var permit = _h.kocnx().permitPeriodEdit();
        self.visreviewFinancial(isSelected && permit);
        self.vishelp(true);
        self.visrefresh(true);
        self.vistools(true);
    });
}
