(function (paw, utils, inventoryFixedAssetsIdentService, fn) {
    function InventoryFixedAssetsIdents(context, settings, localization, formatting, editMode) {
        var self = this;
        this.context = context;
        this.settings = paw.lang.append({
            $identSearch: undefined,
            $identTable: undefined,
            $identRows: undefined,
            $noteModal: undefined,
            $noteInput: undefined
        }, settings);
        this.localization = paw.lang.append({
            notePopupTitle: '@Loc.Note',
            save: '@Loc.Save',
            cancel: '@Loc.Cancel'
        }, localization || {});
        this.formatting = formatting;
        this.editMode = editMode;

        this.selectedIdent = null;
        this.idents = [];
        this.noteModal = new paw.com.noteEditor.NoteEditorPopup({}, {
            popupTitle: this.localization.notePopupTitle,
            save: this.localization.save,
            cancel: this.localization.cancel
        });

        this.onRemoveIdent = function () {
            var $elt = $(this);
            var anNo = utils.value.strToNum($elt.attr('data-remove-ident-position'));
            var ident = utils.iterable.get(self.idents, function (ident) {
                return ident.AnNo === anNo;
            });
            self.removeIdentPosition(ident);
        };

        this.onOpenNoteModal = function (e) {
            var anNo = utils.value.strToNum($(this).attr('data-note-ident-position'));
            var ident = paw.utils.iterable.get(self.idents, function (item) {
                return item.AnNo === anNo;
            });
            self.noteModal.show(ident.Note, {
                onSave: function (content) {
                    ident.Note = content;
                    $("#noteModal-" + anNo)
                        .removeClass("btn-link btn-active")
                        .addClass(content != "" ? "btn-link btn-active" : "btn-link");
                }
            });
        };
    }

    InventoryFixedAssetsIdents.prototype.init = function () {
        this.initIdentSearch();

        if (this.editMode) {
            this.fetchIdents();
        }          
    };

    InventoryFixedAssetsIdents.prototype.destroy = function () {
        self.settings.$identRows.find('button[data-note-ident-position]').on('off', this.onOpenNoteModal);
        this.settings.$identRows.find('[data-remove-ident-position]').on('off', this.onRemoveIdent);
        this.noteModal.destroy();
        
    };

    InventoryFixedAssetsIdents.prototype.initIdentSearch = function () {
        fn.initializeInvoiceIdentInputs(
            this.settings.$identSearch,
            null,
            this.settings.discountType, 
            this.formatting.decimalPlacesPrice,
            $("#ReceiverId"),
            null,
            null,
            this.context.localization,
            null,
            null);
    };

    InventoryFixedAssetsIdents.prototype.fetchIdents = function () {
        var self = this;
        return inventoryFixedAssetsIdentService.fetchIdents().then(function (idents) {
            self.idents = idents;
            self.renderTable();
            self.settings.$identRows.find('button[data-note-ident-position]').on('click', self.onOpenNoteModal);
            self.settings.$identRows.find('[data-remove-ident-position]').on('click', self.onRemoveIdent);
        });
    };

    InventoryFixedAssetsIdents.prototype.renderTable = function () {
        var self = this;
        utils.iterable.iterate(this.idents, function (ident) {
            self.settings.$identRows.append();
            self.renderRow(ident)
        });
        
    };

    InventoryFixedAssetsIdents.prototype.renderRow = function (ident) {
        let row = this.renderColumns(ident);
        fn.initializeNumberInputs();
        fn.reorderColumns(true, row);
        this.settings.$identRows.append(row);
    };

    InventoryFixedAssetsIdents.prototype.renderColumns = function (ident) {
        var row = fn.createTableRowElement("tr", "position-" + ident.AnNo, "", null, false, "row");
        var itemActiveClass = (ident.IsActive === false ? " text-danger" : "");
        row.appendChild(fn.createTableRowElement("input", "anNo-" + ident.AnNo, "form-control", ident.AnNo, true, null, "text", null, null, true));
        row.appendChild(fn.createTableRowElement("input", "name-" + ident.AnNo, "form-control" + itemActiveClass, ident.Name, false, null, "text", null, () => updateIdentBasicData(ident.AnNo), true));
        row.appendChild(fn.createTableRowElement("input", "department-" + ident.AnNo, "form-control" + itemActiveClass, ident.Department, false, null, "text", null, () => updateIdentBasicData(ident.AnNo), true));
        row.appendChild(fn.createTableRowElement("input", "setItemType-" + ident.AnNo, "form-control" + itemActiveClass, ident.SetItemType, false, null, "text", null, () => updateIdentBasicData(ident.AnNo), true));

        var actions = fn.createTableRowElement("td", null, "actions-cell");
        var button = docTypePermissionCtrl.decorateElt(
            fn.createTableRowElement("button", "noteModal-" + ident.AnNo, "identNote btn btn-link " + (ident.Note !== "" ? "btn-active" : ""), null, false, null, "button",
                //this.onOpenNoteModal
                //() => this.onOpenNoteModal(ident, row)),
                null),
            'Write',
            'disabled');
        button.setAttribute('data-note-ident-position', ident.AnNo);
        button.appendChild(fn.createTableRowElement("i", null, "fa fa-comment"));
        button.appendChild(docTypePermissionCtrl.decorateElt(
            fn.createTableRowElement("input", "note-" + ident.AnNo, "hidden", ident.Note),
            'Write',
            'disabled'));
        button.appendChild(docTypePermissionCtrl.decorateElt(
            fn.createTableRowElement("input", "acKey-" + ident.AnNo, "hidden", ident.AcKey),
            'Write',
            'disabled'));
        button.appendChild(docTypePermissionCtrl.decorateElt(
            fn.createTableRowElement("input", "vat-" + ident.AnNo, "hidden", ident.VAT),
            'Write',
            'disabled'));
        button.appendChild(docTypePermissionCtrl.decorateElt(
            fn.createTableRowElement("input", "priceCurrency-" + ident.AnNo, "hidden", ident.PriceCurrency),
            'Write',
            'disabled'));
        button.appendChild(docTypePermissionCtrl.decorateElt(
            fn.createTableRowElement("input", "serialNo-" + ident.AnNo, "hidden", ident.SerialNo),
            'Write',
            'disabled'));
        button.appendChild(docTypePermissionCtrl.decorateElt(
            fn.createTableRowElement("input", "dateDue-" + ident.AnNo, "hidden", ident.DateDue),
            'Write',
            'disabled'));
        button.appendChild(docTypePermissionCtrl.decorateElt(
            fn.createTableRowElement("input", "qtyOld-" + ident.AnNo, "hidden", 0),
            'Write',
            'disabled'));
        actions.appendChild(button);

        var deleteBtn = docTypePermissionCtrl.decorateElt(
            fn.createTableRowElement("button", "identRemove-" + ident.AnNo, "btn btn-outline editableInput", null, false, null, "button", null),
            'Delete',
            'disabled');
        deleteBtn.setAttribute('data-remove-ident-position', ident.AnNo);
        deleteBtn.appendChild(fn.createTableRowElement("span", null, "far fa-trash-alt"));
        actions.appendChild(deleteBtn);
        row.appendChild(actions);

        return row;
    };

    InventoryFixedAssetsIdents.prototype.removeIdentPosition = function (ident) {
        var self = this;
        $().customModal({
            title: "",
            destroyOnClose: true,
            showCloseButton: true,
            id: "removeInventoryFixedAssetsIdentConfirmation",
            type: "question",
            text: this.context.localization.confirmDeleteMsg,
            buttons: [
                {
                    icon: 'fa fa-times',
                    text: this.context.localization.no,
                    closeOnAction: true,
                    type: 'ghost'
                },
                {
                    icon: 'fa fa-check',
                    text: this.context.localization.yes,
                    action: function () {
                        inventoryFixedAssetsIdentService.removeIdentPosition(ident);
                    },
                    closeOnAction: true,
                    type: 'primary'
                }
            ]
        });
    };


    paw.view.inventoryFixedAssets.InventoryFixedAssetsIdents = InventoryFixedAssetsIdents;
})(paw, paw.utils, paw.view.inventoryFixedAssets.inventoryFixedAssetsIdentService, fn = {
    initializeNumberInputs: initializeNumberInputs,
    reorderColumns: reorderColumns,
    createTableRowElement: createTableRowElement,
    initializeInvoiceIdentInputs: initializeInvoiceIdentInputs
});