(function (paw, utils, fn) {
    function InventoryFixedAssetsList(settings, localization) {
        var self = this;
        this.settings = paw.lang.append({
            $dataTable: undefined,
            $keywords: undefined,
            $dateFrom: undefined,
            $dateTo: undefined,
            $docType: undefined,
            $itemsDisplayType: undefined,
            InventoryFixedAssetsType: {
                FixedAssets: 'FixedAssets',
                SmallInventory: 'SmallInventory'
            },
            VerifyStatus: {
                Confirmed: 'Confirmed',
                NotConfirmed: 'NotConfirmed'
            }
        }, settings);

        this.localization = paw.lang.append({
            fixedAssets: 'FixedAssets',
            smallInventory: 'SmallInventory',
            confirmed: '@Loc.Confirmed',
            notConfirmed: '@Loc.NotConfirmed'
        }, localization);

        this.searchDebounced = fn.debouncer(function () {
            $.fn.dataTable.reload(self.settings.$dataTable);
        }, 300);

        this.onSearch = function () {
            self.searchDebounced();
        };
    }

    InventoryFixedAssetsList.prototype.init = function () {
        var self = this;
        this.settings.$dataTable.DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url: this.settings.$dataTable.attr('data-url'),
                data: function (d) {
                    d.Keywords = self.settings.$keywords.val();
                    d.DateFrom = fn.reformatDate(self.settings.$dateFrom.val());
                    d.DateTo = fn.reformatDate(self.settings.$dateTo.val());
                    d.DocType = self.settings.$docType.val();
                    d.ItemsDisplayType = self.settings.$itemsDisplayType.val();
                }
            },
            columns: [
                {
                    data: "Id",
                    orderable: false,
                    className: 'dt-body-center',
                    render: $.fn.dataTable.render.checkbox('documentSelectionChanged')
                },
                {
                    "data": "AcKey",
                    name: "AcKey",
                    className: 'dt-body-center',
                    render: function (data, type, full) {
                        return '<button type="button" class="btn btn-ghost dropdown-toggle p-none" data-toggle="dropdown" aria-expanded="false"><span class="fa fa-eye fa-lg text-muted m-r-xs"></span><span class="text-muted caret"></span></button>';
                    }
                },
                {
                    "data": "AcKeyView",
                    name: "AcKey",
                    render: $.fn.dataTable.render.link('AcKey')
                },
                { "data": "InventoryDate" },
                {
                    "data": "InventoryType",
                    render: function (inventoryType) {
                        switch (inventoryType) {
                            case self.settings.InventoryFixedAssetsType.FixedAssets:
                                return self.localization.fixedAssets;
                            case self.settings.InventoryFixedAssetsType.SmallInventory:
                                return self.localization.smallInventory;
                            default:
                                return inventoryType;
                        }
                    }
                },
                {
                    "data": "Status",
                    "render": function (status) {
                        if (status === self.settings.VerifyStatus.Confirmed) {
                            return '<span class="label label-default">' + self.localization.confirmed + '</span>';
                        }
                        return '<span class="label label-primary">' + self.localization.notConfirmed + '</span>';
                    }
                }

            ]
        });

        this.settings.$keywords.off('keyup', this.onSearch).on('keyup', this.onSearch);
        this.settings.$docType.on('change', function () {
            $.fn.dataTable.reload(self.settings.$dataTable);
        });
        this.settings.$itemsDisplayType.on('change', function () {
            $.fn.dataTable.reload(self.settings.$dataTable);
        });
        this.settings.$dateFrom.on('dp.change', function () {
            $.fn.dataTable.reload(self.settings.$dataTable);
        });
        this.settings.$dateTo.on('dp.change', function () {
            $.fn.dataTable.reload(self.settings.$dataTable);
        });
    };

    paw.view.inventoryFixedAssets.InventoryFixedAssetsList = InventoryFixedAssetsList;
})(paw, paw.utils, {
    debouncer: debouncer,
    reformatDate: reformatDate
});