require('./resource');
require('./model');
import { BaseViewModel } from '../master/baseViewModel.js';
import { CompanyInfoDataViewModel } from '../company/model';

export function CompanyInfoViewModel() {
    var self = this;
    self.putFormData = function () {
        var selectedRecord = self.getSelectedRecord();
        var selectedId = selectedRecord.id();

        var model = selectedRecord.toServer();
        if (!model)
            return;
        ApiService.request(
            {
                url: "/api/companyinfo/putvalue",
                data: model,
                success: function (data) {
                    if (!_h.isNullOrEmpty(data)) {
                        self.getRecords(selectedId);
                        toast.success(self.entityName, 'put');
                    }
                },
                error: function (data) {
                    toast.error(self.entityName, 'put');
                }
            });
    }
    self.afterFilterBy = function (x, selectedId) {
        var rec;
        if (selectedId) {
            if (_h.any(self.records()))
                for (var i = 0; i < self.records().length; i++) {
                    rec = self.records()[i];
                    if (rec.id() === selectedId) {
                        self.changeSelectedRecord(rec, false, false, false);
                    }
                }
        } else {
            if (_h.any(self.records())) {
                rec = self.records()[0];
                self.changeSelectedRecord(rec, false, false, false);
            }
        }
    }
    self.init = function () {
        self.fillTableProperties();
        self.getRecords();
    }
    ko.utils.extend(self, new BaseViewModel({
        entityName: "CompanyInfo",
        getList: true,
        apiService: {
            actionList: ["filterby"]
        },
        buttons: [
            { name: 'help', access: false, cMenu: false },
            { name: 'refresh', access: false, cMenu: false },
        ],
        filters: [],
        sortDirection: "Ascending",
        sortProperty: "id",
        context: self,
        saveState: true,
        cols: companyinfoModelRes,
        selectRow: true,
        contextMenu: true,
        searchKeyType: "enter"
    }));
    self.btnVisibility = ko.computed(function () {
        self.vishelp(true);
        self.visrefresh(true);
    });
    self.changeChildModel = function (item) {
        var cols = [];
        var idObj = {
            fa: '#',
            colName: 'id',
            tablecol: false,
            visibleCol: false,
            sortDir: "None",
            alias: null,
            type: "template",
            templateName: "checkbox-table-template",
            dataType: -1
        };

        cols.push(idObj);
        if (!_h.isNullOrEmpty(item.fields()))
            for (var i = 0; i < item.fields().length; i++) {
                var field = item.fields()[i];
                var order = field.order.toString();
                var obj = {
                    fa: field.name,
                    colName: order,
                    tablecol: true,
                    visibleCol: true,
                    sortDir: "None",
                    alias: null,
                    dataType: field.dataType,
                    required: field.required
                };
                switch (field.dataType) {
                    case 1:
                        obj.type = "string";
                        break;
                    case 2:
                        obj.type = "string";
                        break;
                    case 3:
                        obj.type = "pdate";
                        break;
                    case 4:
                        obj.type = "file";
                        break;
                }

                if (i === 0)
                    obj.sortDir = "Ascending";

                cols.push(obj);
            }

        var model = new CompanyInfoDataViewModel(cols, true);
        model.formId(item.id());
        model.formName(item.title());
        self.childModel(model);
        self.childModel().init();
    }
}