(function (paw, utils, inventoryFixedAssetsService) {
    function InventoryFixedAssetsCreate(settings, localization) {
        this.settings = paw.lang.append({
            $form: undefined,
            $saveBtn: undefined,
            $deleteBtn: undefined,
            $confirmBtn: undefined,
            $createBtn: undefined,
            editUrl: undefined,
            editMode: false,
            ident: {},
            formatting: {
                decimalPlacesPrice: 2,
                decimalPlacesQuantity: 1
            }
        }, settings);
        this.localization = paw.lang.append({
            language: "@Loc.Language",
            deleteInventoryFixedAssetsMsg: "@Loc.DeleteInventoryDocument",
            noResults: '@Loc.NoResults',
            name: '@Loc.Name',
            ident: '@Loc.Ident',
            price: '@Loc.Price',
            stock: '@Loc.Stock',
            confirmDeleteMsg: '@Loc.ConfirmDeleteItemMessage',
            no: '@Loc.No',
            yes: '@Loc.Yes'
        }, localization);

        this.idents = new paw.view.inventoryFixedAssets.InventoryFixedAssetsIdents(this, this.settings.ident, this.localization, this.settings.formatting, this.settings.editMode);
    }

    InventoryFixedAssetsCreate.prototype.init = function () {
        if (this.settings.editMode) {
            this.idents.init();
        }

        this.bootstrapComponents();
    };

    InventoryFixedAssetsCreate.prototype.onSave = function (e) {
        var self = this;
        utils.async.asyncAction(function () {
            return self.save();
        }, this.settings.$saveBtn, [
            this.settings.$deleteBtn,
            this.settings.$confirmBtn,
            this.settings.$createBtn
        ]);
    };

    InventoryFixedAssetsCreate.prototype.onRemove = function () {
        var self = this;
        var data = this.compileDataModelFromFormData();
        $().customModal({
            title: "",
            destroyOnClose: true,
            showCloseButton: true,
            id: "deleteInventoryFixedAssetsAlertModal",
            type: "question",
            text: self.localization.deleteInventoryFixedAssetsMsg,
            buttons: [
                {
                    icon: 'fa fa-times',
                    text: self.localization.no,
                    closeOnAction: true,
                    type: 'ghost'
                },
                {
                    icon: 'fa fa-check',
                    text: self.localization.yes,
                    action: async function () {
                        utils.async.asyncAction(function () {
                            return inventoryFixedAssetsService.remove({
                                AcKey: data.AcKey
                            });
                        }, self.settings.$deleteBtn, [
                            self.settings.$saveBtn,
                            self.settings.$confirmBtn,
                            self.settings.$createBtn
                        ]);
                        
                    },
                    closeOnAction: true,
                    type: 'primary'
                }
            ]
        });;
    };

    InventoryFixedAssetsCreate.prototype.onConfirm = function () {
        utils.async.asyncAction(function () {
            return inventoryFixedAssetsService.confirm();
        }, this.settings.$confirmBtn, [
            this.settings.$deleteBtn,
            this.settings.$saveBtn,
            this.settings.$createBtn
        ]);
    };

    InventoryFixedAssetsCreate.prototype.save = function () {
        var self = this;
        var data = this.compileDataModelFromFormData();
        if (this.settings.editMode) {
            return inventoryFixedAssetsService.update(data);
        }
        return inventoryFixedAssetsService.create(data).then(function (response) {
            window.history.pushState({}, null, self.settings.editUrl + response.data.AcKey);
            location.reload();
        });
    };

    InventoryFixedAssetsCreate.prototype.compileDataModelFromFormData = function () {
        var formData = this.settings.$form.serializeArray();
        formData.push({ name: 'ItemsJsonString', value: JSON.stringify(this.idents.idents) });
        var data = {};
        utils.iterable.iterate(formData, function (item) {
            data[item.name] = item.value;
        });
        return data;
    };

    InventoryFixedAssetsCreate.prototype.bootstrapComponents = function () {
        $(".datepicker").datepicker({
            format: 'dd.mm.yyyy',
            language: this.language
        }).on('changeDate', function () {
            $(this).datepicker('hide');
        });
    };

    paw.view.inventoryFixedAssets.InventoryFixedAssetsCreate = InventoryFixedAssetsCreate;
})(paw, paw.utils, paw.view.inventoryFixedAssets.inventoryFixedAssetsService);