require('./model');
require('./resource');
var { BaseViewModel } = require('../master/baseViewModel');
import { SelectItem } from '../master/model';
import { ExtraInformationSection } from './model';
export function ActionLogViewModel() {
    var self = this;
    self.userSelectList = ko.observableArray();
    self.checkAll = ko.observable();
    self.getUserSelectList = function () {
        self.apiService.request({
            name: "/api/common/getuserslist",
            data: {
                controllerName: self.controllerName
            },
            success: function (data) {
                self.userSelectList.removeAll();
                self.users.removeAll();
                if (_h.any(data)) {
                    for (var j = 0; j < data.length; j++) {
                        var target = new SelectItem(data[j]);
                        self.userSelectList.push(target);
                    }
                }
            },
            messageType: 'getSelectList',
            messageKey: "User"
        });
    }
    self.toggleRight = function () {
        if (self.wideRight()) {
            if (self.isSelectedRecord()) {
                const selectedRecord = self.getSelectedRecord();
                self.wideRight(false);
                const model = new ExtraInformationSection(selectedRecord.extraInformation());
                self.childModel(model);
                self.iconSize("icon-image-small");
            }
        } else {
            self.wideRight(true);
            self.childModel(null);
            self.iconSize("icon-image");
        }
    };
    self.checkAll.subscribe(function (newValue) {
        if (newValue !== undefined && newValue !== null) {
            ko.utils.arrayFilter(self.records(),
                function (record) {
                    record.selected(newValue);
                });
        }
    });
    self.hasSelectedRecord = ko.pureComputed(function () {
        var count = 0;
        ko.utils.arrayFilter(self.records(),
            function (record) {
                if (record.selected())
                    count++;
            });
        if (count > 0)
            return true;
        return false;
    });
    self.countSelectedRecord = ko.pureComputed(function () {
        var count = 0;
        ko.utils.arrayFilter(self.records(),
            function (record) {
                if (record.selected())
                    count++;
            });
        return count;
    });
    self.toggleDetails = function (item, e, y) {
        item.detailsShow(!item.detailsShow());
    }
    self.changeChildModel = function (target, force) {
        const selectedRecord = self.getSelectedRecord();
        const model = new ExtraInformationSection(selectedRecord.extraInformation());
        self.childModel(model);
        self.iconSize("icon-image-small");
    }
    self.exportExcelBtn = function () {
        var filterModel = self.createFilterByModel();
        var model = {
            controllerName: 9,
            inputData: filterModel
        };
        _h.kocnx().createExcel(model);
    }
    self.init = function () {
        self.fillTableProperties();
        var [start, end] = dateHelper.getActionLogDate();
        self.startDate(start);
        self.endDate(end);
        self.getRecords();
        self.getUserSelectList();
    }
    ko.utils.extend(self, new BaseViewModel({
        entityName: "ActionLog",
        getList: true,
        apiService: {
            actionList: ["filterby", "delete"]
        },
        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: 'startDate', type: "datepiker" },
            { name: 'endDate', type: "datepiker" },
            { name: 'controllerNames', type: "multiselect", source: controllerNames.filter(a => !a.hidden), groupBy: "group" },
            { name: 'actionNames', type: "multiselect", source: actionNames.filter(a => !a.hidden), groupBy: "group" },
            { name: 'users', type: "multiselect", source: self.userSelectList },
            { name: 'recent', type: "bool" },
            { name: 'isMarked' },
            { name: 'timeCreated' }
        ],
        sortDirection: "Descending",
        sortProperty: "date",
        context: self,
        cols: actionlogModelRes,
        selectRow: true,
        saveState: true,
        contextMenu: true,
        isMultiSelectRows: true,
        searchKeyType: "enter",
        resizable: true
    }));
    self.btnVisibility = ko.computed(function () {
        self.visexportExcel(true);
        self.vishelp(true);
        self.visrefresh(true);
        self.vistools(true);
    });
    self.refreshTable = function () {
        self.childModel(null);
        config.context.getRecords();
    };
    self.filterBy = function () {
        self.pageNumber(1);
        var [start, end] = dateHelper.getActionLogDate(self.startDate(), self.endDate());
        if (self.startDate() !== start)
            self.startDate(start);
        if (self.endDate() !== end)
            self.endDate(end);
        self.getRecords();
    }
    self.clearSearchBoxes = function () {
        var [start, end] = dateHelper.getActionLogDate();
        self.startDate(start);
        self.endDate(end);
        self.controllerNames.removeAll();
        self.actionNames.removeAll();
        self.users.removeAll();
        self.recent(undefined);
        self.isMarked(undefined);
        self.timeCreated(undefined);
    }
}
